Codeforces Round #782 (Div. 2)

Codeforces Round #782 (Div. 2)

A. Red Versus Blue

[Link](Problem - A - Codeforces)

题意

​ 让你构造一个长度为 n n n的字符串有 r r r R R R b b b B B B b < r    b + r = n b<r \ \ b+r=n b<r  b+r=n且连续相同的字符串长度最大值最小。

思路

  • 贪心

由于 r > b r>b r>b,我们最优解应该是将 r r r分成尽量多且平均的段,这些段由 B B B来分割,也就是将 r r r分成 b + 1 b+1 b+1份,且尽可能均衡,所以先给每份一个 r / ( b + 1 ) r/(b+1) r/(b+1),剩下的 r % ( b + 1 ) r\%(b+1) r%(b+1)从前往后依次分给每一份直到分完即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        int b, r; cin >> n >> r >> b;
        int k = r / (b + 1);
        for (int i = 1; i <= b + 1; i ++) a[i] = k;
        int kk = r % (b + 1);
        for (int i = 1; i <= kk; i ++) a[i] ++;
        for (int i = 1; i <= b + 1; i ++) {
            while (a[i] --) cout << 'R';
            if (i != b + 1) cout << 'B';
        }

        cout << '\n';
    }
    return 0;
}

B. Bit Flipping

[Link](Problem - B - Codeforces)

题意

​ 给你一个 01 01 01串,每次你可以选择一个位置 i i i并让除了 i i i以外所有的位置取反,问你 k k k次操作后字典序最大是多少。

思路

  • 贪心

​ 因为要字典序最小,我们从前往后贪心,对于每一位我们想让它变成 1 1 1,当前这一位的是否改变取决于转的奇偶,对于第 i i i位分类来看:

  • 当前 1 1 1则我们想让它转偶数次,因此如果 k % 2 = = 1 k\%2==1 k%2==1,我们就让当前这一位转一次,其它位对它的影响就是偶数了
  • 当前 0 0 0则我们想让它转奇数次,因此如果 k % 2 = = 0 k\%2==0 k%2==0,我们就让当前这一位转一次,其它位对它的影响就是奇数了

如果还剩操作数,这些操作都给最后一位,如果贪到中间操作数用完了,注意后面数的奇偶性改变一下。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N], b[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        cin >> n >> k;
        int kk = k;
        string str; cin >> str;
        for (int i = 0; i < n; i ++) a[i] = 0, b[i] = (str[i] - '0');
        int i = 0;
        for (; i < n; i ++) {
            if (!k) break;
            if (i == n - 1) {
                a[i] = k;
                k = kk - k;
                k %= 2;
                if (k) b[i] ^= 1;
                break;
            }
            if (str[i] == '1' && kk % 2 == 0) b[i] = 1;
            else if (str[i] == '1' && kk % 2) {
                a[i] ++;
                k --;
                b[i] = 1;
                if (!k) break;
            }
            else if (str[i] == '0' && kk % 2) b[i] = 1;
            else {
                a[i] ++;
                k --;
                b[i] = 1;
                if (!k) break;
            }
        } 
        i ++;
        for (; i < n; i ++) 
            if (kk % 2) b[i] ^= 1;
        for (int i = 0; i < n; i ++) cout << b[i];
        cout << '\n';
        for (int i = 0; i < n; i ++)
            cout << a[i] << ' ';
        cout << '\n';
        }
    return 0;
}

C. Line Empire

[Link](Problem - C - Codeforces)

题意

​ 数轴有 n n n个单调上升的点,一开始你在 0 0 0号点,假设当前你在第 i i i个点你有两个操作:

  1. 占领右边第一个没被占领的点 x j x_j xj,花费 b × ∣ x j − x i ∣ b\times |x_j-x_i| b×xjxi

  2. 移动到右边某个占领的点 x j x_j xj,花费 a × ∣ x j − x i ∣ a\times|x_j-x_i| a×xjxi

问你占领 n n n个点的最小花费是多少。

思路

  • 贪心,前缀和

