您的位置:首页 > 其它

Codeforces #223 Div.2

2014-01-13 16:45 417 查看
A. Sereja and Dima

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards
are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards
by the end of the game, wins.

Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.

Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.

Input

The first line contains integer n (1 ≤ n ≤ 1000) —
the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.

Output

On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.

Sample test(s)

input
4
4 1 2 10


output
12 5


input
7
1 2 3 4 5 6 7


output
16 12


Note

In the first sample Sereja will take cards with numbers 10 and 2, so
Sereja's sum is 12. Dima will take cards with numbers 4 and 1,
so Dima's sum is 5.

题意:判断最前与最后,取较大值相加输出。

题解:水题。

Code
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int n,i,a[1005];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int start=0,end=n-1,p=0,q=0,flag=0;
while(start<end)
{
if(a[start]>a[end]&&flag==0)
{
p+=a[start];
start++;
flag=1;
}
else if(a[start]<a[end]&&flag==0)
{
p+=a[end];
end--;
flag=1;
}
else if(a[start]>a[end]&&flag!=0)
{
q+=a[start];
start++;
flag=0;
}
else if(a[start]<a[end]&&flag!=0)
{
q+=a[end];
end--;
flag=0;
}
}
if(flag==0) p+=a[end];
else q+=a[end];
printf("%d %d\n",p,q);
return 0;
}


B. Sereja and Stairs

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Sereja loves integer sequences very much. He especially likes stairs.

Sequence a1, a2, ..., a|a| (|a| is
the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|),
that the following condition is met:
a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.

For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.

Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the
table?

Input

The first line contains integer m (1 ≤ m ≤ 105) —
the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000) —
the numbers on the Sereja's cards.

Output

In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.

Sample test(s)

input
5
1 2 3 4 5


output
5
5 4 3 2 1


input
6
1 1 2 2 3 3


output
5
1 2 3 2 1


题意:给一组数,用其中尽可能多的数来组成一个先递增再递减的数列。要求输出采用的数字数目以及所能构成的最大数组。

题解:每个数字最多使用两次,另外最大数只能使用一次。当某个数只能使用一次的情况下,应把它放在后面的递减数列以满足最大输出。

代码初编译成功最后Wrong answer on test 30,原因未找出。

Code

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int n,i,k,a[5005]={0};
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
a[k]++;
}
int sum=0,max=1,min=5000;
for(i=0;i<5005;i++)
{
if(a[i]>=2) {sum+=2;if(i>max) max=i;if(i<min) min=i;}
if(a[i]==1) {sum++;if(i>max) max=i;if(i<min) min=i;}
}
if(a[max]>=2) sum--;
printf("%d\n",sum);
for(i=min;i<max;i++)
if(a[i]>=2) {printf("%d ",i);a[i]--;}
printf("%d ",max);
for(i=max-1;i>min;i--)
if(a[i]>0) printf("%d ",i);
printf("%d\n",min);
return 0;
}


C. Sereja and Prefixes

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.

Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence
or takes l first elements of the current sequence and adds them c times
to the end. More formally, if we represent the current sequence as a1, a2, ..., an,
then after we apply the described operation, the sequence transforms intoa1, a2, ..., an[, a1, a2, ..., al] (the
block in the square brackets must be repeated c times).

A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.

Input

The first line contains integer m (1 ≤ m ≤ 105) —
the number of stages to build a sequence.

Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding
one number to the end of the sequence, in this case the line contains integer xi (1 ≤ xi ≤ 105) —
the number to add. Type 2 means copying a prefix of length li to
the end ci times,
in this case the line further contains two integers li, ci (1 ≤ li ≤ 105, 1 ≤ ci ≤ 104), li is
the length of the prefix, ci is
the number of copyings. It is guaranteed that the length of prefix li is
never larger than the current length of the sequence.

The next line contains integer n (1 ≤ n ≤ 105) —
the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than
zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of
the sequence.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams
or the %I64dspecifier.

Output

Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.

Sample test(s)

input
6
1 1
1 2
2 2 1
1 3
2 5 2
1 4
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16


output
1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4


题意:首先输入stage数量,之后输入每个stage的类型:1为在数列后添加新的数字,2为取数列第1到第l个数字重复c遍添加在数列后面。再输入一个数表示要求输出的数字个数,再下一行为输出数字所在的位置。

题解:好吧。我不会。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: