A. Group of Students
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score from 1 to m points. We know that c1 schoolchildren got 1 point, c2 children got 2 points, ..., cm children got m points. Now you need to set the passing rate k (integer from 1 to m): all schoolchildren who got less than k points go to the beginner group and those who get at strictly least k points go to the intermediate group. We know that if the size of a group is more than y, then the university won't find a room for them. We also know that if a group has less than x schoolchildren, then it is too small and there's no point in having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from x to y, inclusive.

Help the university pick the passing rate in a way that meets these requirements.

Input

The first line contains integer m (2 ≤ m ≤ 100). The second line contains m integers c1, c2, ..., cm, separated by single spaces (0 ≤ ci ≤ 100). The third line contains two space-separated integers x and y (1 ≤ x ≤ y ≤ 10000). At least one ci is greater than 0.

Output

If it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least x and at most y, print 0. Otherwise, print an integer from 1 to m — the passing rate you'd like to suggest. If there are multiple possible answers, print any of them.

Examples
Input
5
3 4 3 2 1
6 8
Output
3
Input
5
0 3 3 4 2
3 10
Output
4
Input
2
2 5
3 6
Output
0
Note

In the first sample the beginner group has 7 students, the intermediate group has 6 of them.

In the second sample another correct answer is 3.

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int m;
 6 int a[105];
 7 int main()
 8 {
 9     scanf("%d",&m);
10     int he=0;
11     for(int i=1;i<=m;i++)
12     {
13         scanf("%d",&a[i]);
14         he+=a[i];
15         }
16     int x,y;
17     int sum=0;
18     int ans=0;
19     scanf("%d %d",&x,&y);
20     for(int i=1;i<=m;i++)
21     {
22         sum+=a[i];
23         if(sum>y)
24         {   printf("0\n");
25             return 0;
26             }
27         if((sum>=x&&sum<=y)&&(he-sum>=x&&he-sum<=y))
28            { 
29                printf("%d\n",i+1);
30                return 0;
31              }
32         }
33     printf("0\n");
34     return 0;
35 }

 

B. Flag Day
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:

  • overall, there must be m dances;
  • exactly three people must take part in each dance;
  • each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland).

The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance.

You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.

Output

Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.

Examples
Input
7 3
1 2 3
1 4 5
4 6 7
Output
1 2 3 3 2 2 1 
Input
9 3
3 6 9
2 5 8
1 4 7
Output
1 1 1 2 2 2 3 3 3 
Input
5 2
4 1 5
3 1 2
Output
2 3 1 1 3 

题意:n个舞者 m个舞 每个舞蹈由3个不同的舞者组成 并且每个舞者的衣着颜色不同 问你n个舞者的衣着分别为什么 (1,2,3代表三种颜色)
题解:具体看代码 大致思想就是 固定第i位置的舞者的衣着为i 通过交换位置 向上对齐
 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 using namespace std;
 4 int n,m;
 5 int re[100005];
 6 int a[100005][5];
 7 int mp[100005][5];
 8 int aa,bb,cc;
 9 int main()
10 {
11     scanf("%d %d",&n,&m);
12     memset(mp,0,sizeof(mp));
13     scanf("%d %d %d",&aa,&bb,&cc);
14     mp[aa][1]=1;
15     mp[bb][2]=1;
16     mp[cc][3]=1;
17     a[1][1]=aa;a[1][2]=bb;a[1][3]=cc;
18     for(int i=2;i<=m;i++){
19         scanf("%d %d %d",&aa,&bb,&cc);
20         a[i][1]=aa;a[i][2]=bb;a[i][3]=cc;
21         for(int j=1;j<=3;j++){
22          if(mp[aa][j]==1){
23            if(j!=1){
24                swap(a[i][1],a[i][j]);
25            }
26          }
27           if(mp[bb][j]==1){
28                if(j!=2){
29                swap(a[i][2],a[i][j]);
30            }
31          }
32           if(mp[cc][j]==1){
33                if(j!=3){
34                swap(a[i][3],a[i][j]);
35            }
36          }
37       }
38       mp[a[i][1]][1]=1;
39       mp[a[i][2]][2]=1;
40       mp[a[i][3]][3]=1;
41     }
42     for(int j=1;j<=3;j++)
43     {
44         for(int i=1;i<=n;i++)
45         {
46             re[a[i][j]]=j;
47         }
48     }
49     for(int i=1;i<=n;i++)
50         printf("%d ",re[i]);
51     printf("\n");
52     return 0;
53 }

 

