A. Andryusha and Socks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andryusha is an orderly boy and likes to keep things in their place.

Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.

Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?

Input

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.

The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.

It is guaranteed that Andryusha took exactly two socks of each pair.

Output

Print single integer — the maximum number of socks that were on the table at the same time.

Examples
input
1
1 1
output
1
input
3
2 1 1 3 2 3
output
2
Note

In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.

In the second example Andryusha behaved as follows:

  • Initially the table was empty, he took out a sock from pair 2 and put it on the table.
  • Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
  • Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
  • Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
  • Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
  • Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
Thus, at most two socks were on the table at the same time.

 题意:给你n对袜子 现在一只一只的放到桌子上  共2*n个数 当一双袜子同时出现在桌子时则拿走这双袜子 问桌子上最多能有多少只袜子

 题解:标记  水

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 int exm;
16 int mp[100005];
17 int main()
18 {
19     scanf("%d",&n);
20     int now=0;
21     int ans=-1;
22     memset(mp,0,sizeof(mp));
23     for(int i=1; i<=2*n; i++)
24     {
25         scanf("%d",&exm);
26         if(mp[exm]) {
27             mp[exm]--;
28             now--;
29         }
30         else {
31             mp[exm]++;
32             now++;
33         }
34         ans=max(ans,now);
35     }
36     printf("%d\n",ans);
37     return 0;
38 }

 

B. The Meeting Place Cannot Be Changed
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.

At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.

You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.

Input

The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.

The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.

Output

Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.

Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if  holds.

Examples
input
3
7 1 3
1 2 1
output
2.000000000000
input
4
5 10 3 2
2 3 2 4
output
1.400000000000
Note

In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.

 题意:给你n个人的位置以及运动速度 (方向随意) 问最少需要多少时间能够使得所有的人到达同一个点

 题解:三分 位置

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 struct node
16 {
17     double x,v;
18 }N[60004];
19 double f(double t)
20 {
21     double maxn=0;
22     for(int i=1;i<=n;i++)
23     {
24         double ans;
25         ans=(abs(N[i].x-t))/N[i].v;
26         maxn=max(ans,maxn);
27     }
28     return maxn;
29 }
30 int main()
31 {
32     scanf("%d",&n);
33     for(int i=1;i<=n;i++)
34         scanf("%lf",&N[i].x);
35     for(int i=1;i<=n;i++)
36         scanf("%lf",&N[i].v);
37     double l=1,r=1000000000,m1,m2;
38     for(int i=1;i<2000;i++)
39     {
40         m1=l+(r-l)/3.0;
41         m2=r-(r-l)/3.0;
42         if(0.00000001<(f(m2)-f(m1)))
43             r=m2;
44         else
45             l=m1;
46     }
47     printf("%f\n",f(l));
48     return 0;
49 }

 

C. Andryusha and Colored Balloons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.

The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if ab and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.

Andryusha wants to use as little different colors as possible. Help him to choose the colors!

Input

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park.

Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path.

It is guaranteed that any square is reachable from any other using the paths.

Output

In the first line print single integer k — the minimum number of colors Andryusha has to use.

In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

Examples
input
3
2 3
1 3
output
3
1 3 2
input
5
2 3
5 3
4 3
1 3
output
5
1 3 2 5 4
input
5
2 1
3 2
4 3
5 4
output
3
1 2 3 1 2
Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

Illustration for the first sample.

In the second example there are following triples of consequently connected squares:

  • 1 → 3 → 2
  • 1 → 3 → 4
  • 1 → 3 → 5
  • 2 → 3 → 4
  • 2 → 3 → 5
  • 4 → 3 → 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.
Illustration for the second sample.

In the third example there are following triples:

  • 1 → 2 → 3
  • 2 → 3 → 4
  • 3 → 4 → 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only.
Illustration for the third sample.

 

 

题意:给你一颗树 现在涂色 要求连续的三个结点的颜色不能相同  问最少需要多少种颜色 并输出每个结点的颜色

