TI? =? 3, ifI-Th child is good at PEEach child happens to be good at exactly one of these three subjects.
The Team Scientific Decathlon Olympus IAS 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 Olympus IAD? How shoshould the teams be formed for that?
InputThe first line contains integerN(1? ≤?N? ≤? 5000)-the number of children in the school. The second line containsNIntegersT1 ,?T2 ,?...,?TN(1? ≤?TI? ≤? 3), whereTIDescribes the skill ofI-Th child.
OutputIn the first line output integerW-The largest possible number of teams.
Then printWLines, 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 1NIn the order of their appearance in the input. Each child must particle 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 valueWEqual to 0.
Sample test (s) input71 3 1 3 2 1 2
Output23 5 26 7 4
Input42 1 1 2
Output0
Code:#include
#include
#include
#include#include
#include
#include
#include
#include
#define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;priority_queue
,greater
>Q;const int maxn=5000+10;int n;int t1[maxn],t2[maxn],t3[maxn];int main(){ int x1,x2,x3,x,ans; while(~scanf("%d",&n)) { x1=x2=x3=0; for(int i=1;i<=n;i++) { scanf("%d",&x); if(x==1) t1[++x1]=i; else if(x==2) t2[++x2]=i; else t3[++x3]=i; } ans=min(x1,x2); ans=min(ans,x3); if(ans==0) printf("%d\n",0); else { x=0; printf("%d\n",ans); for(int i=1;i<=ans;i++) { ++x; printf("%d %d %d\n",t1[x],t2[x],t3[x]); } } } return 0;}
Question 2:It is to give the successor of each person and then ask what the final order is ..
Based on the given data, the second and the second to the last can be introduced. However, when the number is an even number, it cannot be pushed. In fact, it can be based on the first two releases.
All sequences, because according to the first can be introduced 1, 3, 5, 7, 2 * n + 1, according to 2 can be introduced 2, 4, 6, 8, 2 * n... Just store it with an adjacent table.
Question:
B. Queuetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDuring the lunch break allNBerland 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 integerN(2? ≤?N? ≤? 2. 105)-the number of students in the queue.
ThenNLines follow,I-Th line contains the pair of integersAI,?BI(0? ≤?AI,?BI? ≤? 106), whereAIIs the ID number of a person in front of a student andBIIs 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 sequenceNIntegersX1 ,?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) input492 310 731 07 141
Output92 7 31 141
NoteThe picture into strates the queue for the first sample.
Code:#include
#include
#include
#include#include
#include
#include
#include
#include
#define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;priority_queue
,greater
>Q;const int maxn=2000000+10;struct Edge{ int to,next;}edge[maxn<<2];int ans[maxn],head[maxn],cnt;bool vis[maxn];void add_edge(int x,int y){ edge[++cnt].to=y; edge[cnt].next=head[x]; head[x]=cnt;}int num1[1000005],num2[1000005];int main(){ int u,v,n,x,xx; while(~scanf("%d",&n)) { memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis)); memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); cnt=0; for(int i=1;i<=n;i++) { scanf("%d %d",&u,&v); if(u==0) ans[2]=v; num1[u]--; num2[v]--; if(u==0||v==0) continue; add_edge(u,v); } for(int i=0;i<1000005;i++) { if(num1[i]==-1&&num2[i]==0) { ans[1]=i; break; } } x=1; xx=head[ans[1]]; while(x