您的位置:首页 > 大数据 > 人工智能

Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs

2014-01-19 22:18 1321 查看
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.

解题报告:

此题为贪心,每次从两头拿到最大的数即可。

#include<stdio.h>
int s[1010];
int main(){
int n,i,maxn,minn,a,b,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i]);
maxn=n-1;minn=0;t=0;a=0;b=0;
while(n){
if(t%2==0){
if(s[maxn]>s[minn]){
a+=s[maxn];
maxn--;
}
else{
a+=s[minn];
minn++;
}
}
else{
if(s[maxn]>s[minn]){
b+=s[maxn];
maxn--;
}
else{
b+=s[minn];
minn++;
}
}
n--;t++;
}
printf("%d %d\n",a,b);
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


解题报告:
此题为找到一个最长的楼梯序列。那么最长可以一直上升,一直下降,又上升,又下降。所以可以先排序。然后可以找到一条最长的上升。然后在反向寻找一条最长的下降(使用过的数就不在使用),然后合起来即可。

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int s1[100010],s2[100010];
int main(){
int i,j,t,n;
scanf("%d",&t);
memset(s1,0,sizeof(s1));
for(i=0;i<t;i++)
scanf("%d",&s1[i]);
sort(s1,s1+t);
j=0;
for(i=0;i<t;i++){
if(s1[i]!=s1[i+1]){
s2[j++]=s1[i];
s1[i]=-1;
}
}
for(i=t-1;i>=0;i--){
if(s1[i]!=-1&&s2[j-1]!=s1[i])
s2[j++]=s1[i];
}
printf("%d\n%d",j,s2[0]);
for(i=1;i<j;i++)
printf(" %d",s2[i]);
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 排序