Codeforces Round #223 (Div. 2)

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.


题意:2个人轮流在一个数列取数,每轮都取第一个和最后一个比较大的数,问最后2个人的数的和

题解:直接用个数组模拟一下就行了


#include
#include
#include
#include
int a[1008],dp[100000][100000];
int main()
{
    int i,n,l,r,di,se;

    while(scanf("%d",&n)>0)
    {
        di=se=0;
        for(i=0;ia[r])
            {
                if(i&1) di+=a[l++];
                else se+=a[l++];
            }
            else
            {
                if(i&1) di+=a[r--];
                else se+=a[r--];
            }
        }
        printf("%d %d\n",se,di);
    }

    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


题意:给n个数,取一些出来,排成先严格递增再严格递减的序列

题解:把n个数升序排序,顺着扫一遍,若比上一个取出来的数大就取出来并标记,再倒着扫一遍,若比上一个取出来的数小就取出来,然后输出就行了

#include
#include
#include
#include
#include
using namespace std;
int a[100008],flag[100008],b[100008];
int main()
{
    int i,n,last,all;

    while(scanf("%d",&n)>0)
    {
        for(all=i=0;ia[last])
            {
                b[all++]=a[i];
                last=i;
                flag[i]=0;
            }
        }
        for(i=n-1;i>=0;i--)
        {
            if(flag[i]&&a[i]


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 cincout 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

题意:给定m次操作,操作有1,2。1的话代表把x加入序列末,2代表把序列前缀长度为l,复制c遍加到末尾,然后输入n个数,输出n个数下标对应的数字。

题解:考虑到每次复制都只需取前l个数,而l小于10^5,所以先把前10^5个数打出来,然后在m个操作上面逐个模拟,若要查找的数是这个操作制造出来的,若是1的话,直接输出,若是2的话,就mod l 然后就是刚才打印的那10^5的数组相应的数了


#include
#include
#include
#include
#define MAX 1000000
struct point{
    int sta,l,c,x;
}p[MAX+8];
long long a[MAX+8];
int save[MAX+8];
int main()
{
    int i,j,k,n,m,all;
    long long now,id,temp;

    while(scanf("%d",&m)>0)
    {
        for(i=0;i


你可能感兴趣的:(codeforces,ACM之路(c/c++),ACM,codeforces,cf223)