Codeforces Round #350 (Div. 2)

来源:互联网 发布:淘宝助理模板数据包 编辑:程序博客网 时间:2024/06/07 03:27
A. Holidays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings — 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 1 000 000) — the number of days in a year on Mars.

Output

Print two integers — the minimum possible and the maximum possible number of days off per year on Mars.

Examples
input
14
output
4 4
input
2
output
0 2
Note

In the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off .

In the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off.


题意: 火星和地球一样。有5个工作日,2个假日。 问给定n最少能过多少个节日最多能过多少个假日

思路:水题。 最少:从周日开始。 最多:从周六开始

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<stack>#include<bitset>#include<map>#include<set>using namespace std;const int MAXN=500+5;#define inf 0x3f3f3f3ftypedef long long int LL;int main(){int n;while(scanf("%d",&n)!=EOF){int minans,maxans;minans=(n/7)*2+(n%7>=6?1:0);if(n<=2){maxans=n;}else{maxans=2; n-=2;maxans+=(n/7)*2+(n%7>=6?1:0);}printf("%d %d\n",minans,maxans);}return 0;}

B. Game of Robots
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.

At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.

Your task is to determine the k-th identifier to be pronounced.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 0001 ≤ k ≤ min(2·109, n·(n + 1) / 2).

The second line contains the sequence id1, id2, ..., idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.

Output

Print the k-th pronounced identifier (assume that the numeration starts from 1).

Examples
input
2 21 2
output
1
input
4 510 4 18 3
output
4
Note

In the first sample identifiers of robots will be pronounced in the following order: 112. As k = 2, the answer equals to 1.

In the second test case identifiers of robots will be pronounced in the following order: 101041041810418,3. As k = 5, the answer equals to 4.


题意:每个人都有自己的名字,现在在玩一个游戏,叫做叫名字,每个人都会叫前面人的名字和自己的名字,现在问你第k个叫的名字是啥

思路:第一个人: 1  第二个人: 1 2 第三个人: 1 2 3 第四个人:1 2 3 4 ....很明显。第i个人需要叫1-i, 那么我们统计前缀和就可以知道第k个人是谁叫的了,然后求余一下就知道叫的是谁了。

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<stack>#include<bitset>#include<map>#include<set>using namespace std;const int MAXN=100000+5;#define inf 0x3f3f3f3ftypedef long long int LL;LL sum[MAXN],id[MAXN];void init(){sum[0]=0;for(int i=1;i<=100000;i++){sum[i]=sum[i-1]+i;}}int main(){int n;LL k;init();while(scanf("%d%I64d",&n,&k)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&id[i]);}int index=upper_bound(sum,sum+100000+1,k)-sum;k=k-sum[index-1];printf("%I64d\n",id[k%(index-1)==0?index-1:(k%(index-1))]);}return 0;}

C. Cinema
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.

In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different).

Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows.

The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.

The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie.

The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie.

It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Output

Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.

If there are several possible answers print any of them.

Examples
input
32 3 223 22 3
output
2
input
66 3 1 1 3 751 2 3 4 52 3 4 5 1
output
1
Note

In the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.

In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.


题意: 有n个人。 每个人擅长一种语言a[i], 然后m部电影。每部电影有2个值,分别是这部电影的音频语言b[i]和字幕语言c[i] 要求你选一个电影优先满足能听懂的人最多。如果存在能听懂的人一样多的话,那么再满足能看懂字幕的人最多。 如果有多个同时满足,那么输出任意。

思路:暴力贪心就好。记录当前最优的能听懂音频的人数和看懂字幕的人。

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<stack>#include<bitset>#include<map>#include<set>using namespace std;const int MAXN=200000+5;#define inf 0x3f3f3f3ftypedef long long int LL;int a[MAXN],b[MAXN],c[MAXN];int main(){int n,m;while(scanf("%d",&n)!=EOF){int maxple=-1,maxrat=-1,index=0; map<int,int>count;for(int i=1;i<=n;i++){scanf("%d",&a[i]);count[a[i]]++;}scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d",&b[i]);}for(int i=1;i<=m;i++){scanf("%d",&c[i]);}for(int i=1;i<=m;i++){int tempple=count[b[i]];int temprat=count[c[i]];if(tempple>maxple){maxple=tempple;maxrat=temprat;index=i;}else if(tempple==maxple){if(temprat>maxrat){maxrat=temprat;index=i;}}}printf("%d\n",index);}return 0;}

D1. Magic Powder - 1
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.

Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.

Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

Input

The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples
input
3 12 1 411 3 16
output
4
input
4 34 3 5 611 12 14 20
output
3
Note

In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.

In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.


题意:给定n个材料,m个魔法值。每个魔法值可以替换成一点材料。  现在给定你拥有的每一种材料的数量a[i], 然后做一份食物需要每一种材料的数量b[i].

