Codeforces Round #469 (Div. 2)

开学啦!

A. Left-handers, Right-handers and Ambidexters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and a ambidexters, who can play with left or right hand.

The coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands.

Ambidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand.

Please find the maximum possible size of the team, where equal number of players use their left and right hands, respectively.

Input

The only line contains three integers lr and a (0 ≤ l, r, a ≤ 100) — the number of left-handers, the number of right-handers and the number of ambidexters at the training.

Output

Print a single even integer — the maximum number of players in the team. It is possible that the team can only have zero number of players.

Examples
input
Copy
1 4 2
output
6
input
Copy
5 5 5
output
14
input
Copy
0 2 0
output
0
Note

In the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team.

In the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand.



贪心策略:先安排只能用左右手的人,再安排剩下的人和全能的人再一起,最后考虑剩下的全能人。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);

int main() {
	int l,r,a,ans;
	cin >> l >> r >> a;
	ans=min(l,r)*2;
	r-=ans/2;l-=ans/2;
	if (l>0) {
		int q=min(l,a);
		ans+=q*2;
		a-=q;l-=q;
	} else {
		int q=min(r,a);
		ans+=q*2;
		a-=q;r-=q;
	}
	if (a>0) {
		ans+=a/2*2;
	}
	cout << ans;
	return 0;
}
B. Intercepted Message
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.

Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.

Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.

You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.

The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.

It is guaranteed that x1 + ... + xn = y1 + ... + ymAlso, it is guaranteed that x1 + ... + xn ≤ 106.

Output

Print the maximum number of files the intercepted array could consist of.

Examples
input
Copy
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
output
3
input
Copy
3 3
1 10 100
1 100 10
output
2
input
Copy
1 4
4
1 1 1 1
output
1
Note

In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 715 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.

In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 110 and 100.

In the third example the only possibility is that the archive contains a single file of size 4.



贪心,按顺序用two-pointer在两组分组里扫,尽量让当前分组的总和相等。


话说分组这个词前几天计算机网络课上刚刚听过= =


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
ll a[maxn],b[maxn];

int main() {
	int n,m,i,j;
	ll sa,sb,sq;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) {
		scanf("%I64d",&a[i]);
	}
	int ans=0;
	sb=0;
	sa=a[1];sq=1;
	for (i=1;i<=m;i++) {
		scanf("%I64d",&b[i]);
		sb+=b[i];
		while (sa<sb&&sq<n) {
			sq++;
			sa+=a[sq];
		}
		if (sa==sb) {
			ans++;
			sb=0;sq++;sa=a[sq];
		}
	}
	printf("%d\n",ans);
	return 0;
}

C. Zebras
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 001001010 are zebras, while sequences 101100101 are not.

Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.

Input

In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.

Output

If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples
input
Copy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
input
Copy
111
output
-1


题目要求将一个01串分成若干子序列,使得每个子序列都是010101...0的形式。


首先可以观察到,0再多也没有关系,因为0可以单独组成一个子序列。所以,我们只要关注序列当中的1,为它们配上0.

而当一个0卡在两个1中间时,显然这时它做出的贡献最多,因为这时它一举解决了两个1的配对问题。如果不能如此,那么把0放在1后面也是可以解决一个1的配对问题的。实际上因为我们从左到右按顺序扫01串的时候,不知道某个0后面是否有1,所以上述两种情况可以归为一类。

那么可以把所有子序列的结尾位置放进两个序列,一个以0结尾,一个以1结尾。碰到1时,随便找一个0结尾的子序列配对;碰到0时,先看它是否能与之前的1配对,不能就把它单独提出来作为一个序列的开头,以增加未来能够与1配对的子序列数量。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=200005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int pre[maxn];
char s[maxn];
vector<int> v[maxn];

int main() {
	int len,i,j;
	scanf("%s",s+1);
	len=strlen(s+1);
	queue<int> a,b;
	for (i=1;i<=len;i++) {
		if (s[i]=='1') {
			if (a.empty()) {
				printf("-1");
				return 0;
			}
			pre[i]=a.front();a.pop();
			b.push(i);
		} else {
			if (!b.empty()) {
				pre[i]=b.front();b.pop();
			} else pre[i]=-1;
			a.push(i);
		}
	}
	if (!b.empty()) {
		printf("-1");
		return 0;
	}
	int ans=a.size();
	printf("%d\n",ans);
	stack<int> st;
	while (!a.empty()) {
		int now=a.front();
		a.pop();
		int cnt=0;
		st.push(now);cnt++;
		while (pre[now]!=-1) {
			now=pre[now];
			st.push(now);
			cnt++;
		}
		printf("%d ",cnt);
		while (!st.empty()) {
			printf("%d ",st.top());
			st.pop();
		}
		printf("\n");
	}
	return 0;
}
D. A Leapfrog in the Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.

Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:

