Codeforces Round #373 (Div. 2) ADCE题解

来源:互联网 发布:怎么成为网络写手 编辑:程序博客网 时间:2024/05/22 18:23

A. Vitya in the Countryside

给出一个循环的序列的一部分,求下一个数是应该UP还是DOWN

0和15直接输出,其余根据倒数第二个数判断。

#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;typedef pair<int,int> PII;#define mp make_pair#define pb push_back#define FIN freopen("in.txt", "r", stdin);#define FOUT freopen("out.txt", "w", stdout);#define lson l, mid, cur << 1#define rson mid + 1, r, cur << 1 | 1#define lowbit(x) ((x)&(-x))#define bitcnt(x) __builtin_popcount(x)#define bitcntll(x) __builtin_popcountll(x)#define debug puts("-------------");//#pragma comment(linker, "/STACK:1024000000,1024000000")const int INF = 0x3f3f3f3f;const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;const double eps = 1e-8;const int MOD = 1e9 + 7;const int MAXN = 1e2 + 50;const int MAXM = 1e4 + 50;int n, a[MAXN];int main() {#ifdef LOCAL_NORTH    FIN;#endif // LOCAL_NORTH    while (~scanf("%d", &n)) {        for (int i = 0; i < n; i++) scanf("%d", &a[i]);        if (n == 1) {            if (a[0] == 15) puts("DOWN");            else if (a[0] == 0) puts("UP");            else puts("-1");        }        else {            if (a[n - 1] > a[n - 2]) {                if (a[n - 1] == 15) puts("DOWN");                else puts("UP");            } else {                if (a[n - 1] == 0) puts("UP");                else puts("DOWN");            }        }    }#ifdef LOCAL_NORTH    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;#endif // LOCAL_NORTH    return 0;}

B. Anatoly and Cockroaches

一个只含有r和b的序列,每次可以序列内交换位置,或者直接改变某个位置,输出变为rb交替的序列的最小操作次数。

结果只有两种情况,然后分情况统计一下需要变换的位置,然后取max(b->a的个数,a->b的个数)。

#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;typedef pair<int,int> PII;#define mp make_pair#define pb push_back#define FIN freopen("in.txt", "r", stdin);#define FOUT freopen("out.txt", "w", stdout);#define lson l, mid, cur << 1#define rson mid + 1, r, cur << 1 | 1#define lowbit(x) ((x)&(-x))#define bitcnt(x) __builtin_popcount(x)#define bitcntll(x) __builtin_popcountll(x)#define debug puts("-------------");//#pragma comment(linker, "/STACK:1024000000,1024000000")const int INF = 0x3f3f3f3f;const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;const double eps = 1e-8;const int MOD = 1e9 + 7;const int MAXN = 1e5 + 50;const int MAXM = 1e4 + 50;int n;char c[MAXN];int main() {#ifdef LOCAL_NORTH    FIN;#endif // LOCAL_NORTH    while (~scanf("%d", &n)) {        scanf("%s", &c);        int ans = INF, rb = 0, br = 0;        for (int i = 0; i < n; i++) { // num one is black            if (c[i] != (i % 2 ? 'b' : 'r')) {                if (c[i] == 'b') rb++;                else br++;            }        }        ans = min(ans, max(rb, br));        rb = 0, br = 0;        for (int i = 0; i < n; i++) { // num one is red            if (c[i] !=  (i % 2 ? 'r' : 'b')) {                if (c[i] == 'r') rb++;                else br++;            }        }        ans = min(ans, max(rb, br));        printf("%d\n", ans);    }#ifdef LOCAL_NORTH    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;#endif // LOCAL_NORTH    return 0;}

C. Efim and Strange Grade

给出一个小数,最多可以进行k次四舍五入,输出可以得到的最大结果。

找到从左往右数,第1个可以四舍五入的位置,然后从这一位开始往左进行进位操作和四舍五入操作,每一次进位k--,直到k=0或者到小数点。