C. Knight Tournament
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.

As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:

  • There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.
  • The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.
  • After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.
  • The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament.

You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.

Write the code that calculates for each knight, the name of the knight that beat him.

Input

The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ nli ≤ xi ≤ ri) — the description of the i-th fight.

It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.

Output

Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.

Examples
Input
4 3
1 2 1
1 3 3
1 4 4
Output
3 1 4 0 
Input
8 4
3 5 4
3 7 6
2 8 8
1 8 1
Output
0 8 4 6 4 8 6 1 
Note

Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.

题意:n个人 m场比赛 x战胜了 [l,r]中除了已经被淘汰的和他本身的人 输出每个人是被谁战胜的

题解:set

 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 //code by  drizzle
 4 #include<iostream>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<algorithm>
 8 #include<vector>
 9 #define ll __int64
10 #define PI acos(-1.0)
11 #define mod 1000000007
12 using namespace std;
13 set<int> s;
14 set<int>::iterator it,itt;
15 int n,m;
16 int a[300005];
17 int l,r,x;
18 int main()
19 {
20     scanf("%d %d",&n,&m);
21     memset(a,0,sizeof(a));
22     for(int i=1;i<=n;i++)
23         s.insert(i);
24     for(int i=1;i<=m;i++)
25     {
26         scanf("%d %d %d",&l,&r,&x);
27         it=s.lower_bound(l);
28         for(;(*it)<=r&&it!=s.end();it=itt){
29             a[*it]=x;
30             itt=it;
31             itt++;
32             s.erase(*it);
33         }
34         s.insert(x);
35         a[x]=0;
36     }
37     for(int i=1;i<=n;i++)
38         printf("%d ",a[i]);
39     printf("\n");
40     return 0;
41 }
42 /*
43 11 6
44 1 2 2
45 7 8 7
46 3 4 4
47 6 9 6
48 5 10 10
49 2 11 11
50 */

 

E. Compartments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.

The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.

However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.

After all the swaps each compartment should either have no student left, or have a company of three or four students.

Input

The first line contains integer n (1 ≤ n ≤ 106) — the number of compartments in the carriage. The second line contains n integers a1, a2, ..., an showing how many students ride in each compartment (0 ≤ ai ≤ 4). It is guaranteed that at least one student is riding in the train.

Output

If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.

Examples
Input
5
1 2 2 4 3
Output
2
Input
3
4 1 1
Output
2
Input
4
0 3 0 4
Output
0

  

题意:给你n个数 数的范围为0~4  经过最少的操作(每次改变1) 使得n个数的目标状态为{0,3,4}

题解:恶心分类

     

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<vector>
 7 #define ll __int64
 8 #define PI acos(-1.0)
 9 #define mod 1000000007
10 using namespace std;
11 int n;
12 map<int,int>mp;
13 int exm;
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i=1; i<=n; i++)
18     {
19         scanf("%d",&exm);
20         mp[exm]++;
21     }
22     if(mp[1]>=mp[2])
23     {
24         int len=mp[1]-mp[2];
25         int jishu=mp[2]+(len/3)*2;
26         mp[3]=mp[3]+len/3+mp[2];
27         len=len%3;
28         if(len==0)
29             printf("%d\n",jishu);
30         if(len==1)
31         {
32             if(mp[3]>=1)
33                 printf("%d\n",jishu+1);
34             else if(mp[4]>=2)
35             printf("%d\n",jishu+2);
36             else
37                 printf("-1\n");
38         }
39         if(len==2)
40         {
41             if(mp[4]>=1||mp[3]>=2)
42                 printf("%d\n",jishu+2);
43             else
44                 printf("-1\n");
45         }
46     }
47     else
48     {
49         int len=mp[2]-mp[1];
50         int jishu=mp[1]+(len/3)*2;
51         mp[3]=mp[3]+(len/3)*2+mp[1];
52         len=len%3;
53         if(len==0)
54             printf("%d\n",jishu);
55         if(len==1)
56         {
57             if(mp[4]>=1)
58                 printf("%d\n",jishu+1);
59             else if(mp[3]>=2)
60                 printf("%d\n",jishu+2);
61             else
62                 printf("-1\n");
63         }
64         if(len==2)
65         {
66           printf("%d\n",jishu+2);
67         }
68     }
69     return 0;
70 }