题解:dfs处理

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 struct node
16 {
17     int pre;
18     int to;
19 }N[400005];
20 int pre[200005];
21 int nedge=0;
22 int mp[200005];
23 int re[200005];
24 int ma=0;
25 void add(int p,int to)
26 {
27     nedge++;
28     N[nedge].to=to;
29     N[nedge].pre=pre[p];
30     pre[p]=nedge;
31 }
32 void dfs(int root,int be1,int be2,int ans)
33 {
34     mp[root]=1;
35     ma=max(ma,ans);
36     re[root]=ans;
37     int jishu=1;
38     be2=be1;
39     be1=ans;
40     for(int i=pre[root];i;i=N[i].pre)
41     {
42         if(mp[N[i].to]==1)
43             continue;
44         while(jishu)
45         {
46             if(jishu==be1||jishu==be2)
47                 jishu++;
48             else
49                 break;
50         }
51         dfs(N[i].to,be1,be2,jishu);
52         jishu++;
53     }
54 }
55 int main()
56 {
57     memset(pre,0,sizeof(pre));
58     memset(mp,0,sizeof(mp));
59     scanf("%d",&n);
60     int l,r;
61     for(int i=1;i<=n-1;i++)
62     {
63         scanf("%d %d",&l,&r);
64         add(l,r);
65         add(r,l);
66     }
67     dfs(1,0,0,1);
68     printf("%d\n",ma);
69     for(int i=1;i<=n;i++)
70         printf("%d ",re[i]);
71      return 0;
72 }

 

D. Innokenty and a Football League
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.

Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:

  1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
  2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".

Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.

Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of clubs in the league.

Each of the next n lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.

Output

It it is not possible to choose short names and satisfy all constraints, print a single line "NO".

Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.

If there are multiple answers, print any of them.

Examples
input
2
DINAMO BYTECITY
FOOTBALL MOSCOW
output
YES
DIN
FOO
input
2
DINAMO BYTECITY
DINAMO BITECITY
output
NO
input
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
output
YES
PLM
PLS
GOG
input
3
ABC DEF
ABC EFG
ABD OOO
output
YES
ABD
ABE
ABO
Note

In the first sample Innokenty can choose first option for both clubs.

In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.

In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.

In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club yif the first options of x and y are different.

 题意:给你n个队伍的名字  现在要求选择队伍名的简写 两种选择 第一种是选择第一个串的前三个字符 第二种是选择第一个串的前两个字符以及第二个串的首字符 现在要求 每个队伍的简写要唯一存在 并且不能有 某两只

队伍 对应的第一个可选择的简写相同 并且第二个可选择的简写也相同

 题解:map标记处理

update: cf 数据出水了 这个是错误的..  orzzz

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 char a[30],b[30];
16 char ans[1005][30];
17 string str,st;
18 char exm1[30];
19 map<string,int>mp1;
20 map<string,int>mp2;
21 map<string,int>mp;
22 int main()
23 {
24     scanf("%d",&n);
25     int flag=1;
26     for(int i=1;i<=n;i++)
27     {
28         scanf("%s %s",a,b);
29         if(flag){
30         exm1[0]=a[0];exm1[1]=a[1];exm1[2]=a[2];
31         str.append(exm1);
32         int zha1=0,zha2=0;
33         st=str;
34         if(mp1[str]==0){
35             ans[i][0]=a[0];ans[i][1]=a[1];ans[i][2]=a[2];
36             mp1[str]=1;
37             zha1=1;
38         }
39         str.clear();
40         exm1[2]=b[0];
41         str.append(exm1);
42          if(mp2[str]==0){
43             ans[i][0]=a[0];ans[i][1]=a[1];ans[i][2]=b[0];
44             mp2[str]=1;
45             zha2=1;
46             zha1=0;
47         }
48         if(zha1==1){
49             if(mp[st])
50                 flag=0;
51             else
52                 mp[st]=1;
53         }
54         else{
55             if(zha2==1){
56                 if(mp[str])
57                     flag=0;
58                 else
59                     mp[str]=1;
60             }
61         }
62         str.clear();
63         if((zha1==0&&zha2==0))
64             flag=0;
65         }
66     }
67     if(flag==0)
68         printf("NO\n");
69     else{
70         printf("YES\n");
71         for(int i=1;i<=n;i++)
72         {
73             cout<<ans[i]<<endl;
74         }
75     }
76     return 0;
77 }