到了小数点之后,不管k是否为0,都不能再四舍五入,这时只能进行进位操作了。

#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;typedef pair<int,int> PII;#define mp make_pair#define pb push_back#define FIN freopen("in.txt", "r", stdin);#define FOUT freopen("out.txt", "w", stdout);#define lson l, mid, cur << 1#define rson mid + 1, r, cur << 1 | 1#define lowbit(x) ((x)&(-x))#define bitcnt(x) __builtin_popcount(x)#define bitcntll(x) __builtin_popcountll(x)#define debug puts("-------------");//#pragma comment(linker, "/STACK:1024000000,1024000000")const int INF = 0x3f3f3f3f;const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;const double eps = 1e-8;const int MOD = 1e9 + 7;const int MAXN = 2e5 + 50;const int MAXM = 1e4 + 50;int n, t;char c[MAXN];int main() {#ifdef LOCAL_NORTH    FIN;#endif // LOCAL_NORTH    while (~scanf("%d%d%s", &n, &t, c)) {        int pos = -1;        bool ok = false;        for (int i = 0; c[i]; i++) {            if (c[i] == '.') {                ok = true;                continue;            }            if (!ok) continue;            if (c[i] >= '5') {                pos = i;                break;            }        }        if (pos == -1) {            puts(c);            continue;        }        int i = pos;        bool carry = false, flag = true;        while (i >= 0) {            if (c[i] == '.') {                i--;                flag = false;                continue;            }            if (!flag) {                if (!carry) break;                else if (c[i] == '9') {                    c[i]= '0';                } else {                    c[i]++;                    carry = false;                    break;                }            }            else {                if (!carry && c[i] >= '5' && t > 0) {                    carry = true;                    c[i] = '0';                    t --;                } else if (carry && c[i] == '9') {                    c[i] = '0';                } else if (carry && c[i] < '5') {                    c[i]++;                    i++;                    carry = false;                }            }            i--;        }        c[pos] = 0;        if (carry) printf("1");        int t = 0;        for (int i = strlen(c) - 1; i >= 0 && c[i] == '0';  i--) t++;        c[strlen(c) - t] = 0;        if (c[strlen(c) - 1] == '.') c[strlen(c) - 1] = 0;        puts(c);    }#ifdef LOCAL_NORTH    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;#endif // LOCAL_NORTH    return 0;}

E. Sasha and Array

一个1e5的序列,有2种操作:

1)区间[l,r]加x

2)查询区间[l,r]内的F[i]的和。F[i]为斐波那契数列的第i项。

线段树套矩阵。

可以简单地理解为把普通的线段树区间更新区间查询的节点以及lazy tag替换为矩阵。

由于矩阵运算的结合律,我们在PushDown的时候可以直接节点与lazy tag相乘。

全部用矩阵来搞的话会跑5000ms左右,把节点变成一个二维数组的话会快很多。

下面是节点为数组的代码。节点为矩阵的代码和这个几乎完全一样。

