cf619 div2 abcd

传送门


A Three Strings standard input/output 1 s, 256 MB Submit Add to favourites x12927

 因为要求必须 n 步 交换, 所以只需要所有的 i (0-n-1)  if((c[i]==b[i]) || (c[i]==a[i])) 即可,
B Motarack's Birthday standard input/output 2 s, 256 MB Submit Add to favourites x8278

 需要把相邻的两个数差值最小, 只有未知数的差值可以有变化 所以只需要记录未知数也就是-1的左右数字的最大值和最小值即可, k = max+min>>1  然后遍历更新一下m即可
C Ayoub's function standard input/output 1 s, 256 MB Submit Add to favourites x5585

有n个字符,m个1, 问最大 含1的子串数量, 可以试着直接求最小 含0的字符串数量  然后用 总的子串数相减即可  容斥+贪心?

有 m 个 1 将 0分开,  可以看成     0...0 1  0..0 0..0 1 0..0  1  0..0  1 0..0  1 0...0,  将 n-m个0 尽可能平均分在m+1个区间中, 每个区间的0串数量 为 l*(l+1)/2   l为区间长度, 令 z = n-m, g =  m+1, k = floor(z/g), x = z%g  然后把m+1个组分为x个有(k+1)0      g-x 个 (k)0

就是把 n-m 个 0 平均分在m+1 个区间中, 如果有多余的再多分一个给一些区间

我还特判了一下z <= g 直接输出 n*(n+1)/2-z即可

D Time to Run standard input/output 1 s, 256 MB Submit Add to favourites x2688

Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times.
这句话我不是很懂, 但结合题解 好像意思是 不能强行经过同一个店两次  所以构造的路径 最后一步向上回到起点是可行的?

     最大化路径如左图, 先绿 -> 黄 -> 绿-> 粉红-> 黄 -> 绿-> 粉红-> 黑色, 齐活   长度为 4mn-2n-2m

这样构造的路径长度为 3n+1 (向右 和 上下左 与 向下 分别为 n步, 再加左下角一条向上的路径)  <= 3e3

其中这条路径的存储用的是 vector<string, int> v   储存每步的动作 和 这步的步数, 而不是构造一个string

如果这条路径长度小于 k  无解,  

否则把这条最长的路径从后削减直到长度为 k, 其中有些细节需要处理  比如说 n*1  或者  1*m  路径中有空串, 所以需要去除,
 然后这样构造的路径

#include <bits/stdc++.h> 
using namespace std;
#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); i++) 
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)

//cf 619 div2
void taskA(){
    int t; cin >> t;
    while(t--) {
        string a,b,c; cin >> a >> b >> c;
        int n = a.size(), f = 0;
        _for(i,0,n) {
            if((c[i]==b[i]) || (c[i]==a[i]))
                continue;
            else {
                f = 1; break;
            }
        }
        cout << (f? "NO
": "YES
");
    }
    return;
}
void taskB() {
    int t; cin >> t;
    while(t--) {
        int n; cin >> n;
        vector<int> a(n);
        _for(i,0,n) cin >> a[i];
        int mi = 1000000000, ma = 0;
        _for(i,0,n) 
            if(a[i] == -1) 
            {
                if(i and a[i-1]!=-1) mi = min(mi, a[i-1]), ma = max(ma, a[i-1]);
                if(i+1<n and a[i+1]!=-1) mi = min(mi, a[i+1]), ma = max(ma, a[i+1]);
            }
        int k = mi+ma>>1, m = 0;
        _for(i,0,n) if(a[i] == -1) a[i] = k;
        _for(i,1,n) m = max(m, abs(a[i]-a[i-1]));
        cout << m << " " << k << "
";
    }
    return;
}
void taskC() {
    int t; cin >> t;
    while(t--) {

        ll n,m; cin >> n >> m;
        ll z = n-m, g = m+1;
        if(z <= m+1) {
            cout << n*(n+1)/2-z << "
";
            continue;
        }
        ll k = z/g;
        ll x = z%g;
        ll ans = n*(n+1)/2-k*(k+1)/2*(g-x)-(k+1)*(k+2)/2*x;
        cout << ans << "
";
    }
    return;
}
vector<pair<string, int> > v, v2;

void fix(vector<pair<string, int> > &v) {
    v2 = v;
    v.clear();
    int len = v2.size();
    _for(i,0,len) 
      if(v2[i].second)
        v.push_back(v2[i]);
    return;
}

void taskD(){
  int n,m,k; cin >> n >> m >> k;
  int all = 4*n*m-2*n-2*m;
  if(all < k) { cout << "NO
"; return; }
  cout << "YES
";
  v.clear();
  _rep(i,1,n) {
    v.push_back({"R", m-1});
    if(i==1) 
      v.push_back({"L", m-1});
    else 
      v.push_back({"UDL", m-1});

    if(i!=n) v.push_back({"D", 1});
    else v.push_back({"U", n-1});
  }
  fix(v);
  while(all > k) {
    string tmp = v.back().first;
    int sec = v.back().second;
    int len1 = (int)tmp.size();
    int cur = len1*sec;
    v.pop_back();
    all -= cur;
    if(all >= k) continue;
    cur = k-all;
    int t1 = cur/len1;
    if(t1)  v.push_back({tmp, t1});
    
    if(cur%len1)    tmp.resize(cur%len1), v.push_back({tmp, 1});
    all = k;
  }
  int len = v.size();
  cout << len << "
";
  
  _for(i,0,len) cout << v[i].second << " " << v[i].first << "
";
  return;
}
int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    //taskA();
    //taskB();
    taskC();
    //taskD();
    return 0;
}
原文地址:https://www.cnblogs.com/163467wyj/p/12880107.html