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.
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.
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.
4 4 1 2 10
12 5
7 1 2 3 4 5 6 7
16 12
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.
题意:给你n个数字,S和D同学每次只能从最左或者最右取数,而且取其中较大的数,S先取,D后取,分别输出S和D取数总和
思路:按照题意模拟就好~
#include<stdio.h>
#define N 1000
int num[N+10];
int main()
{
int sum1,sum2,n,m;
while(scanf("%d",&m)!=EOF)
{
sum1 = sum2 = 0;
for(int i = 1; i <= m; i ++)
scanf("%d",&num[i]);
int count = 1;
n = m;
for(int j = 1; j <= m; j ++)
{
if(num[n]>num[count])
{
if(j%2)
sum1 += num[n];
else
sum2 += num[n];
n--;
}
else
{
if(j%2)
sum1 += num[count];
else
sum2 += num[count];
count ++;
}
}
printf("%d %d\n",sum1,sum2);
}
return 0;
}
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:
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?
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.
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
5 1 2 3 4 5
5 5 4 3 2 1
6 1 1 2 2 3 3
5 1 2 3 2 1
思路:vis数组存储输入数出现的次数,max存n个数中的最大值,先递增存储小于最大值maxx且vis数组大于等于1的数,vis数组值减1,将maxx值单独存入数组,再递减存储小于最大值且vis数组大于等于1的数。
总结:这道题,也是在小伙伴的指导下自己才完成了。纯暴力,但是,还是不会,蓝桥杯也是很多暴力题,所以,自己的路还很远呐~~emm,昨天因为这道题,被cf坑惨呐,语言交错一直tle,大半夜修仙,最后还是隔壁大佬发现的问题,QAQ,默默感谢一波隔壁大佬救我于水火之中
#include<stdio.h>
#include<string.h>
#define N 100000
int vis[N+10],num[N+10];
int main()
{
int n,number,k,max;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
max=-0x3f3f3f3f;
for(int i = 0; i < n; i ++)
{
scanf("%d",&number);
if(number > max)
max = number;
vis[number] ++;
}
k = 0;
for(int i = 0; i < max; i ++)
{
if(vis[i])
num[k++] = i;
}
num[k++] = max;
for(int i = max-1; i >= 0; i--)
if(vis[i])
num[k++] = i;
printf("%d\n", k);
printf("%d",num[0]);
for(int i = 1; i < k; i ++)
printf(" %d",num[i]);
printf("\n");
}
return 0;
}