Codeforces Round #541 (Div. 2)

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 w1w1 and a height of h1h1, while the second rectangle has a width of w2w2 and a height of h2h2, where w1≥w2w1≥w2. In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field.

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 (1,1)(1,1), the rightmost top cell of the first rectangle has coordinates (w1,h1)(w1,h1), the leftmost bottom cell of the second rectangle has coordinates (1,h1+1)(1,h1+1) and the rightmost top cell of the second rectangle has coordinates (w2,h1+h2)(w2,h1+h2).

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 w1,h1,w2w1,h1,w2 and h2h2 (1≤w1,h1,w2,h2≤1081≤w1,h1,w2,h2≤108, w1≥w2w1≥w2) — the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles.

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

Note

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:

ac:

#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;

int w1,h1,w2,h2;

int main()
{
    scanf("%d%d%d%d",&w1,&h1,&w2,&h2);
    printf("%d\n",2*h1+2*h2+2*w1+4);
    return 0;
}

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 (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if the current score is «xx:yy», then after the goal it will change to "x+1x+1:yy" or "xx:y+1y+1". What is the largest number of times a draw could appear on the scoreboard?

The pairs "aiai:bibi" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.

Input

The first line contains a single integer nn (1≤n≤100001≤n≤10000) — the number of known moments in the match.

Each of the next nn lines contains integers aiai and bibi (0≤ai,bi≤1090≤ai,bi≤109), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).

All moments are given in chronological order, that is, sequences xixi and yjyj are non-decreasing. The last score denotes the final result of the match.

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

3
2 0
3 1
3 4

output

Copy

2

input

Copy

3
0 0
0 0
0 0

output

Copy

1

input

Copy

1
5 4

output

Copy

5

Note

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.

解析:

比较前后的情况来累加,注意各种情况

ac:

#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define MAXN 20005
using namespace std;

int a[MAXN]={0},b[MAXN]={0};

int solve(int a1,int b1,int a2,int b2)
{
    if(a1==b1)
    {
        if(b2>a2)
            return a2-a1;
        else return b2-b1;
    }
    else if(a1>b1)
    {
        if(b2>=a1&&b2<=a2)
            return b2-a1+1;
        else if(b2<a1)
            return 0;
        else if(b2>a2)
            return a2-a1+1;
    }
    else{
        if(b1>a2)
            return 0;
        else if(a2>=b1&&b2>=a2)
            return a2-b1+1;
        else if(a2>b2)
            return b2-b1+1;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i],&b[i]);
    int ax=0,bx=0;
    ll sum=1;
    a[0]=0,b[0]=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i-1]==a[i]&&b[i-1]==b[i]) continue;
        //cout<<solve(a[i-1],b[i-1],a[i],b[i])<<endl;
        sum+=solve(a[i-1],b[i-1],a[i],b[i]);
    }
    cout<<sum<<endl;
    return 0;
}

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 nn children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.

Formally, let's number children from 11 to nn in a circle order, that is, for every ii child with number ii will stand next to the child with number i+1i+1, also the child with number 11 stands next to the child with number nn. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.

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 nn (2≤n≤1002≤n≤100) — the number of the children who came to the cowboy Vlad's birthday.

The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) denoting heights of every child.

Output

Print exactly nn integers — heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.

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

Note

In the first example, the discomfort of the circle is equal to 11, since the corresponding absolute differences are 11, 11, 11 and 00. Note, that sequences [2,3,2,1,1][2,3,2,1,1] and [3,2,1,1,2][3,2,1,1,2] form the same circles and differ only by the selection of the starting point.

In the second example, the discomfort of the circle is equal to 2020, since the absolute difference of 1010 and 3030 is equal to 2020.

解析:

排序然后交错一下就能最小

ac:

#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;

