您的位置:首页 > 其它

Educational Codeforces Round 65 (Rated for Div. 2)

2019-05-30 18:11 387 查看

A. Telephone Number
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.

You are given a string s of length n, consisting of digits.

In one operation you can delete any character from string s. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.

You need to determine whether there is such a sequence of operations (possibly empty), after which the string s becomes a telephone number.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains one integer n (1≤n≤100) — the length of string s.

The second line of each test case contains the string s (|s|=n) consisting of digits.

Output
For each test print one line.

If there is a sequence of operations, after which s becomes a telephone number, print YES.

Otherwise, print NO.

Example
inputCopy
2
13
7818005553535
11
31415926535
outputCopy
YES
NO
Note
In the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535.

题意:定义一个数字串串为一个手机号当且仅当这个数字串以8开头并且长度为11,现在给出t组数据,每组数据给出一个数字串,询问这个数字串能否经过删除一些数字变成字符串
题解:只要从8出现的位置到末尾的长度>=11即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
char ch[105];
scanf("%s",ch+1);
int f=0;
for(int i=1;i<=n;i++){
if(ch[i]=='8'&&n-i+1>=11){
f=1;
break;
}
}
if(f)printf("YES\n");
else printf("NO\n");
}
return 0;
}

B. Lost Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.

The jury guessed some array a consisting of 6 integers. There are 6 special numbers — 4, 8, 15, 16, 23, 42 — and each of these numbers occurs in a exactly once (so, a is some permutation of these numbers).

You don’t know anything about their order, but you are allowed to ask up to 4 queries. In each query, you may choose two indices i and j (1≤i,j≤6, i and j are not necessarily distinct), and you will get the value of ai⋅aj in return.

Can you guess the array a?

The array a is fixed beforehand in each test, the interaction program doesn’t try to adapt to your queries.

Interaction
Before submitting the answer, you may ask up to 4 queries. To ask a query, print one line in the following format: ? i j, where i and j should be two integers such that 1≤i,j≤6. The line should be ended with a line break character. After submitting a query, flush the output and read the answer to your query — one line containing one integer a 4000 i⋅aj. If you submit an incorrect query (or ask more than 4 queries), the answer to it will be one string 0. After receiving such an answer, your program should terminate immediately — otherwise you may receive verdict “Runtime error”, “Time limit exceeded” or some other verdict instead of “Wrong answer”.

To give the answer, your program should print one line ! a1 a2 a3 a4 a5 a6 with a line break in the end. After that, it should flush the output and terminate gracefully.

Example
inputCopy
16
64
345
672
outputCopy
? 1 1
? 2 2
? 3 5
? 4 6
! 4 8 15 16 23 42
Note
If you want to submit a hack for this problem, your test should contain exactly six space-separated integers a1, a2, …, a6. Each of 6 special numbers should occur exactly once in the test. The test should be ended with a line break character.

题意:一道简单的交互题,只要会写交互格式即可。有6个数字4, 8, 15, 16, 23, 42的一个排列,要求你经过4次询问得出这个排列,询问格式为"? a b"得到a*b的值。第一次询问"? 1 2",第二次"? 1 3"即可得出a1,a2,a3的值,第三次询问"? 4 5",第四次询问"? 4 6"即可得出a4,a5,a6的值

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
int a[7]={0,4, 8, 15, 16, 23, 42 };
int a1,a2,a3,a4,a5,a6;
int main(){
cout<<"? 1 2"<<endl;
fflush(stdout);
int x,y;
cin>>x;
cout<<"? 1 3"<<endl;
fflush(stdout);
cin>>y;
for(int i=1;i<=6;i++){
for(int j=1;j<=6;j++){
for(int k=1;k<=6;k++){
if((i!=j&&i!=k&&j!=k)&&(a[i]*a[j]==x&&a[i]*a[k]==y)){
a1=a[i];
a2=a[j];
a3=a[k];
}
}
}
}
cout<<"? 4 5"<<endl;
fflush(stdout);
cin>>x;
cout<<"? 4 6"<<endl;
fflush(stdout);
cin>>y;
for(int i=1;i<=6;i++){
for(int j=1;j<=6;j++){
for(int k=1;k<=6;k++){
if((i!=j&&i!=k&&j!=k)&&(a[i]*a[j]==x&&a[i]*a[k]==y)){
a4=a[i];
a5=a[j];
a6=a[k];
}
}
}
}
cout<<"! "<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<" "<<a5<<" "<<a6<<endl;
fflush(stdout);
return 0;
}

C. News Distribution
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In some social network, there are n users communicating with each other in m groups of friends. Let’s analyze the process of distributing some news between users.

Initially, some user x receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn’t know.

For each user x you have to determine what is the number of users that will know the news if initially only user x starts distributing it.

