Codeforces Round#429(Div.2)

A. Generous Kefa

如果有字母的个数大于k则NO

#include<bits/stdc++.h>
using namespace std;
int arr[28],n,k;
string str;
int main(){
    cin>>n>>k;
    cin>>str;
    for(int i = 0;i<str.length();i++){
        arr[(int)(str[i]-‘a‘)]++;
    }
    for(int i = 0;i<26;i++){
        if(arr[i] > k )return 0*printf("NO");
    }
    printf("YES");
    return 0;
} 

B. Godsend

如果和为奇数first直接赢,如果没有奇数second直接赢,有的话拿走奇数个奇数

数组中有奇数个奇数,second无论怎么拿都是first赢

#include<bits/stdc++.h>
using namespace std;
int arr[1000100],n,sum,even,odd;
int main(){
    cin>>n;
    for(int i = 0;i<n;i++){
        scanf("%d",&arr[i]);
        if(arr[i]%2) odd++;
        else even++;
        sum+=arr[i];
    }
    if(sum%2 || odd) return 0*printf("First");
    else return 0*printf("Second");
    return 0;
}

C. Leha and Function

数论水平太差……总之b中最小数配a中最大数找规律过的,代码就不贴了

D. Leha and another game about graph

先判不行的情况,如果有奇数个1,没有-1一定不行

从任一节点dfs,看别人的代码在dfs完所有子树后都有这样的操作 if(d[to] == 1) d[pos] ^= 1

想了好久,模拟了一下,也就是从靠近树叶的d为1的节点回溯,如果父节点为0改为1,为-1则直接找到停下来

另外还要用并查集去环

#include<bits/stdc++.h>
using namespace std;
#define MAXN    300005
struct edge {int dest, num; };
int n, m, f[MAXN];
int cnt, ans[MAXN];
int root, d[MAXN];
vector <edge> a[MAXN];
void work(int pos, int fa) {
    for (unsigned i = 0; i < a[pos].size(); i++) {
        if (a[pos][i].dest == fa) continue;
        work(a[pos][i].dest, pos);
        if (d[a[pos][i].dest] == 1) {
            ans[++cnt] = a[pos][i].num;
            if (d[pos] != -1) d[pos] ^= 1;
        }
    }
}
int F(int x) {
    if (f[x] == x) return x;
    else return f[x] = F(f[x]);
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &d[i]);
        f[i] = i;
    }
    for (int i = 1; i <= m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        if (F(x) == F(y)) continue;
        f[F(x)] = F(y);
        a[x].push_back((edge) {y, i});
        a[y].push_back((edge) {x, i});
    }
    root = 1;
    for (int i = 1; i <= n; i++)
        if (d[i] == -1) {
            root = i;
            break;
        }
    work(root, 0);
    if (d[root] == 1) printf("-1\n");
    else {
        printf("%d\n", cnt);
        for (int i = 1; i <= cnt; i++)
            printf("%d ", ans[i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-12-29 11:30:03

Codeforces Round#429(Div.2)的相关文章

CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上差分 ] | Codeforces Round #429(Div 1) 题意: 选择一个边集合,满足某些点度数的奇偶性 分析: 将d = 1的点连成一颗树,不在树上的点都不连边. 可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求 即整棵树上至多只有1个点不满足自身要求,

CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #include <bits/stdc++.h> using namespace std; const int N = 2e5+5; int a[N], b[N], c[N], n; int aa[N], bb[N]; bool cmp1(int x, int y) { return a[x] > a[y

【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph

题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1,那么必有解.否则如果初始不合法的点数为偶数,那么必有解,否则无解.因为删一条边,要么使图中不合法的点数+2,要么不变,要么-2. 如果有解,构造图的任意一个生成树,如果有-1,就让-1为根,否则任意结点为根.然后从叶子向根定每个点的入度数,由于自底向上,一个结点的儿子边都被处理完后,只需要决定父边

【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))

[题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最大. [算法]数学结论+数学期望+排序 [题解]很无奈,这题放在div2 C,难以推导的期望公式,广为人知的结论,容易观察样例得出的做法,都体现了这道题的不合理性. F(n,k)=(n+1)/(k+1) 公式推导可能触及我的知识盲区了QAQ 得到公式后,显然要求k尽可能小,n尽可能大,经验告诉我们随着两数

Codeforces Round #429 (Div. 2) 841A. Generous Kefa(签到题)

A. Generous Kefa One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si - lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give o

Codeforces Round #429 (Div. 2)ABC

A: 题意:n个东西,k个朋友,全部给朋友,每个朋友不可以拿同样的,问是否可行 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 map<char ,int >ma; 5 int main(){ 6 int n,k; 7 cin>>n>>k; 8 string s; 9 cin>>s; 10 for(int i=0;i<n;i++){ 11 ma[s[i]]++; 12 if(ma[s

Codeforces Round #429 (Div. 2) 841B Godsend(签到题)

B. Godsend Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