CodeForces Round 193 Div2
6931 단어 codeforces
A. Down the Hatch!time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEverybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice!
Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index bi from 0 to n-1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index bi, then this person acted like that:
he pointed at the person with index (bi+1) mod n either with an elbow or with a nod (x mod y is the remainder after dividing x by y); if j≥4 and the players who had turns number j-1, j-2, j-3, made during their turns the same moves as player bi on the current turn, then he had drunk a glass of juice; the turn went to person number (bi+1) mod n.
The person who was pointed on the last turn did not make any actions.
The problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him.
You can assume that in any scenario, there is enough juice for everybody.
InputThe first line contains a single integer n (4≤n≤2000) — the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns.
OutputPrint a single integer — the number of glasses of juice Vasya could have drunk if he had played optimally well.
Sample test(s)Input4abbbaOutput1Input4abbabOutput0NoteIn both samples Vasya has got two turns — 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like "abbbb". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different.
This question is very simpily.We can just enumerate all the turns for Vasya and check if it meet the conidition.
#include<stdio.h>
#include<string.h>
int N;
char ch[2025];
bool judge(int x)
{
if (x<3) return false;
if (ch[x-1]==ch[x-2] && ch[x-2]==ch[x-3]) return true;
if (N==1) return true;
if (N==2 && ch[x-1]==ch[x-3]) return true;
if (N==3 && ch[x-1]==ch[x-2]) return true;
return false;
}
int main()
{
int i,Max=0;
scanf("%d",&N);
scanf("%s",ch);
for (i=N;i<strlen(ch);i+=N)
if (judge(i)) Max++;
printf("%d
",Max);
return 0;
}
B. Maximum Absurditytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputReforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1≤a≤b≤n-k+1,b-a≥k) and sign all laws with numbers lying in the segments [a; a+k-1] and [b; b+k-1] (borders are included).
As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
InputThe first line contains two integers n and k (2≤n≤2·105, 0<2k≤n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1,x2,...,xn — the absurdity of each law (1≤xi≤109).
OutputPrint two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a+k-1] and [b; b+k-1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
Sample test(s)Input5 23 6 1 1 6Output1 4Input6 21 1 1 1 1 1Output1 3NoteIn the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3+6+1+6=16.
In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1+1+1+1=4.
Still easy.s[i] means the sum of the value from law i to law i+k-1.f[i] means the maximum value of s[j] between i and N.
#include<stdio.h>
#include<string.h>
__int64 w[200025],s[200025],f[200025];
int main()
{
int N,K,i;
scanf("%d%d",&N,&K);
for (i=1;i<=N;i++) scanf("%I64d",&w[i]);
s[1]=0;
for (i=1;i<=K;i++) s[1]+=w[i];
for (i=2;i+K-1<=N;i++) s[i]=s[i-1]-w[i-1]+w[i+K-1];
f[N-K+1]=s[N-K+1];
for (i=N-K;i>=1;i--)
if (s[i]>f[i+1]) f[i]=s[i];
else f[i]=f[i+1];
__int64 Max=0,tmp;
int a,b;
for (i=1;i+2*K-1<=N;i++)
if (s[i]+f[i+K]>Max) Max=s[i]+f[i+K];
for (i=1;i+2*K-1<=N;i++)
if (s[i]+f[i+K]==Max)
{
a=i;
Max-=s[i];
break;
}
for (i=a+K;i+K-1<=N;i++)
if (s[i]==Max)
{
b=i;
break;
}
printf("%d %d
",a,b);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Codeforces Round #715 Div. 2C The Sports Festival: 구간 DP전형구간 DP의 초전형. 이하, 0-indexed. 입력을 정렬하여 어디서나 시작하고 최적으로 좌우로 계속 유지하면 좋다는 것을 알 수 있습니다. {2000})$의 주문이 된다. 우선, 입력을 소트하여 n개의 요소를 $...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.