SCAU 周训五

A - A CodeForces - 632B

1.题目描述:
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.

The way to split up game pieces is split into several steps:

First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
Alice will get all the pieces marked A and Bob will get all the pieces marked B.
The strength of a player is then the sum of strengths of the pieces in the group.

Given Alice’s initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.

Input
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.

The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.

The third line contains n characters A or B — the assignment of teams after the first step (after Alice’s step).

Output
Print the only integer a — the maximum strength Bob can achieve.

Examples
Input
5
1 2 3 4 5
ABABA
Output
11
Input
5
1 2 3 4 5
AAAAA
Output
15
Input
1
1
B
Output
1
Note
In the first sample Bob should flip the suffix of length one.

In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

In the third sample Bob should do nothing.
2.题意:略。
3.思路:略。
4.代码:看前几周的测试,里面有,原题。

B - B CodeForces - 627A

1.题目描述:
Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?

Input
The first line of the input contains two integers s and x (2 ≤ s ≤ 1012, 0 ≤ x ≤ 1012), the sum and bitwise xor of the pair of positive integers, respectively.

Output
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.

Examples
Input
9 5
Output
4
Input
3 3
Output
2
Input
5 2
Output
0
Note
In the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2).

In the second sample, the only solutions are (1, 2) and (2, 1).

2.题意:

3.思路:

4.代码:

C - C CodeForces - 623A

1.题目描述:
One day student Vasya was sitting on a lecture and mentioned a string s1s2… sn, consisting of letters “a”, “b” and “c” that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties:

G has exactly n vertices, numbered from 1 to n.
For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs “a”-“b” and “b”-“c” are neighbouring, while letters “a”-“c” are not.
Vasya painted the resulting graph near the string and then erased the string. Next day Vasya’s friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya’s adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.

Input
The first line of the input contains two integers n and m — the number of vertices and edges in the graph found by Petya, respectively.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.

Output
In the first line print “Yes” (without the quotes), if the string s Petya is interested in really exists and “No” (without the quotes) otherwise.

If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters “a”, “b” and “c” only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.

Examples
Input
2 1
1 2
Output
Yes
aa
Input
4 3
1 2
1 3
1 4
Output
No
Note
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings “aa”, “ab”, “ba”, “bb”, “bc”, “cb”, “cc” meets the graph’s conditions.

In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.

2.题意:

3.思路:

4.代码:

D - D CodeForces - 604B

1.题目描述:
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won’t place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.

Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

Input
The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.

The next line contains n space-separated integers s1, s2, …, sn (1 ≤ s1 ≤ s2 ≤ … ≤ sn ≤ 1 000 000), the sizes of Kevin’s cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

Output
Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

Examples
Input
2 1
2 5
Output
7
Input
4 3
2 3 5 9
Output
9
Input
3 2
3 5 7
Output
8
Note
In the first sample, Kevin must pack his two cowbells into the same box.

In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.

In the third sample, the optimal solution is {3, 5} and {7}.

2.题意:
一个盒子里可以放1个或两个数,给出k个盒子,n个数,数字按照不降序排列,求盒子最小的容量(盒子容量大于等于盒子内数的和)。

3.思路:
贪心。看到排好序,看到最小值,贪心直接上啦。由于有k个盒子(k<=n),那么剩下的n-k个是必定要匹配的,我们只需要在前n-k个和他们后面的数比较组合就好啦。具体来说就是 0和 2(n-k)-1匹配,1和2(n-k)-2匹配(匹配这里就是要求前后归于平均,可能下标会有点问题,欢迎大佬指正。),我们用匹配值和最后一个最大值进行比较取最大就行了。

4.代码:

//D
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#define FAST ios::sync_with_stdio(false)
using namespace std;
const int MAX_N=100000+5;
int num[MAX_N];
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	int n,k,m,s;
	cin>>n>>k;m=n-k;
	for(int i=0;i<n;++i) cin>>num[i];
	s=num[n-1];
	for(int i=0;i<m;++i)
		s=max(s,num[i]+num[2*m-i-1]);
	cout<<s<<endl;
	return 0;
}

E - E HDU - 2098

1.题目描述:
把一个偶数拆成两个不同素数的和,有几种拆法呢?
Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
Sample Input
30
26
0
Sample Output
3
2

2.题意:
哥德巴赫猜想

3.思路:
暴力匹配。

4.代码:

//E
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cmath>
#define FAST ios::sync_with_stdio(false)
const int MAX_N=10000+5;
bool prim[MAX_N];
using namespace std;
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	for(int i=2;i<=MAX_N;++i) prim[i]=1;
	for(int i=2;i<=MAX_N;++i)
		for(int j=2*i;j<=MAX_N;j+=i)
			prim[j]=0;
	int n;
	while(scanf("%d",&n)&&n)
	{
		int cnt=0;
		for(int i=3;i<n/2;++i)
			if(prim[i]&&prim[n-i])
				cnt++;
		cout<<cnt<<endl;
	}
	return 0;
}

F - F POJ - 3259

1.题目描述:
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1…N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself 😃 .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2… M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2… M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1… F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

2.题意:
FJ有n个农场,农场里有路可以到达其他农场,农场之间还有虫洞!可以穿越时空,问FJ能从起点回到过去的起点吗?

3.思路:
最短路径问题。
解法一:暴力Flody。因为数据量小,所以直接确认自己到自己的边小于零马上返回可以。(卡时间的暴力解法)。

4.代码:

//F
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#define FAST ios::sync_with_stdio(false)
using namespace std;
const int MAX_N=500+5;
const int INF=0x3f3f3f3f;
int G[MAX_N][MAX_N];
int n;
inline bool Floyd()
{
	for(int k=1;k<=n;++k)
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=n;++j)
				if(G[i][j]>G[i][k]+G[k][j])
					G[i][j]=G[i][k]+G[k][j];
			if(G[i][i]<0) return true;
		}
	return false;
}
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	long long Case;
	scanf("%lld",&Case);
	while(Case--)
	{
		int m,w;
		scanf("%d%d%d",&n,&m,&w);
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				G[i][j]=i==j?0:INF;
		int s,e,t;
		while(m--)
		{
			scanf("%d%d%d",&s,&e,&t);
			if(t<G[s][e])
			G[s][e]=G[e][s]=t;
		}
		while(w--)
		{
			scanf("%d%d%d",&s,&e,&t);
			if(-t<G[s][e])
			G[s][e]=-t;
		}
		if(Floyd()) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

G - G CodeForces - 474D

1.题目描述:
We saw the little game Marmot made for Mole’s lunch. Now it’s Marmot’s dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

Input
Input contains several test cases.

The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.

The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.

Output
Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).

Examples
Input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
For K = 2 and length 1 Marmot can eat ®.
For K = 2 and length 2 Marmot can eat (RR) and (WW).
For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR).
For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can’t eat (WWWR).

2.题意:
好像是鼹鼠吧(还是啥总之是动物),他们呢只吃红(花)发发和白(花)发发,然后呢,他们对于白花有特别的要求,生活要有仪式感,所以呢,他们只喜欢吃一组数量为k的白花。给出一个区间[a,b],问在这区间中有多少种配餐方法?

3.思路:
这是一道动态规划的题目,我们用dp[i],表示数量为i的配餐方法,那么我有两种选择——(1):吃一朵红花;(2)吃一组白花。所以我们的状态转移方程就是:dp[i]=dp[i-1]+dp[i-k];然后我们再求dp数组的前缀和来应付查询就ok啦。

4.代码:

//G
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#define FAST ios::sync_with_stdio(false)
using namespace std;
const int mod=1000000007;
const int MAX_N=100005;
int dp[MAX_N],sum[MAX_N];
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	long long t,k;
	scanf("%lld %lld",&t,&k);
	for(int i=0;i<k;++i) dp[i]=1;
	for(int i=k;i<=MAX_N;++i) dp[i]=(dp[i-1]+dp[i-k])%mod;
	for(int i=1;i<=MAX_N;++i) sum[i]=(sum[i-1]+dp[i])%mod;
	bool flag=1;
	while(t--)
	{
		long long a,b;
		scanf("%lld %lld",&a,&b);
		long long cnt=(sum[b]-sum[a-1]+mod)%mod;
		if(flag) {flag=0;printf("%lld",cnt);}
		else printf("\n%lld",cnt);
	}
	return 0;
}

H - H HDU - 1166

1.题目描述:
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59

2.题意:
略。

3.思路:
这个太典型了,线段树或者树状数组都可以。
解法一:线段树解法。动态查询区间和、修改区间值的利器。这个碍于篇幅不赘述,会补上完整的线段树的。但是这里可以说一下思路。线段树就是将一个区间一分为二,然后继续细分直到每个都只有1的叶子节点,然后每一段都做递归处理。

4.代码:

//H
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#define FAST ios::sync_with_stdio(false)
using namespace std;
const int MAX_N=50000+5;
int num[MAX_N];
struct node
{
	int l,r,sum,lazy;
	node(){l=r=sum=lazy=0;}
}a[MAX_N<<2];
inline void update(int k){a[k].sum=a[k<<1].sum+a[k<<1|1].sum;}
void build(int k,int l,int r)
{
	a[k].l=l;a[k].r=r;
	if(l==r)
	{
		a[k].sum=num[l];
		return;
	}
	int m=(l+r)>>1;
	build(k<<1,l,m);
	build(k<<1|1,m+1,r);
	update(k);
}
void change(int k,int x ,int y)
{
	if(a[k].l==a[k].r){a[k].sum+=y;return;}
	int m=(a[k].l+a[k].r)>>1;
	if(x<=m) change(k<<1,x,y);
	else change(k<<1|1,x,y);
	update(k);
}
void pushdown(int k)
{
	if(a[k].l==a[k].r){a[k].lazy=0;return;}
	a[k<<1].sum+=(a[k<<1].r-a[k<<1].l+1)*a[k].lazy;
	a[k<<1|1].sum+=(a[k<<1|1].r-a[k<<1|1].l+1)*a[k].lazy;
	a[k<<1].lazy+=a[k].lazy;
	a[k<<1|1].lazy+=a[k].lazy;
	a[k].lazy=0;
}
int query(int k,int l,int r)
{
	if(a[k].lazy) pushdown(k);
	if(a[k].l==l&&a[k].r==r) return a[k].sum;
	int m=(a[k].l+a[k].r)>>1;
	if(r<=m) return query(k<<1,l,r);
	if(l>m) return query(k<<1|1,l,r);
	return query(k<<1,l,m)+query(k<<1|1,m+1,r);
}
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	long long Case;
	scanf("%lld",&Case);
	for(int I=1;I<=Case;++I)
	{
		printf("Case %d:\n",I);
		int N;
		cin>>N;
		for(int i=1;i<=N;++i) scanf("%d",&num[i]);
		string s;
		build(1,1,N);
		while(cin>>s)
		{
			int l,r,i,k;
			if(s=="End") break;
			else if(s=="Query")
			{
				scanf("%d %d",&l,&r);
				cout<<query(1,l,r)<<endl;
			}
			else if(s=="Add")
			{
				scanf("%d %d",&i,&k);
				change(1,i,k);
			}
			else
			{
				scanf("%d %d",&i,&k);
				change(1,i,-k);
			}
		}
	}
	return 0;
}

//我会补完的QAQ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值