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.
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.
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;
}
题意:给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]
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.
Output
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
Sample test(s)
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