思路:最小堆模拟,  每次替换成最小的材料中。尽量做出更多的食物。

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<stack>#include<bitset>#include<map>#include<set>using namespace std;const int MAXN=1000+5;#define inf 0x3f3f3f3ftypedef long long int LL;int has[MAXN],need[MAXN];struct Node{int Cnt,Id;Node(int a=0,int b=0):Cnt(a),Id(b){}};struct cmp{ bool operator ()(Node &a,Node &b){          return a.Cnt>b.Cnt;//最小值优先      }};void solve(int n,int k){priority_queue<Node,vector<Node>,cmp>Q;for(int i=1;i<=n;i++){Q.push(Node(has[i]/need[i],i));has[i]%=need[i];}while(k>=0){int cnt=Q.top().Cnt;int id=Q.top().Id;Q.pop();if(k>=need[id]-has[id]){k-=(need[id]-has[id]);has[id]=0;cnt+=1;}else{printf("%d\n",cnt);return;}Q.push(Node(cnt,id));}}int main(){int n,k;while(scanf("%d%d",&n,&k)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&need[i]);}for(int i=1;i<=n;i++){scanf("%d",&has[i]);}solve(n,k);}return 0;}


D2. Magic Powder - 2
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.


题意:同上

思路:D2是D1的加强版。因为K的范围比较大,如果继续沿用D1的思路那么会TLE。   所以我们可以二分X,X为做出的食物数量。然后判断当前要做出X分食物所需的魔法值和K来比较。 注意下爆long long 

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<stack>#include<bitset>#include<map>#include<set>using namespace std;const int MAXN=100000+5;const int INF=2e9+5;#define inf 0x3f3f3f3ftypedef long long int LL;int has[MAXN],need[MAXN];bool check(LL x,int n,int k){LL temp=0;for(int i=1;i<=n;i++){if(has[i]/need[i]>=x){continue;}LL cnt=1LL*(x-has[i]/need[i])*need[i]-has[i]%need[i];if(temp-k>0||cnt-k>0||temp+cnt-k>0){ //先判断再进行累计。防止爆LONG LONG return true;}temp+=cnt;}if(temp>k){return true;}return false;}int main(){int n,k;while(scanf("%d%d",&n,&k)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&need[i]);}for(int i=1;i<=n;i++){scanf("%d",&has[i]);}LL l=0,r=INF,mid;while(r>=l){mid=(l+r)/2;if(check(mid,n,k)){r=mid-1;}else{l=mid+1;}}printf("%lld\n",r);}return 0;}

E. Correct Bracket Sequence Editor
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).

Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":

  • 1st bracket is paired with 8th,
  • 2d bracket is paired with 3d,
  • 3d bracket is paired with 2d,
  • 4th bracket is paired with 7th,
  • 5th bracket is paired with 6th,
  • 6th bracket is paired with 5th,
  • 7th bracket is paired with 4th,
  • 8th bracket is paired with 1st.

Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:

  • «L» — move the cursor one position to the left,
  • «R» — move the cursor one position to the right,
  • «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).

After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).

There are pictures illustrated several usages of operation "D" below.

All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.

Polycarp is very proud of his development, can you implement the functionality of his editor?

Input

The first line contains three positive integers nm and p (2 ≤ n ≤ 500 0001 ≤ m ≤ 500 0001 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.

It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.

Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.

Output

Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

Examples
input
8 4 5(())()()RDLD
output
()
input
12 5 3((()())(()))RRDLD
output
(()(()))
input
8 8 8(())()()LLLLLLDD
output
()()
Note

In the first sample the cursor is initially at position 5. Consider actions of the editor:

  1. command "R" — the cursor moves to the position 6 on the right;
  2. command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
  3. command "L" — the cursor moves to the position 4 on the left;
  4. command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.

Thus, the answer is equal to ().


题意:给定长度为n的括号字符串串, 给定m个操作,和当前位置pos,   操作一共分三种:L,光标[pos]向左移动一位,R,光标向右移动一位。 D,删除与当前位置括号匹配的所有括号[即匹配括号内部的所有括号],并且光标移动到下一个括号处。

思路:先用栈来确定每个括号的匹配位置。然后进行链表模拟即可。

#include<iostream>#include<stdio.h>#include<cstring>#include<string>#include<cmath>#include<map>#include<set>#include<queue>#include<stack> #include<bitset>#include<vector> #include<algorithm>using namespace std; const int MAXN=500000+5;#define INF 0x3f3f3f3fchar str[MAXN],op[MAXN];int Next[MAXN],pre[MAXN],pos[MAXN];stack<int>st;int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);int n,m,p;while(scanf("%d%d%d",&n,&m,&p)!=EOF){scanf("%s",(str+1));scanf("%s",op);pre[0]=0;   Next[0]=1;pre[n+1]=n; Next[n+1]=n+1;for(int i=1;i<=n;i++){if(str[i]=='('){st.push(i);}else{int l=st.top(); st.pop();int r=i;pos[l]=r;pos[r]=l;}Next[i]=i+1;pre[i]=i-1;}int x=p;for(int i=0;i<m;i++){if(op[i]=='L'){x=pre[x];}else if(op[i]=='R'){x=Next[x];}else{int l,r;l=x,r=pos[x];if(l>r){swap(l,r);}Next[pre[l]]=Next[r];pre[Next[r]]=pre[l];x=Next[r];if(x>n){x=pre[l];}}}for(int i=Next[0];i<=n;i=Next[i]){printf("%c",str[i]);}printf("\n");}return 0;}


0 0
原创粉丝点击