http://codeforces.com/contest/490/problem/A
A 题 贪心水题
A. Team Olympiad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputThe 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?
InputThe 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.
OutputIn 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 7 1 3 1 3 2 1 2 output 2 3 5 2 6 7 4 input 4 2 1 1 2 output 0#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n,m;
int t;
int a[5];
int b[5005];
int main ()
{
while (scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
b[i]=t;
if(t == 1)
a[1]++;
if(t == 2)
a[2]++;
if(t == 3)
a[3]++;
}
int ans = min (a[1],min(a[2],a[3]));
printf("%d\n",ans);
for(int i=1 ;i<=ans;i++)
{
for(int j =1 ;j<=5005;j++)
{
if(b[j] == 1)
{
printf("%d ",j);
b[j] = 0;
break;
}
}
for(int j =1 ;j<=n;j++)
{
if(b[j] == 2)
{
printf("%d ",j);
b[j] = 0;
break;
}
}
for(int j =1;j<=n;j++)
{
if(b[j] == 3)
{
printf("%d\n",j);
b[j] = 0;
break;
}
}
}
}
return 0;
}
http://codeforces.com/contest/490/problem/B
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.
InputThe 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.
OutputPrint 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 4 92 31 0 7 31 0 7 141 output 92 7 31 141 NoteThe picture illustrates the queue for the first sample.
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n;
int ne[1000010],pre[1000010],flog[1000010];
int ans [1000010];
int a,b;
int main ()
{
while (cin>>n)
{
memset (flog,0,sizeof(flog));
memset (ne,-1,sizeof(ne));
memset (pre,-1,sizeof(pre));
memset (ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
cin>>a>>b;
ne[a] = b;
pre[b] = a;
flog[a] = 1;
flog [b] = 1;
}
int tmp = 0;
int k = 2;
while (1)
{
ans[k]=ne[tmp];
flog[tmp]=0;
k+=2;
tmp = ne[tmp];
if(tmp <= 0)
break;
}
int cnt ;
for(int i=1 ;i<=1000000;i++)
{
if ( flog[i] )
{
cnt = i;
break;
}
}
tmp = cnt ;
while (1)
{
if (pre[tmp] < 0)
break;
tmp = pre[tmp];
}
k=1;
while (1)
{
ans[k] = tmp;
k+=2;
tmp = ne[tmp];
if (tmp<=0)
break;
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}
http://codeforces.com/contest/490/problem/C
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.
InputThe first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to106 digits. The second line contains a pair of space-separated positive integers a, b (1 ≤ a, b ≤ 108).
OutputIn 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 116401024 97 1024 output YES 11640 1024 input 284254589153928171911281811000 1009 1000 output YES 2842545891539 28171911281811000 input 120 12 1 output NO#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<utility>
char s[1000000 + 5];
long long a,b;
int ok_a[1000000 + 5],ok_b[1000000 + 5];
int main ()
{
while (scanf("%s",s+1)!=EOF)
{
int len = strlen(s+1);
scanf("%I64d %I64d",&a,&b);
long long tmp ,t;
tmp = 0;
for(int i=1;i<=len;i++)
{
tmp = (tmp*10 + s[i] - '0')%a;
if (tmp == 0)
ok_a[i] = 1;
else
ok_a[i] = 0;
}
tmp = 0,t=1;
for(int i = len;i>=1;i--)
{
tmp = ( tmp + t*( s[i] - '0') )%b;
t = t*10%b;
if (tmp == 0 )
ok_b[i] = 1;
else
ok_b[i] = 0;
}
int ans ;
int ok=0;
int i;
for (i = 2; i <= len; ++i)
if (ok_a[i - 1] && ok_b[i] && s[i] != '0') break;
if (i == len + 1)
{
puts("NO");
return 0;
}
puts("YES");
for (int j = 1; j < i; ++j)
putchar(s[j]);
puts("");
for (int j = i; j <= len; ++j)
putchar(s[j]); puts("");
return 0;
}
return 0;
}