Codeforces Round #224 (Div. 2)

两题还被最终评测卡了一题,怒跪哎。。too young

事后4题第五题不懂。。

A. Ksenia and Pan Scales
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.

The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

Input

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.

The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.

It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

Output

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input.

If there are multiple answers, print any of them.

Sample test(s)
input
AC|T
L
output
AC|TL
input
|ABC
XYZ
output
XYZ|ABC
input
W|T
F
output
Impossible
input
ABC|
D
output
Impossible

题意:求两边能否放字母相同。

思路:简单的模拟。判断即可

代码:

#include <stdio.h>
#include <string.h>

char str1[105], str2[105];
int l = 0, r = 0, vis[105];

int main() {
    int i;
    memset(vis, -1, sizeof(vis));
    scanf("%s%s", str1, str2);
    int len1 = strlen(str1);
    for (i = 0; i < len1; i++) {
        if (str1[i] == ‘|‘) break;
            vis[str1[i] - ‘A‘] = 0;
            l++;
    }
    i++;
    for (;i<len1;i++) {
        vis[str1[i] - ‘A‘] = 1; r++;
    }
    int len2 = strlen(str2);
    for (i = 0; i < len2; i++)
        if (l < r) {
            l ++;
            vis[str2[i] - ‘A‘] = 0;
        }
        else {
            r++;
            vis[str2[i] - ‘A‘] = 1;
        }
    if (l != r) printf("Impossible\n");
    else {
        for (i = 0; i < 26; i++)
            if (vis[i] == 0) {
                printf("%c", i + ‘A‘);
            }
        printf("|");
        for (i = 0; i < 26; i++)
            if (vis[i] == 1) {
                printf("%c", i + ‘A‘);
            }
        printf("\n");
    }
    return 0;
}

B. Number Busters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arthur and Alexander are number busters. Today they‘ve got a competition.

Arthur took a group of four integers a,?b,?w,?x (0?≤?b?<?w,?0?<?x?<?w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c?=?c?-?1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b?≥?x, perform the assignment b?=?b?-?x, if b?<?x, then perform two consecutive assignments a?=?a?-?1; b?=?w?-?(x?-?b).

You‘ve got numbers a,?b,?w,?x,?c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c?≤?a.

Input

The first line contains integers a,?b,?w,?x,?c (1?≤?a?≤?2·109,?1?≤?w?≤?1000,?0?≤?b?<?w,?0?<?x?<?w,?1?≤?c?≤?2·109).

Output

Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem‘s limits.

题意:求经过多少时间c<=a,c和a都有变换的方式。

思路:推个公式。。c每秒都减1,a在b<x的时候减1,并且b = b + w - x,在b>=x的时候不变化,并且b = b - x..也就是a和c只有在b = b - x的时候差距才会减少1,所以b一共减了(c-a)次x, 设加了k次w - x.那么得到b最后的值为b - x * (c - a) + k * (w - x).然后b最后的值+x 必然不小于x(因为b >= x 才是执行b = b - x)..所以得到公式b - x * (c - a) + k *(w - x) + x >= x ==> k = (ceil)((b - x * (c - a))/(w - x)).

#include <stdio.h>
#include <string.h>
#include <math.h>

long long a, b, w, x, c;

long long solve() {
	if (c <= a) return 0;
	long long ans = (long long)ceil((x * (c - a) - b) * 1.0 / (w - x)) + (c - a);
	return ans;
}

int main() {
	scanf("%lld%lld%lld%lld%lld", &a, &b, &w, &x, &c);
	printf("%lld\n", solve());
	return 0;
}

C. Arithmetic Progression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1,?a2,?...,?an of length n, that the following condition fulfills:

a2?-?a1?=?a3?-?a2?=?a4?-?a3?=?...?=?ai?+?1?-?ai?=?...?=?an?-?an?-?1.

For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.

Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n?+?1 cards to make an arithmetic progression (Alexander has to use all of his cards).

Arthur has already bought a card but he hasn‘t written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.

Input

The first line contains integer n (1?≤?n?≤?105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander‘s cards. The numbers are positive integers, each of them doesn‘t exceed 108.

Output

If Arthur can write infinitely many distinct integers on the card, print on a single line -1.

Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).

题意:有一些数,你要加上一个数使之成为等差序列,求所有答案。

思路:分类讨论,去判断即可。

代码:有点挫

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

const int N = 100005;

int n, num[N];
set<int> ans;
void init() {
	ans.clear();
	for (int i = 0; i < n; i++)
		scanf("%d", &num[i]);
	sort(num, num + n);
}

bool solve() {
	if (n == 1) {
		return false;
	}
	if (n == 2) {
		int d = num[1] - num[0];
		ans.insert(num[0] - d);
		ans.insert(num[1] + d);
		if (d % 2 == 0)
			ans.insert(num[0] + d / 2);
		return true;
	}
	else {
		int d, dd, nn = 0; d = num[1] - num[0];
		map<int, int> vv; vv.clear();
		int save[N], saven = 0; save[saven++] = d;
		for (int i = 1; i < n; i++) {
			if (d != num[i] - num[i - 1]) {
				d = num[i] - num[i - 1];
				save[saven++] = d;
				nn++;
			}
			d = num[i] - num[i - 1];
			vv[d]++;
		}
		if (nn > 2) {
			return true;
		}
		else {
			if (nn == 0) {
				int d = num[1] - num[0];
				ans.insert(num[0] - d);
				ans.insert(num[n - 1] + d);
			}
			else {
				sort(save, save + saven);
				for (int k = saven - 1; k >= 0; k--) {
					if (vv[save[k]] == 1) {
						for (int ii = 1; ii < n; ii ++) {
							int dd = num[ii] - num[ii - 1];
							if (dd == save[k]) {
								if (dd % 2) return true;
								int ddd = dd / 2;
								for (int l = 0; l < saven; l++) {
									if (l == k) continue;
									if (save[l] != ddd) return true;
								}
								ans.insert(num[ii] - dd / 2);
								return true;	
							}
						}
					}
				}
			}
		}
	}
}

int main() {
	scanf("%d", &n);
	init();
	if(!solve()) printf("-1\n");
	else {
		printf("%d\n", ans.size()); int bo = 0;
		for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) {
			if (bo++) printf(" ");
			printf("%d", *it);
		}
		if (ans.size() != 0)
			printf("\n");
	}
	return 0;
}

