Codeforces Round #442 (Div. 2)

A

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const LL INF = 1e18 + 10;
string s[5] = {"Danil", "Olya", "Slava", "Ann", "Nikita"};
int Solve(string a, string b) {
	int ans = 0;
	for(int i = 0; i < (int)a.size(); ++i) {
		if(a.substr(i, b.size()) == b)	ans++;
	}
	return ans;
}

int main(){
	ios::sync_with_stdio(false);
	string st;
	cin >> st;
	int cnt = 0;
	for(int i = 0; i < 5; ++i) {
		cnt += Solve(st, s[i]);
	}
	if(cnt == 1)	cout << "YES" << endl;
	else	cout << "NO" << endl;
	return 0;
}


B

题意:移除一些字符是的所剩下的字符最多并且保证第一段全是a,第二段全是b,第三段全是a

思路:考虑所有情况 全a 全b ab aba 
做一下前后缀求出这些情况的最大值即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const LL INF = 1e18 + 10;
int pre[qq][2], suf[qq][2];
string st;

int main(){
	ios::sync_with_stdio(false);
	cin >> st;
	for(int i = 0; i < (int)st.size(); ++i) {
		pre[i + 1][0] = pre[i][0];
		pre[i + 1][1] = pre[i][1];
		if(st[i] == 'a')	pre[i + 1][0]++;
		else	pre[i + 1][1]++;
	}
	for(int i = (int)st.size(); i > 0; --i) {
		suf[i][0] = suf[i + 1][0];
		suf[i][1] = suf[i + 1][1];
		if(st[i - 1] == 'a')	suf[i][0]++;
		else	suf[i][1]++;
	}
	int len = (int)st.size();
	int maxn = 0;
	for(int i = 0; i <= len; ++i) {
		maxn = max(maxn, pre[i][0] + suf[i + 1][1]);
		maxn = max(maxn, pre[i][1] + suf[i + 1][0]);
	}
	for(int i = 1; i <= len; ++i) {
		for(int j = i + 1; j <= len; ++j) {
			maxn = max(maxn, pre[i][0] + suf[j][0] + (pre[j][1] - pre[i - 1][1]));
		}
	}
	printf("%d\n", maxn);
	return 0;
}


C

题意:n个点排成一列每个点都有一个或多个tank,你每次可以在任意点投放一个炸弹,被炸第一次的tank会随机的向临近的点移动,当tank第二次被炸后那么tank将被摧毁,问最少多少个炸弹能保证所有tank都被摧毁

思路:发现可知奇数位的tank一定移向偶数位,那么我们可以先炸一次偶数位,这样所有tank全部位于奇数位,再炸一次奇数位,那么剩下的tank一定又位于偶数位,最后炸一次偶数位即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const LL INF = 1e18 + 10;

int main(){
	int n;	scanf("%d", &n);
	printf("%d\n", n / 2 * 2 + (n + 1) / 2);
	for(int i = 2; i <= n; i += 2) {
		printf("%d ", i);
	}
	for(int i = 1; i <= n; i += 2) {
		printf("%d ", i);
	}
	for(int i = 2; i <= n; i += 2) {
		printf("%d ", i);
	}
	puts("");
	return 0;
}



E

题意:两种操作,get x 获得根结点为x的子树的所有1的个数,pow x 翻转根结点为x的子树(1变0   0变1)

思路:一个很裸的dfs序转化为区间翻转与查询问题, 用线段数维护区间翻转即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define lson (rt << 1)
#define rson ((rt << 1) | 1)
#define mk make_pair
#define fi first
#define se second
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 8e5 + 10;
const LL INF = 1e18 + 10;
int sum[qq], lazy[qq], start[qq], ed[qq], num[qq];
int Time = 0;
vector<int> G[qq];
void Dfs(int u) {
	start[u] = ++Time;
	int sz = (int)G[u].size();
	for(int i = 0; i < sz; ++i) {
		Dfs(G[u][i]);
	}
	ed[u] = Time;
}
void PushDown(int l, int r, int rt) {
	int mid = (l + r) >> 1;
	if(lazy[rt]) {
		sum[lson] = (mid - l + 1) - sum[lson];
		sum[rson] = (r - (mid + 1) + 1) - sum[rson];
		lazy[lson] ^= 1;
		lazy[rson] ^= 1;
		lazy[rt] = 0;
	}
}
void PushUp(int rt) {
	sum[rt] = sum[lson] + sum[rson];
}
void Build(int l, int r, int rt) {
	lazy[rt] = 0;
	if(l == r) {
		sum[rt] = num[l];
		return;
	}
	int mid = (l + r) >> 1;
	Build(l, mid, lson);
	Build(mid + 1, r, rson);
	PushUp(rt);
}
void UpDate(int l, int r, int rt, int x, int y) {
	if(x <= l && r <= y) {
		sum[rt] = (r - l + 1) - sum[rt];
		lazy[rt] ^= 1;
		return;
	}
	PushDown(l, r, rt);
	int mid = (l + r) >> 1;
	if(mid >= x)	UpDate(l, mid, lson, x, y);
	if(mid < y)		UpDate(mid + 1, r, rson, x, y);
	PushUp(rt);
}
int Query(int l, int r, int rt, int x, int y) {
	if(x <= l && r <= y) {
		return sum[rt];
	}
	PushDown(l, r, rt);
	int mid = (l + r) >> 1;
	int ans = 0;
	if(mid >= x)	ans += Query(l, mid, lson, x, y);
	if(mid < y)		ans += Query(mid + 1, r, rson, x, y);
	return ans;
}

int main(){
	int n;	scanf("%d", &n);
	for(int x, i = 2; i <= n; ++i) {
		scanf("%d", &x);
		G[x].pb(i);
	}
	Dfs(1);
	for(int x, i = 1; i <= n; ++i) {
		scanf("%d", &x);
		num[start[i]] = x;
	}
	Build(1, n, 1);
	int m;	scanf("%d", &m);
	while(m--) {
		char op[10];
		int x;
		scanf("%s%d", op, &x);
//		printf("%d %d\n", start[x], ed[x]);
		if(op[0] == 'g') {
			printf("%d\n", Query(1, n, 1, start[x], ed[x]));
		} else {
			UpDate(1, n, 1, start[x], ed[x]);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值