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\n", 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\n", 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\n", get(b) - get(a));
        }
    }
    return 0;
}
时间: 2024-08-26 17:55:14

Codeforces Round #263 (Div. 1) A B C的相关文章

贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

题目传送门 1 /* 2 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-1 13:20:01 7 File Name :A.cpp 8 *************************************************/ 9 10 #include <cstdio>

Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 题解: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. 代码: 31 int n; 32 int x[MAXN]; 33 VI G[MAXN]; 34 ll dp[MAX

Codeforces Round #263 (Div.1) B. Appleman and Tree

题目地址:http://codeforces.com/contest/461/problem/B 题目大意:给一棵树.每一个点为白色或黑色.切断一些边,使得每一个连通块有且仅有一个黑点,问划分方案数. 算法讨论:TreeDP. f[x][0..1]表示x所在连通块有0/1个黑点.设y为x的儿子,则DP方程为f[x][1]=f[x][1]*f[y][0]+f[x][1]*f[y][1]+f[x][0]*f[y][1],f[x][0]=f[x][0]*f[y][0]+f[x][0]*f[y][1].

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

题目地址:http://codeforces.com/contest/461/problem/C 题目大意:见原题. 算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记.注意细节. Code: #include <cstdio> #include <algorithm> #define N 100000 using namespace std; bool rev; int n,q,beg=1,end,typ,p,l,r,bit[N+10]; inline v

Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当然最后只是输出cost,就没必要建树什么的了.只要理解了huffman算法构造最优二叉树的思路,就按那么想就知道每个a[i]要加多少次了. 当然这道题没想到这些也可以找出规律的,就是一种贪心思想. #include<iostream> #include<cstdio> #include

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have

Codeforces Round #263 (Div. 1)

B.Appleman and Tree 题目大意.一棵树上的点有的是黑的有的是白的,然后他想断开一些边使得剩下的连通分量里每个连通分量有且仅有一个黑点,求方案数. dp[u][0]表示以u为根的子树且u所在的连通分量没有黑点的方案数. dp[u][1]表示以u为根的子树且u所在的连通分量有一个黑点的方案数. 更新节点u时,对于他的子树v,可以断开或不断开这条边<u,v>. #include <cstdio> #include <algorithm> #include &

Codeforces Round #263 (Div. 2) proA

题目: A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can

Codeforces Round #263 (Div. 2) proB

题目: B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Applem