Codeforces Round #197 (Div. 2)

A.Helpful Maths

分析:将读入的字符转化为数字,直接排个序就可以了。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 500;
int seq[N];

int main() {
    char c;
    int t = 0, idx = 0;
    while ((c = getchar()) != EOF) {
        if (c == '+' || c == '\n') {
            seq[idx++] = t;
            t = 0;
        }
        else t = t * 10 + c - '0';
    }
    sort(seq, seq+idx);
    printf("%d", seq[0]);
    for (int i = 1;  i < idx; ++i) printf("+%d", seq[i]);
    puts("");
    return 0;
} 
View Code

 

B.Xenia and Ringroad

分析:模拟即可,大于当前位置直接走,否则绕一圈后走到目标位置。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;
int n, m;

int main() {
    while (scanf("%d %d", &n, &m) != EOF) {
        int last = 1, x;
        LL ret = 0;
        while (m--) {
            scanf("%d", &x);
            if (x >= last) {
                ret += x - last;
                last = x;
            } else {
                ret += n-last+x;
                last = x;
            }
        }
        printf("%I64d\n", ret);
    }
    return 0;
}
View Code

 

C.Xenia and Weights

分析:直接搜索即可,无法证明为何会如此快的找到答案或者退出。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1005;
char str[15];
int m;
int path[N];

bool dfs(int p, int left, int right, int last) {
    if (p == 0) return true;
    int delta = abs(left-right);
    for (int i = 1; i <= 10; ++i) {
        if (last == i) continue;
        if (str[i] && delta >= 0 && delta < i) {
            path[p] = i;
            if (left < right) {
                if (dfs(p-1, left+i, right, i)) return true;
            }
            else if (dfs(p-1, left, right+i, i)) return true;
        }
    }
    return false;
}

int main() {
    scanf("%s", str+1);
    scanf("%d", &m);
    for (int i = 1; i <= 10; ++i) str[i] -= '0';
    if (!dfs(m, 0, 0, 0)) {
        puts("NO");
    } else {
        puts("YES");
        for (int i = m; i > 0; --i) {
            printf(i == m ? "%d" : " %d", path[i]);
        }
        puts("");
    }
    return 0;
}
View Code

 

D.Xenia and Bit Operations

分析:由于每次改变的位置只会改变与其相关位置的变化,对于其余部分的结果可以使用map存储起来,写的时候对log2(x)没有处理好,导致精度误差,教训啊。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;

const int N = (1<<17)+5;
int n, m;
int seq[N];
int cpy[N];
map<pair<int,int>, int>mp;
map<pair<int,int>, int>::iterator it;

int cal(int p, int l, int r) {
    if (r - l == 1) return mp[make_pair(l, r)] = (seq[l] | seq[r]);
    int mid = (r+l) >> 1;
    int f = (int)floor(log((r-l+1)*1.0)/log(2.0)+0.5); // 直接除取整会带来精度丢失 
    if (p <= mid) {
        if (f & 1) {// or
            return (mp[make_pair(l, mid)] = cal(p, l, mid)) | mp[make_pair(mid+1, r)];
        } else {
            return (mp[make_pair(l, mid)] = cal(p, l, mid)) ^ mp[make_pair(mid+1, r)];
        }
    } else {
        if (f & 1) {
            return mp[make_pair(l, mid)] | (mp[make_pair(mid+1, r)] = cal(p, mid+1, r));
        } else {
            return mp[make_pair(l, mid)] ^ (mp[make_pair(mid+1, r)] = cal(p, mid+1, r));
        }
    }
}

int main() {
    scanf("%d %d", &n, &m);
    int LIM = 1 << n;
    for (int i = 1; i <= LIM; ++i) {
        scanf("%d", &seq[i]);
        cpy[i] = seq[i];
    }
    for (int i = 1, k = 0; i < LIM; i <<= 1, ++k) {
        for (int j = 1; j+i <= LIM; j += (i<<1)) {
            if (k & 1) { // xor 
                cpy[j] ^= cpy[j+i];
                mp[make_pair(j, j+(i<<1)-1)] = cpy[j];
            } else { // or
                cpy[j] |= cpy[j+i];
                mp[make_pair(j, j+(i<<1)-1)] = cpy[j];
            }
        }
    }
    int p, b;
    while (m--) {
        scanf("%d %d", &p, &b);
        seq[p] = b;
        printf("%d\n", cal(p, 1, LIM));
    }
    return 0;
}
View Code

 

posted @ 2013-08-27 13:53  沐阳  阅读(371)  评论(0编辑  收藏  举报