Input
The first line contains two integers n and m (1≤n,m≤5⋅105) — the number of users and the number of groups of friends, respectively.

Then m lines follow, each describing a group of friends. The i-th line begins with integer ki (0≤ki≤n) — the number of users in the i-th group. Then ki distinct integers follow, denoting the users belonging to the i-th group.

It is guaranteed that ∑i=1mki≤5⋅105.

Output
Print n integers. The i-th integer should be equal to the number of users that will know the news if user i starts distributing it.

Example
inputCopy
7 5
3 2 5 4
0
2 1 2
1 1
2 6 7
outputCopy
4 4 1 4 4 2 2

题意:有n个人,m个团体,两个人互为朋友当且仅当二人至少同时存在于一个团体中,每个人得到一个消息之后会和朋友分享,求每个人得到消息后会有多少人得到消息
题解:并查集

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
int fa[500005],siz[500005];
int finds(int x){
int xx=x;
while(fa[x]!=x){
x=fa[x];
}
while(fa[xx]!=x){
int t=fa[xx];
fa[xx]=x;
xx=t;
}
return x;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
fa[i]=i;
siz[i]=1;
}
for(int i=1;i<=m;i++){
int w,ww;
scanf("%d",&w);
if(w)scanf("%d",&ww);
for(int i=1;i<w;i++){
int www;
scanf("%d",&www);
int f1=finds(ww);
int f2=finds(www);
if(f1!=f2){
if(siz[f1]>siz[f2]){
fa[f2]=f1;
siz[f1]+=siz[f2];
}
else{
fa[f1]=f2;
siz[f2]+=siz[f1];
}
}
}
}
for(int i=1;i<=n;i++){
int f=finds(i);
printf("%d",siz[f]);
char cc=(i==n)?'\n':' ';
printf("%c",cc);
}
return 0;
}

