zoukankan      html  css  js  c++  java
  • 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 == '
    ') {
                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
    ", 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
    ", cal(p, 1, LIM));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    vsphere client cd/dvd 驱动器1 正在连接
    使用SecureCRT上传和下载文件
    java android 将 List中元素互换位置
    java中Math常用方法
    关于View转化成bitmap保存成图片
    Java中Math类的几个四舍五入方法的区别
    Math.round(),Math.ceil(),Math.floor()的区别
    动态的添加ImageView到LinearLayout中并居中显示
    android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
    Android 代码设置RelativeLayout元素居中
  • 原文地址:https://www.cnblogs.com/Lyush/p/3284759.html
Copyright © 2011-2022 走看看