You have to write a program that allows you to determine what number will be in the cell with index x(1 ≤ x ≤ n) after Dima's algorithm finishes.

Input

The first line contains two integers n and q (1 ≤ n ≤ 10181 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.

Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.

Output

For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.

Examples
input
Copy
4 3
2
3
4
output
3
2
4
input
Copy
13 4
10
5
4
8
output
13
3
8
9
Note

The first example is shown in the picture.

In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].


这题看到n这么大,就知道不能直接求。而q有2e5,碰到这个数量级,很显然是在提示你用某个对数级别或以下的方法解决问题。而谈到对数,以2为底又是最常见的。

嗯,所以就是个递归或者递推了!

(以上是比赛时的遐想)

递推的话,看看对于相邻的n,序列有什么规律。

n=3              1 3 2

n=4           1 3 2 4

n=5        1 5 2 4 3

n=6     1 4 2 6 3 5

.......

发现规律了吗?

对于每一个n,奇项的规律很显然。至于偶数项,我们把它们单独提出来:

n=3               3                        n=1          1

n=4            3 4                       n=2        1 2

n=5                                                2 1

n=6                              n=3      1 3 2

n=7         6 5 7                                  2 1 3

.......

n=14       8 13 9 12 10 14 11           n=7     1 6 2 5 3 7 4

n=15       12 9 14 10 13 11 15                    4 1 6 2 5 3 7

规律很显然了,偶数项对应的大小关系和n/2之后的大小关系相同。

那么我们只要不断将数列长度折半,直到要查询的下标为奇数就可以了。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);

int main() {
	ll n,i,ans,q,x;
	scanf("%I64d%I64d",&n,&q);
	for (i=1;i<=q;i++) {
		scanf("%I64d",&x);
		ll nq=n;
		ans=0;
		while (nq>0) {
			if (x%2==1) {
				ans+=(x+1)/2;
				break;
			}
			ans+=(nq+1)/2;
			if (nq%2==0) {
				x/=2;
			} else {
				if (x==2) {
					x=nq/2;
				} else {
					x=x/2-1;
				}
			}
			nq/=2;
		}
		printf("%I64d\n",ans);
	}
	return 0;
}
E. Data Center Maintenance
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).

Main feature of services offered by BigData Inc. is the access availability guarantee even under the circumstances of any data center having an outage. Such a guarantee is ensured by using the two-way replication. Two-way replication is such an approach for data storage that any piece of data is represented by two identical copies that are stored in two different data centers.

For each of m company clients, let us denote indices of two different data centers storing this client data as ci, 1 and ci, 2.

In order to keep data centers operational and safe, the software running on data center computers is being updated regularly. Release cycle of BigData Inc. is one day meaning that the new version of software is being deployed to the data center computers each day.

Data center software update is a non-trivial long process, that is why there is a special hour-long time frame that is dedicated for data center maintenance. During the maintenance period, data center computers are installing software updates, and thus they may be unavailable. Consider the day to be exactly h hours long. For each data center there is an integer uj (0 ≤ uj ≤ h - 1) defining the index of an hour of day, such that during this hour data center j is unavailable due to maintenance.

Summing up everything above, the condition uci, 1 ≠ uci, 2 should hold for each client, or otherwise his data may be unaccessible while data centers that store it are under maintenance.

Due to occasional timezone change in different cities all over the world, the maintenance time in some of the data centers may change by one hour sometimes. Company should be prepared for such situation, that is why they decided to conduct an experiment, choosing some non-empty subset of data centers, and shifting the maintenance time for them by an hour later (i.e. if uj = h - 1, then the new maintenance hour would become 0, otherwise it would become uj + 1). Nonetheless, such an experiment should not break the accessibility guarantees, meaning that data of any client should be still available during any hour of a day after the data center maintenance times are changed.

Such an experiment would provide useful insights, but changing update time is quite an expensive procedure, that is why the company asked you to find out the minimum number of data centers that have to be included in an experiment in order to keep the data accessibility guarantees.

Input

The first line of input contains three integers nm and h (2 ≤ n ≤ 100 0001 ≤ m ≤ 100 0002 ≤ h ≤ 100 000), the number of company data centers, number of clients and the day length of day measured in hours.

