codeforces round 279 div2解题报告

来源:互联网 发布:手机炒贵金属软件 编辑:程序博客网 时间:2024/05/29 13:43
codeforces round 279 div2解题报告
A. Team Olympiad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The School №0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti:

  • ti = 1, if the i-th child is good at programming,
  • ti = 2, if the i-th child is good at maths,
  • ti = 3, if the i-th child is good at PE

Each child happens to be good at exactly one of these three subjects.

The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.

What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?

Input

The first line contains integer n (1 ≤ n ≤ 5000) — the number of children in the school. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 3), where ti describes the skill of the i-th child.

Output

In the first line output integer w — the largest possible number of teams.

Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them.

If no teams can be compiled, print the only line with value w equal to 0.

Sample test(s)
input
71 3 1 3 2 1 2
output
23 5 26 7 4
input
42 1 1 2
output
0
有n个人,每个人只会三个技能中的一个,要求3个会的技能不同的人组成一个队伍,问最多能组成多少个队伍。记录每种技能的人数,三种技能会的最少的人数就是组成的队数的最大值,记录的时候记录下会该技能的人,直接输出即可。代码如下:
/*************************************************************************> File Name: a.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月23日 星期日 17时09分20秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;#define N 5010int val[4][N], num;int main(int argc, char *argv[]){int n;while (scanf("%d", &n) != EOF) {int cnt[5];clr(cnt, 0);for (int i = 0; i < n; ++i) {scanf("%d", &num);val[num][cnt[num]++] = i + 1;}int mx = INF;for (int i = 1; i <= 3; ++i) {mx = min(cnt[i], mx);}printf("%d\n", mx);for (int i = 0; i < mx; ++i) {printf("%d %d %d\n", val[1][i], val[2][i], val[3][i]);}}return 0;}

B. Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.

Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).

After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.

Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.

Input

The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue.

Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.

The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.

Output

Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.

Sample test(s)
input
492 310 731 07 141
output
92 7 31 141 
Note

The picture illustrates the queue for the first sample.


一个队伍,有n个人,每个人都有一个唯一的编号(大于0小于等于10^6),现在每个人写下它前边的人的编号和它后边的人的编号(如果相邻没有人就用0代替),n个人写下的n对数ai,bi表示这个人前边和后边人的编号,,顺序是任意的,n个人的序列。因为编号小于等于10^6所以,可以使用两个数组a[k]表示编号为k的人的下下位,b[k]表示编号为k的上上位,不存在用-1表示(就是这里在比赛的时候写错了,我用了0表示不存在,编号用0表示不存在,所以就区分不出来)。那么找出第一位的编号也就是找到这样的k,b[k]为-1,a[k]不为-1,那么这个就是第一位,然后就可以依次找到第3,5,...,等,位,同理,找到第二位的编号,然后就可以找出2,4,...,等,这就找到了整个队伍的序列。
我这里写的是区分了偶数和奇数,其实不用区分也是可以的,照着上边的思路就可以了。
代码如下:
/*************************************************************************> File Name: b.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月23日 星期日 17时18分50秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;#define N 1000010int a[N], b[N], ans[N], num1[N], num2[N];int main(int argc, char *argv[]){int n;while (scanf("%d", &n) != EOF) {//不存在的值一定要和其他可能的取值区分开clr(a, -1);clr(b, -1);for (int i = 0; i < n; ++i) {int u, v;scanf("%d%d", &num1[i], &num2[i]);u = num1[i];v = num2[i];a[u] = v;b[v] = u;}int tmp = a[0];int cnt = 2;while (cnt <= n) {ans[cnt] = tmp;tmp = a[tmp];cnt += 2;}if (n % 2 == 0) {tmp = b[0];cnt = n - 1;while (cnt >= 1) {ans[cnt] = tmp;tmp = b[tmp];cnt -= 2;}} else {int num = 0;for (int i = 0; i < n; ++i) {if (a[num1[i]] != 0 && b[num1[i]] < 0) {num = num1[i];break;}}tmp = num;cnt = 1;while (cnt <= n) {ans[cnt] = tmp;tmp = a[tmp];cnt += 2;}}for (int i = 1; i <= n; ++i) {printf("%d%c", ans[i], (i == n) ? '\n' : ' ');}}return 0;}

C. Hacking Cypher
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.

Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!

Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.

Help Polycarpus and find any suitable method to cut the public key.

Input

The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106digits. The second line contains a pair of space-separated positive integers ab (1 ≤ a, b ≤ 108).

Output

In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part must be divisible by b. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.

If there is no answer, print in a single line "NO" (without the quotes).

Sample test(s)
input
11640102497 1024
output
YES116401024
input
2842545891539281719112818110001009 1000
output
YES284254589153928171911281811000
input
12012 1
output
NO
将一个给定的数字字符串分成两部分,前一部分可以被a整除,后一部分可以被b整除,并且这两部分数不能有前导0,也不能为0(样例三)。
用两个数组dp1[i]表示下标为0到i的数字模a的余数,dp2[i]表示下标i到len-1的数字模b的余数,这样我们只要找到一个位置k,满足dp1[k]==0&&dp2[k+1]==0&&s2[k+1]!='0'就可以了,当然k取值范围为0到len-2。两个数组也很好求。
代码如下:
/*************************************************************************> File Name: c.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月23日 星期日 18时06分42秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;#define N 1000010int dp1[N], dp2[N];int main(int argc, char *argv[]){int a, b;string s;while (cin >> s) {scanf("%d%d", &a, &b);int i = 0;int len = s.length();dp1[0] = (s[0] - '0') % a;for (i = 1; i < len; ++i) {int tmp = s[i] - '0';dp1[i] = (dp1[i - 1] * 10 + tmp) % a;}dp2[len] = 0;int base = 1;for (int i = len - 1; i >= 0; --i) {int tmp = s[i] - '0';dp2[i] = (base * tmp + dp2[i + 1]) % b;base = (base * 10) % b;}int pos = -1;for (int i = 0; i < len - 1; ++i) {if (dp1[i] == 0 && dp2[i + 1] == 0 && s[i + 1] != '0') {pos = i;break;}}if (pos < 0) {cout << "NO" << endl;} else {cout << "YES" << endl;cout << s.substr(0, pos + 1) << endl;cout << s.substr(pos + 1, len - pos - 1) << endl;}}return 0;}


0 0
原创粉丝点击