D. Ksenia and Pawns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ksenia has a chessboard of size n?×?m. Each cell of the chessboard contains one of the characters: "<", ">", "^", "v", "#". The cells that contain character "#" are blocked. We know that all chessboard cells that touch the border are blocked.

Ksenia is playing with two pawns on this chessboard. Initially, she puts the pawns on the chessboard. One cell of the chessboard can contain two pawns if and only if the cell is blocked. In other cases two pawns can not stand in one cell. The game begins when Ksenia put pawns on the board. In one move, Ksenia moves each pawn to a side adjacent cell in the direction of arrows painted on the cell on which the corresponding pawn sits (if the pawn sits on "#", it does not move). Assume that Ksenia moves pawns simultaneously (see the second test case).

Of course, Ksenia plays for points. How can one calculate the points per game? Very simply! Let‘s count how many movements the first pawn made and how many movements the second pawn made, sum these two numbers — it will be the resulting score of the game.

Ksenia wonders: what is the maximum number of points she can earn (for that, she should place the pawns optimally well early in the game). Help her and find that number.

Input

The first line contains two integers, n and m (1?≤?n,?m?≤?2000) — the sizes of the board. Each of the following n lines contains mcharacters – the board‘s description. Each character is one of the characters: "<", ">", "^", "v", "#".

It is guaranteed that the border cells of the table are blocked cells (with character "#").

Output

If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get.

题意:有2个卒。放在地图上,问最多可以走几步,不能两个卒子走到同一个位置。如果可以走无限步输出-1

思路:逆向思维的DFS。从#往里找路线,记录最大路线和次大路线,然后去计算答案即可,如果所有#找完,格子数不足n*m就说明内部肯定有环。为-1

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
const int d[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
const char v[5] = {"^v><"};
const int N = 2005;
int n, m, max1, max2, num, dp[N][N];
char g[N][N];

void init() {
    num = max1 = max2 = 0;
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%s", g[i]);
}

void Max(int M) {
    if (M > max1) {
        max2 = max1; max1 = M;
    }
    else if (M > max2) max2 = M;
}

int dfs(int x, int y, char c) {
    if (x < 0 || x >= n || y < 0 || y >= m || c != g[x][y]) return 0;
    num++;
    if (dp[x][y] != -1) return dp[x][y];
    int Max = 0;
    for (int i = 0; i < 4; i++) {
        int t = dfs(x + d[i][0], y + d[i][1], v[i]);
        Max = max(Max, t);
    }
    dp[x][y] = Max + 1;
    return Max + 1;
}

void solve() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (g[i][j] == ‘#‘) {
                num++;
                for (int k = 0; k < 4; k++) {
                    Max(dfs(i + d[k][0], j + d[k][1], v[k]));
                }
            }
        }
    if (num < n * m) printf("-1\n");
    else printf("%d\n", (max1 == max2 ? max1 + max2 : max1 + max(max1 - 1, max2)));
}

int main() {
    init();
    solve();
    return 0;
}


Codeforces Round #224 (Div. 2)

上一篇:LINUX定制命令提示符


下一篇:使用 Sandcastle 生成代码帮助文档