Codeforces Round #302 (Div. 2) -- (A,B,C)

题目传送:Codeforces Round #302 (Div. 2)

A. Set of Strings

思路:注意开头字母都不相同

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

char s[105];

int flag[26];

int f[26];

int main() {
	int k;
	scanf("%d", &k);
	scanf("%s", s);
	int len = strlen(s);

	memset(flag, 0, sizeof(flag));
	for(int i = 0; i < len; i ++) {
		flag[s[i] - 'a'] = 1;
	}
	int cnt = 0;
	for(int i = 0; i < 26; i ++) {
		cnt += flag[i];
	}
	if(cnt < k) {
		cout << "NO" << endl;
	}
	else {
		memset(f, 0, sizeof(f));

		char cur = s[0];
		cout << "YES" << endl;
		printf("%c", s[0]);
		f[s[0] - 'a'] = 1;
		int tt = 1;
		for(int i = 1; i < len; i ++) {
			if(s[i] != cur && tt != k && f[s[i] - 'a'] == 0) {
				printf("\n%c", s[i]);
				f[s[i] - 'a'] = 1;
				tt ++;
				cur = s[i];
			}
			else printf("%c", s[i]);
		}
	}
	return 0;
}

B. Sea and Islands

思路:构造,给你一个n*n的地图,看地图上能不能有k个岛,岛是由一个联通块组成的(相邻的即为一个联通块)

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;

	int sum = n & 1 ? (n * n + 1) / 2 : n * n / 2;//最多可以有这么多个岛 

	if(k > sum) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
		int t = 0;
		for(int i = 1; i <= n; i++, printf("\n")) {
			for(int j = 1; j <= n; j ++) {
				if(i & 1 && t != k) {
					if(j & 1) {
						printf("L");
						t ++;
					}
					else {
						printf("S");
					}
				}
				else if(!(i & 1) && t != k){
					if(j & 1) {
						printf("S");
					}
					else {
						printf("L");
						t ++;
					}
				}
				else if(t == k) {
					printf("S");
				}
			}
		}
	}
	return 0;
}

C. Writing Code

思路:简单DP,完全背包

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

LL dp[505][505];
LL a[505];

int main() {
	int n, m, b, mod;
	scanf("%d %d %d %d", &n, &m, &b, &mod);
	for(int i = 1; i <= n; i ++) {
		scanf("%d", &a[i]);
	}
	dp[0][0] = 1;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) {
			for(int k = 0; k <= b; k ++) {
				if(k >= a[i]) dp[j][k] += dp[j-1][k - a[i]];
				dp[j][k] %= mod;
			}
		}
	}
	LL ans = 0;
	for(int i = 0; i <= b; i ++) {
		ans += dp[m][i];
		ans %= mod;
	}
	cout << ans << endl;
	return 0;
}
时间: 2024-10-04 14:39:25

Codeforces Round #302 (Div. 2) -- (A,B,C)的相关文章

Codeforces Round #589 (Div. 2) (e、f没写)

https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每一位的数字不相同,找出满足要求的最小的那个数输出,没有找到就输出-1: 1 #include<bits/stdc++.h> 2 using namespace std; 3 bool check(int n){ 4 int vis[15]={0}; 5 while(n){ 6 if(!vis[n%

Codeforces Round #577 (Div. 2) (A、B、C)

A.Important Exam 简单贪心即可 1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector&

Codeforces Round #575 (Div. 3) (A. Three Piles of Candies)(数学)

A. Three Piles of Candies time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as poss

Codeforces Round #302 (Div. 2) A B C

Codeforces Round #302 (Div. 2) A. Set of Strings 字符串 q 被称为 "beautiful" 当且仅当 q 可以被拆分成 k 个子串 (s1, s2, s3, ... , sk) 并且任意两个字串满足首字母不一样. 直接模拟,对 q 的每个字符进行判断,如果该字符在之前没有出现过,那么从它开始就可以组成一个新的字符串,并且计数,如果到了k 了则把之后的都归为一个字符串. #include <cstring> #include

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #259 (Div. 2) (序列)

题目链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

Codeforces Round #253 (Div. 1) (A, B, C)

Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后现在要提示一些牌的信息,要求提示最少,使得所有牌可以被分辨出来. 思路:一共2^10种情况,直接暴力枚举,然后对于每种情况去判断,判断的时候只要两两张牌枚举出来判断即可.不得不说CF机子真心强大,2秒限制还是能跑10^8 B:给定n个发生概率,求选出其中一些事件,使得正好有一件发生的概率最大. 思路:贪心,从大到小排序概率,然后一个个概率进来判断有没有更大,有就加入该事件,没有就跳过 C:给定n个数字,要

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con

完全背包 Codeforces Round #302 (Div. 2) C Writing Code

题目传送门 1 /* 2 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 3 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][0] = 1 表示不选择人的时候所有的bug的种类犯错误都只有一种 4 dp[i][j][k] += dp[i%2][j-1][k-a[i]]: 5 错误示范:dp[i][j][k] += dp[i-1][j-l][k-l*a[i]]; 其实要从上一行的状态推出,即少一行 6 内存限制,