D. Bicolored RBS
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A string is called bracket sequence if it does not contain any characters other than “(” and “)”. A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are RBS and “)(” and “(()” are not.

We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 2-nd pair lies inside the 1-st one, the 3-rd one — inside the 2-nd one and so on. For example, nesting depth of “” is 0, “()()()” is 1 and “()((())())” is 3.

Now, you are given RBS s of even length n. You should color each bracket of s into one of two colors: red or blue. Bracket sequence r, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets b, should be RBS. Any of them can be empty. You are not allowed to reorder characters in s, r or b. No brackets can be left uncolored.

Among all possible variants you should choose one that minimizes maximum of r’s and b’s nesting depth. If there are multiple solutions you can print any of them.

Input
The first line contains an even integer n (2≤n≤2⋅105) — the length of RBS s.

The second line contains regular bracket sequence s (|s|=n, si∈{"(", “)”}).

Output
Print single string t of length n consisting of “0”-s and “1”-s. If ti is equal to 0 then character si belongs to RBS r, otherwise si belongs to b.

Examples
inputCopy
2
()
outputCopy
11
inputCopy
4
(())
outputCopy
0101
inputCopy
10
((()())())
outputCopy
0110001111
Note
In the first example one of optimal solutions is s= “()”. r is empty and b= “()”. The answer is max(0,1)=1.

In the second example it’s optimal to make s= “(())”. r=b= “()” and the answer is 1.

In the third example we can make s= “((()())())”. r= “()()” and b= “(()())” and the answer is 2.D. Bicolored RBS
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A string is called bracket sequence if it does not contain any characters other than “(” and “)”. A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are RBS and “)(” and “(()” are not.

We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 2-nd pair lies inside the 1-st one, the 3-rd one — inside the 2-nd one and so on. For example, nesting depth of “” is 0, “()()()” is 1 and “()((())())” is 3.

Now, you are given RBS s of even length n. You should color each bracket of s into one of two colors: red or blue. Bracket sequence r, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets b, should be RBS. Any of them can be empty. You are not allowed to reorder characters in s, r or b. No brackets can be left uncolored.

Among all possible variants you should choose one that minimizes maximum of r’s and b’s nesting depth. If there are multiple solutions you can print any of them.

Input
The first line contains an even integer n (2≤n≤2⋅105) — the length of RBS s.

The second line contains regular bracket sequence s (|s|=n, si∈{"(", “)”}).

Output
Print single string t of length n consisting of “0”-s and “1”-s. If ti is equal to 0 then character si belongs to RBS r, otherwise si belongs to b.

Examples
inputCopy
2
()
outputCopy
11
inputCopy
4
(())
outputCopy
0101
inputCopy
10
((()())())
outputCopy
0110001111
Note
In the first example one of optimal solutions is s= “()”. r is empty and b= “()”. The answer is max(0,1)=1.

In the second example it’s optimal to make s= “(())”. r=b= “()” and the answer is 1.

In the third example we can make s= “((()())())”. r= “()()” and b= “(()())” and the answer is 2.

题意:给出一个()串,给()染色,染成01,要求0和1的层数的最大值最小,层数的定义为如(()())为2,(())也为2,()()为1
题解:二分出最小的层数,然后根据最小层数填数即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
char ch[200005];
int n,w[200005];
bool check(int x){
int s[4]={0};
int id=0;
for(int i=1;i<=n;i++){
if(ch[i]=='('){
s[id]++;
if(s[id]>x)return false;
if(s[id]==x){
id^=1;
}
}
else{
if(s[0]>s[1]){
s[0]--;
}
else if(s[0]<s[1]){
s[1]--;
}
else{
s[id]--;
}
}
}
return true;
}
int main(){
// int n;
scanf("%d",&n);
scanf("%s",ch+1);
int l=1;
int r=n;
int ans;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
int s[4]={0};
int id=0;
int id2=0;
for(int i=1;i<=n;i++){
if(ch[i]=='('){
s[id]++;
w[i]=id;
if(s[id]==ans){
id^=1;
}
}
else{
if(s[0]>s[1]){
s[0]--;
w[i]=0;
}
else if(s[0]<s[1]){
w[i]=1;
s[1]--;
}
else{
s[id]--;
w[i]=id;
}
}
}
for(int i=1;i<=n;i++){
printf("%d",w[i]);
}
printf("\n");
return 0;
}

E. Range Deleting
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array consisting of n integers a1,a2,…,an and an integer x. It is guaranteed that for every i, 1≤ai≤x.

Let’s denote a function f(l,r) which erases all values such that l≤ai≤r from the array a and returns the resulting array. For example, if a=[4,1,1,4,5,2,4,3], then f(2,4)=[1,1,5].

Your task is to calculate the number of pairs (l,r) such that 1≤l≤r≤x and f(l,r) is sorted in non-descending order. Note that the empty array is also considered sorted.

Input
The first line contains two integers n and x (1≤n,x≤106) — the length of array a and the upper limit for its elements, respectively.

The second line contains n integers a1,a2,…an (1≤ai≤x).

Output
Print the number of pairs 1≤l≤r≤x such that f(l,r) is sorted in non-descending order.

Examples
inputCopy
3 3
2 3 1
outputCopy
4
inputCopy
7 4
1 3 1 2 2 4 3
outputCopy
6
Note
In the first test case correct pairs are (1,1), (1,2), (1,3) and (2,3).

In the second test case correct pairs are (1,3), (1,4), (2,3), (2,4), (3,3) and (3,4).

题意:给出一个序列,可以删除值为[L,R]内的数,要求删除后序列为非递减序列,求这种删除方案的个数
题意:首先按照元素的值进行排序。
删除的区间可以使原序列非递减当且仅当
(1)删除区间左边的区间的元素的原来位置id递增
(2)并且右边的区间元素原id递增
(3)并且左边最大的值的原id在右边原id的左边。
对于(1)(2)可以直接在排序之后找到前面i个元素和后面j个元素,并且这i个元素值非递减,原id递增,后面j个元素值非递减并且原id递增,然后就可以在这i个元素中枚举删除的左边界,二分右边界即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
struct pot{
int id;
int val;
}p[1000005];
int aw[1000005],bw[1000005],cw[1000005],dw[1000005];
bool cmp(struct pot a,struct pot b){
if(a.val!=b.val)return a.val<b.val;
return a.id<b.id;
}
int main(){
int n,x;
scanf("%d%d",&n,&x);
for(int i=1;i<=n;i++){
scanf("%d",&p[i].val);
p[i].id=i;
}
int minr,maxl;
maxl=x;
minr=1;
int minn=n+1;
int tot=0;
sort(p+1,p+1+n,cmp);
for(int i=n;i>=1;i--){
if(p[i].id>minn){
minr=p[i].val;
aw[tot+1]=-p[i].id;
cw[tot+1]=p[i].val;
break;
}
aw[++tot]=-p[i].id;
cw[tot]=p[i].val;
minn=p[i].id;
}
int maxx=0;
int tot2=0;
for(int i=1;i<=n;i++){
if(p[i].id<maxx){
maxl=p[i].val;
bw[tot2+1]=p[i].id;
dw[tot2+1]=p[i].val;
break;;
}
maxx=p[i].id;
bw[++tot2]=p[i].id;
dw[tot2]=p[i].val;
}
ll ans=0;
int pos0=0;
int sta=1;
for(int i=1;i<=maxl;i++){
int pos1=upper_bound(dw+1,dw+1+tot2,i-1)-dw;
if(dw[pos1]>(i-1)){
pos1--;
}
int pos=upper_bound(aw+1,aw+1+tot,-bw[pos1])-aw;
ans+=x-max(cw[pos],i)+1;
}
printf("%lld\n",ans);
return 0;
}

F. Scalar Queries
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a1,a2,…,an. All ai are pairwise distinct.

Let’s define function f(l,r) as follows:

let’s define array b1,b2,…,br−l+1, where bi=al−1+i;
sort array b in increasing order;
result of the function f(l,r) is ∑i=1r−l+1bi⋅i.
Calculate (∑1≤l≤r≤nf(l,r))mod(109+7), i.e. total sum of f for all subsegments of a modulo 109+7.

Input
The first line contains one integer n (1≤n≤5⋅105) — the length of array a.

The second line contains n integers a1,a2,…,an (1≤ai≤109, ai≠aj for i≠j) — array a.

Output
Print one integer — the total sum of f for all subsegments of a modulo 109+7
Examples
inputCopy
4
5 2 4 7
outputCopy
167
inputCopy
3
123456789 214365879 987654321
outputCopy
582491518
Note
Description of the first example:

f(1,1)=5⋅1=5;
f(1,2)=2⋅1+5⋅2=12;
f(1,3)=2⋅1+4⋅2+5⋅3=25;
f(1,4)=2⋅1+4⋅2+5⋅3+7⋅4=53;
f(2,2)=2⋅1=2;
f(2,3)=2⋅1+4⋅2=10;
f(2,4)=2⋅1+4⋅2+7⋅3=31;
f(3,3)=4⋅1=4;
f(3,4)=4⋅1+7⋅2=18;
f(4,4)=7⋅1=7;

待补
G. Low Budget Inception
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
So we got bored and decided to take our own guess at how would “Inception” production go if the budget for the film had been terribly low.

The first scene we remembered was the one that features the whole city bending onto itself:

It feels like it will require high CGI expenses, doesn’t it? Luckily, we came up with a similar-looking scene which was a tiny bit cheaper to make.

Firstly, forget about 3D, that’s hard and expensive! The city is now represented as a number line (infinite to make it easier, of course).

Secondly, the city doesn’t have to look natural at all. There are n buildings on the line. Each building is a square 1×1. Buildings are numbered from 1 to n in ascending order of their positions. Lower corners of building i are at integer points ai and ai+1 of the number line. Also the distance between any two neighbouring buildings i and i+1 doesn’t exceed d (really, this condition is here just to make the city look not that sparse). Distance between some neighbouring buildings i and i+1 is calculated from the lower right corner of building i to the lower left corner of building i+1.

Finally, curvature of the bend is also really hard to simulate! Let the bend at some integer coordinate x be performed with the following algorithm. Take the ray from x to +∞ and all the buildings which are on this ray and start turning the ray and the buildings counter-clockwise around point x. At some angle some building will touch either another building or a part of the line. You have to stop bending there (implementing buildings crushing is also not worth its money).

Let’s call the angle between two rays in the final state the terminal angle αx.

The only thing left is to decide what integer point x is the best to start bending around. Fortunately, we’ve already chosen m candidates to perform the bending.

So, can you please help us to calculate terminal angle αx for each bend x from our list of candidates?

Input
The first line contains two integer numbers n and d (1≤n≤2⋅105, 0≤d≤7000) — the number of buildings and the maximum distance between any pair of neighbouring buildings, respectively.

The second line contains n integers a1,a2,…,an (a1=0, 0<ai+1−ai≤d+1) — coordinates of left corners of corresponding buildings in ascending order.

The third line contains single integer m (1≤m≤2⋅105) — the number of candidates.

The fourth line contains m integers x1,x2,…,xm (0≤xi≤an+1, xi<xi+1) — the coordinates of bends you need to calculate terminal angles for in ascending order.

Output
Print m numbers. For each bend xi print terminal angle αxi (in radians).

Your answer is considered correct if its absolute error does not exceed 10−9.

Formally, let your answer be a, and the jury’s answer be b. Your answer is accepted if and only if |a−b|≤10−9.

Examples
inputCopy
3 5
0 5 7
9
0 1 2 3 4 5 6 7 8
outputCopy
1.570796326794897
1.570796326794897
0.785398163397448
0.927295218001612
0.785398163397448
1.570796326794897
1.570796326794897
1.570796326794897
1.570796326794897
inputCopy
2 7
0 4
3
1 3 4
outputCopy
1.570796326794897
0.927295218001612
1.570796326794897
inputCopy
5 0
0 1 2 3 4
6
0 1 2 3 4 5
outputCopy
1.570796326794897
3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793
1.570796326794897
Note
Here you can see the picture of the city for the first example and the bend at position 2 for it. The angle you need to measure is marked blue. You can see that it’s equal to π4.

You can see that no pair of neighbouring buildings have distance more than 4 between them. d=4 would also suffice for that test.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