#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef __int64 LL;typedef pair<int, int> PII;#define mp make_pair#define pb push_back#define FIN freopen("in.txt", "r", stdin);#define FOUT freopen("out.txt", "w", stdout);#define lson l, mid, cur << 1#define rson mid + 1, r, cur << 1 | 1#define lowbit(x) ((x)&(-x))#define bitcnt(x) __builtin_popcount(x)#define bitcntll(x) __builtin_popcountll(x)//#pragma comment(linker, "/STACK:1024000000,1024000000")const int INF = 0x3f3f3f3f;const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;const double ERR = 1e-8;const int MOD = 1e9 + 7;const int MAXN = 1e5 + 50;const int MAXM = 2e5 + 50;struct Matrix {    int n, m, d[5][5];    void init(int _n = 2, int _m = 2) {        n = _n, m = _m;        memset(d, 0, sizeof(d));    }    Matrix(int _n = 2, int _m = 2) {        n = _n, m = _m;        memset(d, 0, sizeof(d));    }    Matrix operator * (const Matrix& B) const {        Matrix C(n, B.m);        for(int i = 0; i < n; i++)            for(int j = 0; j < B.m; j++)                for(int k = 0; k < m; k++)                    C.d[i][j] = ((LL)d[i][k] * B.d[k][j] + C.d[i][j]) % MOD;        return C;    }    Matrix operator + (const Matrix& B) const {        Matrix C(n, m);        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)                C.d[i][j] = ((LL)d[i][j] + B.d[i][j]) % MOD;        return C;    }    Matrix operator ^ (LL c) const {        Matrix A(n, m), B(n, m);        B.d[0][0] = B.d[1][1] = 1;        B.d[0][1] = B.d[1][0] = 0;        for (int i = 0; i < n; i++)            for (int j = 0; j < m; j++) {                A.d[i][j] = d[i][j];            }        while(c) {            if(c & 1) B = B * A;            A = A * A;            c >>= 1;        }        return B;    }};int n, q;LL sum[MAXN << 2][2];Matrix lazy[MAXN << 2], r;bool col[MAXN << 2];void PushUp(int cur) {    sum[cur][0] = (sum[cur << 1][0] + sum[cur << 1 | 1][0]) % MOD;    sum[cur][1] = (sum[cur << 1][1] + sum[cur << 1 | 1][1]) % MOD;}void updone(int i, Matrix x) {    Matrix A(2, 2);    A.d[0][0] = (sum[i][0] + sum[i][1]) % MOD;    A.d[0][1] = A.d[1][0] = sum[i][0];    A.d[1][1] = sum[i][1];    A = A * x;    sum[i][0] = A.d[0][1];    sum[i][1] = A.d[1][1];    if (col[i])        lazy[i] = lazy[i] * x;    else        lazy[i] = x;    col[i] = true;}void PushDown(int cur) {    if (col[cur]) {        updone(cur << 1, lazy[cur]);        updone(cur << 1 | 1, lazy[cur]);        col[cur] = false;        lazy[cur].init();    }}void build(int l, int r, int cur) {    lazy[cur].init();    col[cur] = false;    if (l == r) {        sum[cur][0] = 1;        sum[cur][1] = 0;        return;    }    int mid = (l + r) / 2;    build(lson);    build(rson);    PushUp(cur);}void update(int l, int r, int cur, int a, int b, Matrix inc) {    if (a <= l && r <= b) {        updone(cur, inc);        return;    }    PushDown(cur);    int mid = (l + r) / 2;    if (a <= mid) update(lson, a, b, inc);    if (b > mid) update(rson, a, b, inc);    PushUp(cur);}LL query(int l, int r, int cur, int a, int b) {    if (a <= l && r <= b) {        return sum[cur][0];    }    PushDown(cur);    int mid = (l + r) / 2;    LL ret = 0;    if (a <= mid) ret = (ret + query(lson, a, b)) % MOD;    if (b > mid) ret = (ret + query(rson, a, b)) % MOD;    return ret;}int main() {#ifdef LOCAL_NORTH    FIN;#endif // LOCAL_NORTH    r.d[0][0] = r.d[0][1] = r.d[1][0] = 1;    while (~scanf("%d%d", &n, &q)) {        build(1, n, 1);        Matrix c;        for (int i = 1; i <= n; i++) {            int t;            scanf("%d", &t);            c = r ^ (t - 1);            update(1, n, 1, i, i, c);        }        while (q--) {            int t, a, b, x;            scanf("%d%d%d", &t, &a, &b);            if (t == 1) {                scanf("%d", &x);                c = r ^ x;                update(1, n, 1, a, b, c);            } else printf("%I64d\n", query(1, n, 1, a, b));        }    }#ifdef LOCAL_NORTH    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;#endif // LOCAL_NORTH    return 0;}


0 0