The second line of input contains n integers u1, u2, ..., un (0 ≤ uj < h), j-th of these numbers is an index of a maintenance hour for data center j.

Each of the next m lines contains two integers ci, 1 and ci, 2 (1 ≤ ci, 1, ci, 2 ≤ nci, 1 ≠ ci, 2), defining the data center indices containing the data of client i.

It is guaranteed that the given maintenance schedule allows each client to access at least one copy of his data at any moment of day.

Output

In the first line print the minimum possible number of data centers k (1 ≤ k ≤ n) that have to be included in an experiment in order to keep the data available for any client.

In the second line print k distinct integers x1, x2, ..., xk (1 ≤ xi ≤ n), the indices of data centers whose maintenance time will be shifted by one hour later. Data center indices may be printed in any order.

If there are several possible answers, it is allowed to print any of them. It is guaranteed that at there is at least one valid choice of data centers.

Examples
input
Copy
3 3 5
4 4 0
1 3
3 2
3 1
output
1
3 
input
Copy
4 5 4
2 1 0 3
4 3
3 2
1 2
1 4
1 3
output
4
1 2 3 4 
Note

Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.

On the other hand, for example, if we shift the maintenance time on hour later for the first data center, then the data of clients 1 and 3 will be unavailable during the hour 0.



某土豪公司建立了n个数据中心,把m份资料每份在其中的两个数据中心备份。

每个数据中心在一天h个小时当中有一个小时需要维护,此时不提供资料下载服务。

现在土豪公司想要将其中若干个数据中心的维护时间向后推迟一小时,并要求一天中任意时刻每份资料都可以被下载,问最少选取多少个数据中心维护。


可以将题目抽象成一个图论模型。

每个数据中心是一个点,若选择A中心调整维护时间时,必须选择B中心,则用一条有向边连接A和B。

按照题目的要求,如果备份同一份数据两个中心的维护时间相差一小时,则两者之间需要连接一条边。

显然建图之后,同一个强连通分量之内的边必须一起选。可以依此缩点构成DAG。而且,要选拓扑序在先的块,则必须选它所指向的所有块。题目要求选取的点最少,显然在DAG内选择出度为0的块是最优的。

那么建图之后tarjan缩点建新图统计度数,最后直接取出度为0的、点数最少的块(强连通分量)就可以了。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int head[maxn],a[maxn],d[maxn];
int dfn[maxn],low[maxn],color[maxn],val[maxn];
bool inst[maxn];
vector<int> v[maxn];
stack<int> st;
int num=0,cnum=0;

struct Edge {
	int from,to,pre;
};
Edge edge[maxn*2];

void addedge(int from,int to) {
	edge[num]=(Edge){from,to,head[from]};
	head[from]=num++;
}

void tarjan(int now) {
	num++;
	dfn[now]=low[now]=num;
	inst[now]=1;
	st.push(now);
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!dfn[to]) {
			tarjan(to);
			low[now]=min(low[now],low[to]);
		}
		else if (inst[to]) 
		    low[now]=min(low[now],dfn[to]); 
	}
	if (dfn[now]==low[now]) {
		inst[now]=0;
		color[now]=++cnum;
		v[cnum].push_back(now);
		while (st.top()!=now) {
			color[st.top()]=cnum;
			inst[st.top()]=0;
			v[cnum].push_back(st.top());
			st.pop();
		}
		st.pop();
	}
}

int main() {
	memset(head,-1,sizeof(head));
	num=0;
	int n,m,k,i,j,x,y;
	scanf("%d%d%d",&n,&m,&k);
	for (i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (i=1;i<=m;i++) {
		scanf("%d%d",&x,&y);
		if ((a[x]+1)%k==a[y]) addedge(x,y);
		if ((a[y]+1)%k==a[x]) addedge(y,x);
	}
	m=num;num=0;
	mem0(dfn);mem0(low);mem0(color);mem0(inst);
	for (i=1;i<=n;i++)
		if (!dfn[i]) tarjan(i);
	int ans=-1;
	mem0(d);
	for (i=0;i<m;i++) 
		if (color[edge[i].from]!=color[edge[i].to]) 
			d[color[edge[i].from]]++;
	for (i=1;i<=cnum;i++)
		if (d[i]==0) 
			if (ans==-1) ans=i; else 
				if (v[i].size()<v[ans].size()) ans=i;
	printf("%d\n",v[ans].size());
	for (i=0;i<v[ans].size();i++) printf("%d ",v[ans][i]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值