好惨啊只水掉两题,C题调了半天WA,结果早上起来发现是一个地方少取模。改了秒过了。
感觉当时有点急躁。A,B都没看清题目WA了次。C题也是下标的处理细节自己比较乱。。写起来就乱了
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
A:两个人玩游戏,可以从头或从尾取数字,每个人都尽量取大,求最后每个人数字和。
思路:模拟就可以了。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int n, num[1005]; bool cmp(int a, int b) { return a > b; } int main() { int ans1 = 0, ans2 = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); int l = 0, r = n - 1, j = 0; while (l <= r) { if (num[l] > num[r]) { if (j % 2 == 0) ans1 += num[l++]; else ans2 += num[l++]; } else { if (j % 2 == 0) ans1 += num[r--]; else ans2 += num[r--]; } j++; } printf("%d %d\n", ans1, ans2); 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
思路:构造问题,数字最大5000,可以开一个数组来记录数字,然后从小往上找,个数为2的用掉,在从上往下找,个数为1的用掉。注意处理最大的那个数字的细节就可以了,
代码:
#include <stdio.h> #include <string.h> #define max(a,b) ((a)>(b)?(a):(b)) const int N = 100005; int n, num, vis[5005], Max, ans; void init() { ans = 0; scanf("%d", &n); Max = 0; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%d", &num); Max = max(Max, num); vis[num]++; } } void solve() { int i, flag = 0, res[N], resn = 0; if (vis[Max] >= 2) flag = 1; for (i = 0; i <= Max; i++) { if (vis[i] >= 2) { vis[i]--; ans++; res[resn++] = i; } } if (flag) Max--; for (i = Max; i >= 0; i--) { if (vis[i] >= 1) { vis[i]--; ans++; res[resn++] = i; } } printf("%d\n", ans); for (i = 0; i < resn - 1; i++) printf("%d ", res[i]); printf("%d\n", res[resn - 1]); } int main() { init(); solve(); return 0; }
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.
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.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the 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
1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
思路:由于复制长度最长10^5,只要开一个10^5的数组来存放前10^5的数字,就可以了。剩下的数字去模拟过程找出下标。用一个now来表示当前找到第几个。由于n个数肯定是递增的,所以只要从左往右一个个找过去,对应m操作一共只需要找一遍,复杂度O(m) + O(n)足以。
代码:
#include <stdio.h> #include <string.h> const int N = 100005; __int64 num[N]; int sn = 0, ansn = 0, save[N], ans[N], m, n; struct CZ { int v, x; __int64 l, c; } cz[N]; void init() { scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%I64d", &cz[i].v); if (cz[i].v == 1) scanf("%I64d", &cz[i].x); else scanf("%I64d%I64d", &cz[i].l, &cz[i].c); } scanf("%d", &n); for (int j = 0; j < n; j++) scanf("%I64d", &num[j]); } void table() { for (int i = 0; i < m; i++) { if (cz[i].v == 1) { save[sn++] = cz[i].x; if (sn == 100000) return; } else { for (int k = 0; k < cz[i].c; k++) { for (int j = 0; j < cz[i].l; j++) { save[sn++] = save[j]; if (sn == 100000) return; } } } } } void solve() { table(); int j = 0, i = 0; __int64 now = 0; while(i < n) { while(j < m) { if (cz[j].v == 1) { now ++; if (now == num[i]) { ans[ansn++] = cz[j].x;i++; if (i >= n) return; j++; break; } j++; } else { __int64 nn = now; now += cz[j].l * cz[j].c; if (now >= num[i]) { while (i < n) { if (num[i] <= now) { ans[ansn++] = save[(num[i++] - nn - 1) % cz[j].l % 100000]; if (i >= n) return; } else { j++; break; } } } else { j++; } } } } } int main() { init(); solve(); for (int i = 0; i < ansn - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[ansn - 1]); return 0; }