Codeforces Round #427 (Div. 2) A—D题

来源:互联网 发布:手机qq聊天监控软件 编辑:程序博客网 时间:2024/06/14 07:00

传送门:点击打开链接

A. Key races
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two boys decided to compete in text typing on the site "Key races". During the competition, they have to type a text consisting of scharacters. The first participant types one character in v1 milliseconds and has ping t1 milliseconds. The second participant types one character in v2 milliseconds and has ping t2 milliseconds.

If connection ping (delay) is t milliseconds, the competition passes for a participant as follows:

  1. Exactly after t milliseconds after the start of the competition the participant receives the text to be entered.
  2. Right after that he starts to type it.
  3. Exactly t milliseconds after he ends typing all the text, the site receives information about it.

The winner is the participant whose information on the success comes earlier. If the information comes from both participants at the same time, it is considered that there is a draw.

Given the length of the text and the information about participants, determine the result of the game.

Input

The first line contains five integers sv1v2t1t2 (1 ≤ s, v1, v2, t1, t2 ≤ 1000) — the number of characters in the text, the time of typing one character for the first participant, the time of typing one character for the the second participant, the ping of the first participant and the ping of the second participant.

Output

If the first participant wins, print "First". If the second participant wins, print "Second". In case of a draw print "Friendship".

Examples
input
5 1 2 1 2
output
First
input
3 3 1 1 1
output
Second
input
4 5 3 1 5
output
Friendship
Note

In the first example, information on the success of the first participant comes in 7 milliseconds, of the second participant — in 14milliseconds. So, the first wins.

In the second example, information on the success of the first participant comes in 11 milliseconds, of the second participant — in 5milliseconds. So, the second wins.

In the third example, information on the success of the first participant comes in 22 milliseconds, of the second participant — in 22milliseconds. So, it is be a draw.


签到题,不解释。

代码实现:
#include<iostream>#include<algorithm>using namespace std;int main(){long long s,v1,v2,t1,t2,sum1,sum2;while(cin>>s>>v1>>v2>>t1>>t2){sum1=s*v1+2*t1;sum2=s*v2+2*t2;if(sum1<sum2)cout<<"First"<<endl;else if(sum1>sum2)cout<<"Second"<<endl;elsecout<<"Friendship"<<endl;}return 0;}

B. The number on the board
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.

You have to find the minimum number of digits in which these two numbers can differ.

Input

The first line contains integer k (1 ≤ k ≤ 109).

The second line contains integer n (1 ≤ n < 10100000).

There are no leading zeros in n. It's guaranteed that this situation is possible.

Output

Print the minimum number of digits in which the initial number and n can differ.

Examples
input
311
output
1
input
399
output
0
Note

In the first example, the initial number could be 12.

In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.


后面的数经过最少次变换,它各位数之和变成前面数。

需要用字符串来吸收,如果各位数和大于k,则输出0,否则对字符串进行排序,把最小的数变为9,知道大于等于k为止。

代码实现:
#include<iostream>#include<algorithm>#include<cstring>using namespace std;int main(){char map[1000005];int k,i;while(cin>>k){memset(map,'\0',sizeof(map));cin>>map;int len=strlen(map);long long ans=0;for(i=0;i<len;i++)ans+=map[i]-'0';if(ans>k){cout<<"0"<<endl;continue;}sort(map,map+len);int count=0;i=0;while(ans<k){ans+=9-(map[i]-'0');count++;i++;}cout<<count<<endl;}return 0;}

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

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xiyi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).

Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment(t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.

You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1iy1i) and the upper right — (x2iy2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.

A star lies in a rectangle if it lies on its border or lies strictly inside it.

Input

The first line contains three integers nqc (1 ≤ n, q ≤ 1051 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.

The next n lines contain the stars description. The i-th from these lines contains three integers xiyisi (1 ≤ xi, yi ≤ 100,0 ≤ si ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness.

The next q lines contain the views description. The i-th from these lines contains five integers tix1iy1ix2iy2i (0 ≤ ti ≤ 109,1 ≤ x1i < x2i ≤ 1001 ≤ y1i < y2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle.

Output

For each view print the total brightness of the viewed stars.

Examples
input
2 3 31 1 13 2 02 1 1 2 20 2 1 4 55 1 1 5 5
output
303
input
3 4 51 1 22 3 03 3 10 1 1 100 1001 2 2 4 42 2 1 4 71 50 50 51 51
output
3350
Note

Let's consider the first example.

At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.

At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.

At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.


有n个坐标,每个坐标给出位置(x,y)和亮度,亮度随时间增加,但超过c即为0. 给出q次询问,每次都是一个矩形区域,和访问时间,输出当前区域的亮度总和。

DP啦,DP[i][j][k]表示坐标为i,j的位置亮度为k的坐标的个数。用到两次容斥原理,即当前亮度和等于i-1和j-1的和再减去i-1,j-1.

代码实现:
#include<iostream>#include<algorithm>#include<cstring>using namespace std;int main(){int dp[105][105][20],i,j,k,n,q,c,x,y,s,t,x1,y1,x2,y2;while(cin>>n>>q>>c){memset(dp,0,sizeof(dp));  //清空数组 for(i=0;i<n;i++){cin>>x>>y>>s;dp[x][y][s]++;   //当前数加1 }for(i=1;i<105;i++){for(j=1;j<105;j++){for(k=0;k<=15;k++){dp[i][j][k]+=dp[i-1][j][k]+dp[i][j-1][k]-dp[i-1][j-1][k];  //前面两个的亮度和减去重合部分,容斥 }}}while(q--){cin>>t>>x1>>y1>>x2>>y2;long long ans=0;for(i=0;i<=c;i++){int light=(i+t)%(c+1);  //时间为t时,灯光的亮度。 ans+=light*(dp[x2][y2][i]-dp[x2][y1-1][i]-dp[x1-1][y2][i]+dp[x1-1][y1-1][i]);}cout<<ans<<endl;}}return 0;}

D. Palindromic characteristics
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.

A string is 1-palindrome if and only if it reads the same backward as forward.

A string is k-palindrome (k > 1) if and only if:

  1. Its left half equals to its right half.
  2. Its left and right halfs are non-empty (k - 1)-palindromes.

The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string tdivided by 2, rounded down.

Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.

Input

The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.

Output

Print |s| integers — palindromic characteristics of string s.

Examples
input
abba
output
6 1 0 0 
input
abacaba
output
12 4 1 0 0 0 0 
Note

In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here.

看note应该能读懂题意。

看到大神的做法是dp思想,dp【i】【j】表示字符串长度为j时,包括的i阶的字符串个数。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio> using namespace std;int dp[5005][5005];int main(){char map[5005];int ans[5005],i,j,k;while(~scanf("%s",map+1)){memset(dp,0,sizeof(dp));memset(ans,0,sizeof(ans));int len=strlen(map+1);for(i=1;i<=len;i++){dp[i][i]=1;ans[1]++;if(i<len&&map[i]==map[i+1]){ans[2]++;ans[1]++;dp[i][i+1]=2;}}for(i=3;i<=len;i++){for(j=1;j+i-1<=len;j++){if(dp[j+1][j+i-2]&&map[j]==map[i+j-1])                  {                    dp[j][j+i-1]=dp[j][j+i/2-1]+1;                    for(k=1;k<=dp[j][j+i-1];k++)ans[k]++;                  }}}cout<<ans[1];for(i=2;i<=len;i++)cout<<' '<<ans[i];cout<<endl;}return 0;}





原创粉丝点击