我们记录一下当前在哪个位置 p r e pre pre,设第 i i i个位置为 x i x_i xi s i = ∑ j = 1 j = i x j s_i=\sum_{j=1}^{j=i}x_j si=j=1j=ixj,每次我们占领一个位置后判断一下,从当前位置移动到占领的位置是否会更优,即是否满足 a × ∣ x i − x p r e ∣ + ( s n − s i − ( n − i ) × x p r e ) < ( s n − s i − ( n − i ) × x i ) a\times|x_i-x_{pre}|+(s_n - s_i-(n-i)\times x_{pre})<(s_n-s_i-(n-i)\times x_i) a×xixpre+(snsi(ni)×xpre)<(snsi(ni)×xi),整理一下即是否满足 a × ∣ x i − x p r e ∣ + ( n − 1 ) × ( x i − x p r e ) < 0 a\times |x_i-x_{pre}|+(n-1)\times(x_i-x_{pre})<0 a×xixpre+(n1)×(xixpre)<0,如果满足我们就移动。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
LL s[N], p[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        LL a, b;
        cin >> n >> a >> b;
        for (int i = 1; i <= n; i ++) cin >> p[i];
        for (int i = 1; i <= n; i ++) s[i] += s[i - 1] + p[i];
        LL res = 0;
        int id = 0;
        for (int i = 1; i <= n; i ++) {
            res += b * (p[i] - p[id]);
            if (a * (p[i] - p[id]) + b * ((n - i) * (p[id] - p[i])) < 0) res += a * (p[i] - p[id]), id = i;
        }
        
        cout << res << '\n';
    }
    return 0;
}

D. Reverse Sort Sum

[Link](Problem - D - Codeforces)

题意

​ 给你一个只含 0 , 1 0,1 0,1的数组 A A A,设 f ( k , A ) f(k,A) f(k,A)为将 A A A的前 k k k个数从小到大排序,之后得到新数组 B k B_k Bk,设 C = B 1 + B 2 + . . . + B n C=B_1+B_2+...+B_n C=B1+B2+...+Bn,每个 B B B的每一位分别相加,现给你 C C C让你求出与之对应的 A A A

思路

  • 构造

​ 这种题就是要从小样例来推大的,我们要找一些性质,首先发现因为每个 B i B_i Bi只是改变了原数组的顺序,因此我们统计一下 C C C的和 s u m sum sum A A A中的 1 1 1的数量即 c n t = s u m / n cnt=sum/n cnt=sum/n

​ 观察一下每个 C i C_i Ci是如何构成的:对于每个 C i C_i Ci B i B_i Bi之前加的是原来的 A i A_i Ai B i B_i Bi包括之后是排序以后加的对应 A i A_i Ai,如果可以将 B i B_i Bi以及之后的这些加的数都消除掉,那么就可以直接判断当前这一位是否大于 0 0 0来确定这一位 A i A_i Ai是什么了。

​ 因此我们可以倒着考虑,维护当前还剩 c n t cnt cnt 1 1 1没放置,对于第 i i i轮,因为会将 [ 1 , i ] [1,i] [1,i]排序,因此 [ i − c n t + 1 , i ] [i-cnt+1,i] [icnt+1,i]这些位置都是 1 1 1,相当于这些位置都会对对应的 C i C_i Ci加一,倒着考虑就将这些位置减 1 1 1,如果操作完第 i i i个位置大于 0 0 0那么这个位置就是 1 1 1。这样区间减对于确定第 i − 1 i-1 i1个位置(即判断第 i − 1 i-1 i1轮)时第 i i i个位置(即第 i i i轮)对它影响就消除了,同理前面的也均在那些轮消除了。

​ 对于动态区间加,和查询某个位置的值,我们可以对原数组差分一下,用个树状数组来维护。

​ 注意特判一下第一个位置,因为第一个位置前面没有位置,他没有每个 C i C_i Ci B i B_i Bi之前加的是原来的 A i A_i Ai,无论怎么减完都是 0 0 0,因此最后特判一下是否还剩下 1 1 1即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, k;
int a[N], c[N];
int tr[N];
int lowbit(int x) {
    return x & -x;
}
void add(int x, int v) {
    for (; x <= n; x += lowbit(x)) tr[x] += v;
}
int sum(int x) {
    int res = 0;
    for (; x; x -= lowbit(x)) res += tr[x];
    return res;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        cin >> n;
        LL cnt = 0;
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
            cnt += a[i];
            c[i] = 0;
            tr[i] = 0;
        }
       
        for (int i = n; i; i --) a[i] -= a[i - 1], add(i, a[i]);
        
        cnt /= n;
        for (int i = n; i; i --) {
            if (!cnt) break;
            add(i - cnt + 1, -1);
            if (sum(i) > 0) c[i] = 1, cnt --;
        }

        if (cnt) c[1] = 1; 
        for (int i = 1; i <= n; i ++)
            cout << c[i] << ' ';
        
        cout << '\n';
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值