Codeforces Round #427 (Div. 2) A B C D

来源:互联网 发布:美容院连锁软件 编辑:程序博客网 时间:2024/06/06 02:15

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 s characters. 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 t2milliseconds.

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 14 milliseconds. 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 5 milliseconds. 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 22 milliseconds. So, it is be a draw.



水题

#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#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;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int main() {//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);int s,v1,v2,t1,t2;cin >> s >> v1 >> v2 >> t1 >> t2;if (v1*s+2*t1<v2*s+2*t2) printf("First"); else     if (v1*s+2*t1>v2*s+2*t2) printf("Second"); else printf("Friendship");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。

尽量把小的改成9就可以了。

#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#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;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);char s[maxn];int a[105];int main() {int n,k,len,i,j,ans=0,sum;scanf("%d",&k);scanf("%s",s);len=strlen(s);sum=k;mem0(a);for (i=0;i<len;i++) {a[s[i]-'0']++;sum-=s[i]-'0';}for (i=0;i<=9;i++) {for (j=1;j<=a[i];j++) {if (sum>0) sum-=9-i,ans++; else break;}if (sum<=0) break;}cout << ans;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 ≤ 1000 ≤ 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 ≤ 1091 ≤ 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个点,每个点的值随时间周期变化,达到c时值变为0,否则值+1.要求查询任意时刻坐标系内某个子矩阵

内的值的和。


如果没有周期,这题就是很普通的二维树状数组模板题,详见poj 2155.

周期最大为10,好了,我们把所有查询的时刻离散到一个周期的不同状态,不断更新就好了。

#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#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;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int f[105][105];int ans[maxn];struct query{int x1,y1,x2,y2,t,id;};query q[maxn];bool cmp(query a,query b) {return a.t<b.t;}struct star{int x,y,s;};star a[maxn];int lowbit(int v) {return (v&(-v));}void update (int n,int m,int c) {    int i=n,j=m;for (i=n;i<=102;i+=lowbit(i))    for (j=m;j<=102;j+=lowbit(j))        f[i][j]+=c;}int findsum(int n,int m) {int i=n,j=m;int sum=0;for (i=n;i>0;i-=lowbit(i))    for (j=m;j>0;j-=lowbit(j))        sum+=f[i][j];return sum;}int main() {int n,m,c,i,j;mem0(f);scanf("%d%d%d",&n,&m,&c);for (i=1;i<=n;i++) {scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].s);update(a[i].x+1,a[i].y+1,a[i].s);} for (i=1;i<=m;i++) {scanf("%d%d%d%d%d",&q[i].t,&q[i].x1,&q[i].y1,&q[i].x2,&q[i].y2);q[i].id=i;q[i].t%=(c+1);}sort(q+1,q+m+1,cmp);int t;j=1,i=1;for (t=0;t<=c;t++) {while (q[i].t<=t&&i<=m) {ans[q[i].id]=findsum(q[i].x2+1,q[i].y2+1)-findsum(q[i].x1,q[i].y2+1)-findsum(q[i].x2+1,q[i].y1)+findsum(q[i].x1,q[i].y1);i++;}for (j=1;j<=n;j++) {if ((a[j].s+t+1)%(c+1)!=0) {update(a[j].x+1,a[j].y+1,1);} else {update(a[j].x+1,a[j].y+1,-c);}}}for (i=1;i<=m;i++) {printf("%d\n",ans[i]);}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 t divided 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.



k重回文子串指左右两部分子串都为k-1重的相同回文子串的字符串。问字符串所有子串中,1到len(字符串长度)

重回文子串各有多少个。注意,k重回文子串也算k-1,k-2...1重回文子串。单个字符就是1重回文子串。


利用manacher算法可以O(n)线性时间内求一个字符串的最长回文子串。

详细请戳:https://segmentfault.com/a/1190000003914228

知道这个算法之后,就很简单了。利用manacher算法把每个后缀都过一遍,把所有回文子串的结尾都记录下

来,放在队列中,再从前向后推。因为k重回文子串也算k-1,k-2...1重回文子串,所以还要处理一下前缀和。复杂

度O(n^2),直接跑过。


//manacher#include <cstdio>#include <iostream>#include <string.h>#include <queue>using namespace std;typedef long long ll;const int maxn=10005;char s[maxn];int rl[maxn],sum[maxn],ans[maxn];queue<int> q;void manacher(int len) {int i,maxright,pos;maxright=pos=0;for (i=0;i<len;i++) {if (i<=maxright) {rl[i]=min(rl[2*pos-i],maxright-i);} else rl[i]=1;while (i-rl[i]>=0&&i+rl[i]<len&&s[i-rl[i]]==s[i+rl[i]])    rl[i]++;if (i+rl[i]-1>maxright) {maxright=i+rl[i]-1;pos=i;} if (rl[i]==i+1) q.push(i);}}int main() {int i,j,n,lena,len;string a;cin >> a;lena=a.length();for (n=lena;n>=1;n--) {len=n;s[0]='#';memset(sum,0,sizeof(sum));for (i=0;i<len;i++) {s[i*2+1]=a[i];s[i*2+2]='#';}len*=2;len++;manacher(len);q.pop();while (!q.empty()) {int now=q.front();int l=now-(now+1)/2,r=now+(now+1)/2;sum[now]=sum[l]+1;ans[sum[now]]++;q.pop(); }a=a.substr(1,len);}int sum=0;for (i=lena-1;i>=1;i--) {ans[i]+=ans[i+1];}for (i=1;i<=lena;i++) {printf("%d",ans[i]);if (i!=lena) printf(" ");}return 0;}




原创粉丝点击