int a[1000]={0};

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i=0;i<n;i++)
        if(i%2==0)
            printf("%d ",a[i]);
    for(int i=n-1;i>=0;i--)
    {
        if(i%2==1)
        {
            printf("%d",a[i]);
            if(i!=1)
                printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

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 nn dishes on the first day and a set of mm dishes on the second day. He made a table aa of size n×mn×m, in which he described his impressions. If, according to the expert, dish ii from the first set was better than dish jj from the second set, then aijaij is equal to ">", in the opposite case aijaij is equal to "<". Dishes also may be equally good, in this case aijaij is "=".

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 aijaij is "<", then the number assigned to dish ii from the first set should be less than the number of dish jj from the second set, if aijaij is ">", then it should be greater, and finally if aijaij is "=", then the numbers should be the same.

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 nn and mm (1≤n,m≤10001≤n,m≤1000) — the number of dishes in both days.

Each of the next nn lines contains a string of mm symbols. The jj-th symbol on ii-th line is aijaij. All strings consist only of "<", ">" and "=".

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 nn integers — evaluations of dishes from the first set, and on the third line print mmintegers — evaluations of dishes from the second set.

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

Note

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

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

题意:

给两个数组,长度分别为n,m.给两个数组的关系

构造出最小的两个数组,如果无法构造,输出NO,否则YES,输出两个数组

解析:

首先要判断是否有环,有环NO

对与"=",我们把它合成一个点,用并查集

用拓扑排序判断是否有环和构造数组

ac:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define MAXN 5005
using namespace std;

char str[1010][1010]={0};
int f[MAXN];
int in[MAXN]={0};
vector<int> mp[MAXN];
int reward[MAXN]={0};
int n,m;

void init(){
    rep(i,1,MAXN)
        f[i]=i;
}
int find(int x){
    return x==f[x]?x:f[x]=find(f[x]);
}
void unite(ll a,ll b){
    ll x=find(a),y=find(b);f[x]=y;
}

void topo()
{
    queue<int> que;
    for(int i=1;i<=n+m;i++)
        if(in[i]==0)
        {
            reward[i]=1;
            que.push(i);
        }
    int count=0;
    while(!que.empty())
    {
        int top=que.front();
        que.pop();
        count++;
        for(int i=0;i<mp[top].size();i++)
        {
            in[mp[top][i]]--;
            if(in[mp[top][i]]==0)
            {
                que.push(mp[top][i]);
                reward[mp[top][i]]=max(reward[mp[top][i]],reward[top]+1);
            }
        }
    }
    if(count!=n+m)//判环
        cout<<"NO"<<endl;
    else{
        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
            cout<<reward[find(i)]<<(n-i?' ':'\n');
        for(int i=1;i<=m;i++)
            cout<<reward[find(n+i)]<<(n-i?' ':'\n');
    }
}

int main()
{
    init();
    scanf("%d%d",&n,&m);
     rep(i,1,n)
        scanf("%s",str[i]+1);
     rep(i,1,n)
        rep(j,1,m)
            if(str[i][j]=='=')
                unite(i,j+n);//合点

    rep(i,1,n)
        rep(j,1,m)
        {
            if(str[i][j]=='=')
                continue;
            if(str[i][j]=='>')
            {
                mp[find(n+j)].push_back(find(i));
                in[find(i)]++;
            }
            else {
                mp[find(i)].push_back(find(n+j));
                in[find(n+j)]++;
            }
        }
    topo();
    return 0;
}

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 nn kittens, enumerated them from 11 and nn and then put them into the cage. The cage consists of one row of nn cells, enumerated with integers from 11 to nn from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n−1n−1 partitions originally. Initially, each cell contained exactly one kitten with some number.

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 ii, Asya:

  • Noticed, that the kittens xixi and yiyi, located in neighboring cells want to play together.
  • 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 n−1n−1 days the cage contained a single cell, having all kittens.

For every day, Asya remembers numbers of kittens xixi and yiyi, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into nn cells.

Input

The first line contains a single integer nn (2≤n≤1500002≤n≤150000) — the number of kittens.

Each of the following n−1n−1 lines contains integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n, xi≠yixi≠yi) — indices of kittens, which got together due to the border removal on the corresponding day.

It's guaranteed, that the kittens xixi and yiyi were in the different cells before this day.

Output

For every cell from 11 to nn print a single integer — the index of the kitten from 11 to nn, who was originally in it.

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

Note

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.

模拟集合,容器合并就可以了,随便用什么容器,我这里用栈

ac:

#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;

int a[MAXN]={0},b[MAXN]={0};
int vis[MAXN]={0};

int g[MAXN]={0};

stack<int> que[MAXN];

int main()
{
    memset(g,-1,sizeof(g));
    int n;
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
        scanf("%d%d",&a[i],&b[i]);
    int j=1;
    for(int i=0;i<n-1;i++)
    {
        if(g[a[i]]==-1&&g[b[i]]==-1)
        {
            que[j].push(a[i]);
            que[j].push(b[i]);
            g[a[i]]=j;
            g[b[i]]=j;
            j++;
        }
        else if(g[a[i]]==-1&&g[b[i]]!=-1)
        {
            que[g[b[i]]].push(a[i]);
            g[a[i]]=g[b[i]];
        }
        else if(g[a[i]]!=-1&&g[b[i]]==-1)
        {
            que[g[a[i]]].push(b[i]);
            g[b[i]]=g[a[i]];//我这里吧 = 写成你成 == ,wa
        }
        else{
            if(que[g[a[i]]].size()>que[g[b[i]]].size())
            {
                int c=g[a[i]];
                int d=g[b[i]];
                while(que[d].size())
                {
                    int x=que[d].top();
                    que[c].push(x);
                    que[d].pop();
                    g[x]=c;
                }
            }
            else{
                int c=g[a[i]];
                int d=g[b[i]];
                while(que[c].size())
                {
                    int x=que[c].top();
                    que[d].push(x);
                    que[c].pop();
                    g[x]=d;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        if(que[i].size()!=0)
        {
            while(que[i].size())
            {
                printf("%d",que[i].top());
                if(que[i].size()!=1)
                    printf(" ");
                else printf("\n");
                que[i].pop();
            }
            break;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值