# CF 1131A，1131B，1131C，1131D，1131F(Round541 A,B,C,D,F)题解

### A. Sea Battle

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

In order to make the "Sea Battle" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of

The rectangles are placed on field in the following way:

• the second rectangle is on top the first rectangle;
• they are aligned to the left, i.e. their left sides are on the same line;
• the rectangles are adjacent to each other without a gap.

See the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue.

Formally, let's introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates

After the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don't belong to the ship are marked. On the pictures in the notes such cells are colored green.

Find out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction.

Input

Four lines contain integers

Output

Print exactly one integer — the number of cells, which should be marked after the ship is destroyed.

Examples
input
Copy
2 1 2 1

output
Copy
12

input
Copy
2 2 1 2

output
Copy
16


In the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares):

In the second example the field looks as:

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int INF=0x3f3f3f3f;
5 ll w1,h1,w2,h2,ans;
6 int main()
7 {
8     ans=0;
9     scanf("%lld%lld%lld%lld",&w1,&h1,&w2,&h2);
10     ans=2*(w1+h1+h2+2);
11     printf("%lld\n",ans);
12     return 0;
13 }
View Code

### B. Draw!

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

You still have partial information about the score during the historic football match. You are given a set of pairs

The pairs "

Input

The first line contains a single integer

Each of the next

All moments are given in chronological order, that is, sequences

Output

Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.

Examples
input
Copy
32 03 13 4
output
Copy
2
input
Copy
30 00 00 0
output
Copy
1
input
Copy
15 4
output
Copy
5

In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.

题解：给你两个人在比赛中某些时刻的得分比，让你判断最多有多少时刻，两个人的比分是相同的；

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 int n;
5 ll x,y,cx,cy,ans;
6 int main()
7 {
8     cin>>n;
9     ans=1;cx=cy=0;
10     for(int i=1;i<=n;++i)
11     {
12         cin>>x>>y;
13         ll L=max(cx,cy);
14         ll R=min(x,y);
15         if(R>=L)
16         {
17             if(cx==cy) ans+=(R-L);
18             else ans+=(R-L+1);
19         }
20         cx=x;cy=y;
21     }
22     cout<<ans<<endl;
23
24
25     return 0;
26 }
View Code

### C. Birthday

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

Cowboy Vlad has a birthday today! There are

Formally, let's number children from

Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.

Input

The first line contains a single integer

The second line contains integers

Output

Print exactly

If there are multiple possible answers, print any of them.

Examples
input
Copy
5
2 1 1 3 2

output
Copy
1 2 3 2 1

input
Copy
3
30 10 20

output
Copy
10 20 30


In the first example, the discomfort of the circle is equal to

In the second example, the discomfort of the circle is equal to

题解：重新排列这些数，是其围城一个环相邻差值和最小；

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 #define clr(a,val) memset(a,val,sizeof(a))
5 #define fi first
6 #define se second
7 #define pb push_back
8 const int INF=0x3f3f3f3f;
9 int n,a[105],b[105];
10 int main()
11 {
12     int n;
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
15     sort(a+1,a+1+n);
16     for(int i=1;i<=n;i++)
17     {
18         if(i&1)
19         b[i/2+1]=a[i];
20         else b[n-i/2+1]=a[i];
21     }
22     for(int i=1;i<=n;i++) printf("%d ",b[i]);
23     printf("\n");
24     return 0;
25 }
View Code

### D. Gourmet choice

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

Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review  — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer.

Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes.

The gourmet tasted a set of

Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if

Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible.

Input

The first line contains integers

Each of the next

Output

The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise.

If case an answer exist, on the second line print

Examples
input
Copy
3 4
>>>>
>>>>
>>>>

output
Copy
Yes
2 2 2
1 1 1 1

input
Copy
3 3
>>>
<<<
>>>

output
Copy
Yes
3 1 3
2 2 2

input
Copy
3 2
==
=<
==

output
Copy
No


In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be

In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.

题解：题目就是给你n和m个数，给出这n和m个数的大小关系，让你求，最小有多少种数；

n和m映射到1~n+m这些数中；

dp[e[x].to]=max(dp[e[x].to],dp[x]+1);

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 #define clr(a,val) memset(a,val,sizeof(a))
5 #define fi first
6 #define se second
7 #define pb push_back
8 const int INF=0x3f3f3f3f;
9 const int N=2e3+7,M=2e6+7;
10 struct data{
11     int to,next;
12 } e[M];
14 int q[N],t,w,fa[N],ans[N],in[N];
15 char mp[N/2][N/2];
16 int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); }
17 void ins(int u,int v)
18 {
19     e[++cnt].to=v;
22     in[v]++;//入度
23 }
24 int main()
25 {
26     scanf("%d%d",&n,&m);
27     for(int i=1;i<=n;++i) scanf("%s",mp[i]+1);
28     for(int i=1;i<=n+m;++i) fa[i]=i;
29     for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(mp[i][j]=='=') fa[find(i)]=find(n+j);//缩点
30     for(int i=1;i<=n;++i) for(int j=1;j<=m;++j)
31     {
32         if(mp[i][j]=='>') ins(find(n+j),find(i));
33         else if(mp[i][j]=='<') ins(find(i),find(n+j));
34     }
35
36     for(int i=1;i<=n+m;++i) if(find(i)==i) tot++;
37     for(int i=1;i<=n+m;++i) if(!in[i]&&find(i)==i) q[w++]=i,dp[i]=1;
38     while(t!=w)//拓扑
39     {
40         x=q[t++];
42         {
43             dp[e[i].to]=max(dp[e[i].to],dp[x]+1);
44             if(--in[e[i].to]==0) q[w++]=e[i].to;
45         }
46     }
47     if(t!=tot) { puts("No");return 0;}
48     else
49     {
50         puts("Yes");
51         for(int i=1;i<=n;++i) printf("%d%c",dp[find(i)],i==n?'\n':' ');
52         for(int i=1;i<=m;++i) printf("%d%c",dp[find(i+n)],i==m?'\n':' ');
53     }
54     return 0;
55 }
View Code

### F. Asya And Kittens

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

Asya loves animals very much. Recently, she purchased

Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day

• Noticed, that the kittens
• Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.

Since Asya has never putted partitions back, after

For every day, Asya remembers numbers of kittens

Input

The first line contains a single integer

Each of the following

It's guaranteed, that the kittens

Output

For every cell from

All printed integers must be distinct.

It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.

Example
input
Copy
5
1 4
2 5
3 1
4 5

output
Copy
3 1 4 2 5


The answer for the example contains one of several possible initial arrangements of the kittens.

The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells.

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define clr(a,val) memset(a,val,sizeof(a))
4 #define ll long long
5 #define PI acos(-1.0)
6 #define mp make_pair
7 #define pb push_back
8 const int INF=0x3f3f3f3f;
9 const int maxn=2e5+10;
10 int n,fa[maxn],r[maxn],g[maxn],x,y;
11 int find(int x){return x==fa[x]? x:fa[x]=find(fa[x]);}
12 int main()
13 {
14     scanf("%d",&n);
15     for(int i=1;i<=n;++i) fa[i]=i,r[i]=i;
16     for(int i=1;i<n;++i)
17     {
18         scanf("%d%d",&x,&y);
19         int fx=find(x),fy=find(y);
20         g[r[fx]]=fy;r[fx]=r[fy];fa[fy]=fx;
21     }
22     for(int i=find(1);i;i=g[i]) printf("%d ",i);
23     printf("\n");
24     return 0;
25 }
View Code

posted @ 2019-02-24 11:01  StarHai  阅读(342)  评论(0编辑  收藏