zoukankan      html  css  js  c++  java
  • Codeforces Round #263 (Div. 1) A B C

    Codeforces Round #263 (Div. 1)

    A:贪心。排个序,然后从后往前扫一遍,计算后缀和。之后在从左往右扫一遍计算答案

    B:树形DP。0表示没有1,1表示有1,0遇到0必定合并。0遇到1也必定合并,1遇到0必定合并。1遇到1,必定切断,依照这样去转移就可以

    C:树状数组,再利用启示式合并,开一个l,r记录当前被子左右下标。和一个flip表示是否翻转

    代码:

    A:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    typedef long long ll;
    const int N = 300005;
    
    int n;
    ll a[N], sum[N];
    
    int main() {
    	scanf("%d", &n);ll ans = 0;
     	for (int i = 0; i < n; i++) {	
        	scanf("%lld", &a[i]);	
         ans += a[i];
    	}
    	sort(a, a + n);
    	for (int i = n - 1; i >= 0; i--)
    		sum[i] = a[i] + sum[i + 1];
    		
    	for (int i = 0; i < n - 1; i++) {
    		ans += sum[i];
    	}
    	printf("%lld
    ", ans);
    	//system("pause");
    	return 0;
    }

    B:

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <cstdlib>
    using namespace std;
    
    const int N = 100005;
    typedef long long ll;
    const ll MOD = 1000000007;
    int n, node[N];
    vector<int> g[N];
    ll dp[N][2];
    
    ll pow_mod(ll x, ll k) {
        ll ans = 1; 
        while (k) {
            if (k&1) ans = ans * x % MOD;
            x = x * x % MOD;
            k >>= 1;
        }
        return ans;
    }
    
    ll inv(ll x) {
        return pow_mod(x, MOD - 2);
    }
    
    void init() {
        scanf("%d", &n);
        int u;
        for (int i = 1; i < n; i++) {
            scanf("%d", &u);
            g[u].push_back(i);
        }
        for (int i = 0; i < n; i++)
            scanf("%d", &node[i]);
    }
    
    void dfs(int u) {
        if (g[u].size() == 0) {
            dp[u][node[u]] = 1;
            return;
        }
        for (int i = 0; i < g[u].size(); i++)
            dfs(g[u][i]);
        dp[u][0] = dp[u][1] = 1;
        if (node[u]) {
            dp[u][0] = 0;
            for (int i = 0; i < g[u].size(); i++) {
                int v = g[u][i];
                dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
            }
        }
        else {
            ll cnt = 0;
            ll mul = 1;
            for (int i = 0; i < g[u].size(); i++) {
                int v = g[u][i];
                dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
                mul = mul * (dp[v][0] + dp[v][1]) % MOD;
            }
            dp[u][1] = 0;
            for (int i = 0; i < g[u].size(); i++){ 
                int v = g[u][i];
                dp[u][1] = (dp[u][1] + mul * inv((dp[v][0] + dp[v][1]) % MOD) % MOD * dp[v][1]) % MOD;
            }
        }
    }
    
    int main() {
        init();
        dfs(0);
        printf("%lld
    ", dp[0][1] % MOD);
        //system("pause");
        return 0;
    }

    C:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define lowbit(x) (x&(-x))
    
    const int N = 100005;
    
    int n, q, bit[N];
    
    void add(int x, int v) {
        while (x < N) {
            bit[x] += v;
            x += lowbit(x);
        }
    }
    
    int get(int x) {
        int ans = 0;
        while (x) {
            ans += bit[x];
            x -= lowbit(x);
        }
        return ans;
    }
    
    int main() {
        scanf("%d%d", &n, &q);
        for (int i = 1; i <= n; i++)
            add(i, 1);
        int tp, a, b;
        int l = 1, r = n, flip = 0;
        while (q--) {
            scanf("%d%d", &tp, &a);
            if (tp == 1) {
                int tl = l, tr = r;
                if (a <= (r - l + 1) / 2) {
                    if (flip) {
                        for (int i = a; i >= 1; i--) {
                            add(r - 2 * i + 1, get(tr - a + i) - get(tr - a + i - 1));
                            r--;
                        }
                    }
                    else {
                        for (int i = a; i >= 1; i--) {
                            add(l + 2 * i - 1, get(tl + a - i) - get(tl + a - i - 1));
                            l++;
                        }
                    }
                } else {
                    a = r - a - l + 1;
                    if (!flip) {
                        for (int i = a; i >= 1; i--) {
                            add(r - 2 * i + 1, get(tr - a + i) - get(tr - a + i - 1));
                            r--;
                        }
                    }
                    else {
                        for (int i = a; i >= 1; i--) {
                            add(l + 2 * i - 1, get(tl + a - i) - get(tl + a - i - 1));
                            l++;
                        }
                    }
                    flip ^= 1;
                }
            }
            else {
                scanf("%d", &b);
                if (flip) {
                    a = r - a;
                    b = r - b;
                    swap(a, b);
                } else {
                    a += l - 1; 
                    b += l - 1;
                }
                printf("%d
    ", get(b) - get(a));
            }
        }
        return 0;
    }


  • 相关阅读:
    汤姆大叔 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解 后六道题答案
    canvas绘制自定义的曲线,以椭圆为例,通俗易懂,童叟无欺
    nodejs 平台的 webscoket 的实现
    javascript 控制台输出 图片 console.log 真强大 真佩服你们的创造力
    ichartjs 使用BUG,assign_scale:true 解决
    timequest静态时序分析学习笔记之工具使用
    timequest静态时序分析学习笔记之命令约束
    timequest静态时序分析学习笔记之基本概念
    浅谈“意识”的物质性
    构建异步处理网络服务器
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6961344.html
Copyright © 2011-2022 走看看