codeforces 495 (AB思維+C暴力+D暴力構造+E樹的直徑)

A - Sonya and Hotels

Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.

The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has nn hotels, where the ii-th hotel is located in the city with coordinate xixi. Sonya is a smart girl, so she does not open two or more hotels in the same city.

Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to dd. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel.

Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original nn hotels to the new one is equal to dd.

Input

The first line contains two integers nn and dd (1≤n≤1001≤n≤100, 1≤d≤1091≤d≤109) — the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.

The second line contains nn different integers in strictly increasing order x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−109≤xi≤109) — coordinates of Sonya's hotels.

Output

Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to dd.

Examples

Input

4 3
-3 2 9 16

Output

6

Input

5 2
4 8 11 18 19

Output

5

Note

In the first example, there are 66 possible cities where Sonya can build a hotel. These cities have coordinates −6−6, 55, 66, 1212, 1313, and 1919.

In the second example, there are 55 possible cities where Sonya can build a hotel. These cities have coordinates 22, 66, 1313, 1616, and 2121.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 3e5 +7;
const int inf = 1e9;
ll _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last;
struct node {int u,v;}e[mxn];
int a[mxn];
int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++) cin>>a[i];
        sort(a+1,a+1+n);
        ans = 2 ;
        for(int i=1;i<n;i++)
        {
            if(a[i+1]-a[i]>2*m) ans+=2;
            else if(a[i+1]-a[i]==2*m) ans+=1;
        }
        cout<<ans<<endl;
    }
}

 

B - Sonya and Exhibition

 

Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.

There are nn flowers in a row in the exhibition. Sonya can put either a rose or a lily in the ii-th position. Thus each of nn positions should contain exactly one flower: a rose or a lily.

She knows that exactly mm people will visit this exhibition. The ii-th visitor will visit all flowers from lilito riri inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.

Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.

Input

The first line contains two integers nn and mm (1≤n,m≤1031≤n,m≤103) — the number of flowers and visitors respectively.

Each of the next mm lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n), meaning that ii-th visitor will visit all flowers from lili to riri inclusive.

Output

Print the string of nn characters. The ii-th symbol should be «0» if you want to put a rose in the ii-th position, otherwise «1» if you want to put a lily.

If there are multiple answers, print any.

Examples

Input

5 3
1 3
2 4
2 5

Output

01100

Input

6 3
5 6
1 4
4 6

Output

110010

Note

In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions;

  • in the segment [1…3][1…3], there are one rose and two lilies, so the beauty is equal to 1⋅2=21⋅2=2;
  • in the segment [2…4][2…4], there are one rose and two lilies, so the beauty is equal to 1⋅2=21⋅2=2;
  • in the segment [2…5][2…5], there are two roses and two lilies, so the beauty is equal to 2⋅2=42⋅2=4.

The total beauty is equal to 2+2+4=82+2+4=8.

In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions;

  • in the segment [5…6][5…6], there are one rose and one lily, so the beauty is equal to 1⋅1=11⋅1=1;
  • in the segment [1…4][1…4], there are two roses and two lilies, so the beauty is equal to 2⋅2=42⋅2=4;
  • in the segment [4…6][4…6], there are two roses and one lily, so the beauty is equal to 2⋅1=22⋅1=2.

The total beauty is equal to 1+4+2=71+4+2=7.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 3e5 +7;
const int inf = 1e9;
ll _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last;
struct node {int u,v;}e[mxn];
int a[mxn];
int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++) cin>>u>>v;
        for(int i=1;i<=n;i++)
            if(i&1) cout<<1;
            else cout<<0;
        cout<<endl;
    }
}

C - Sonya and Robots

 

Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.

Sonya has drawn nn numbers in a row, aiai is located in the ii-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.

Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.

For example, if the numbers [1,5,4,1,3][1,5,4,1,3] are written, and Sonya gives the number 11 to the first robot and the number 44 to the second one, the first robot will stop in the 11-st position while the second one in the 33-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number 44 to the first robot and the number 55 to the second one, they will meet since the first robot will stop in the 33-rd position while the second one is in the 22-nd position.

Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.

Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs (pp, qq), where she will give pp to the first robot and qq to the second one. Pairs (pipi, qiqi) and (pjpj, qjqj) are different if pi≠pjpi≠pj or qi≠qjqi≠qj.

Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.

Input

The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of numbers in a row.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105) — the numbers in a row.

Output

Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.

Examples

Input

5
1 5 4 1 3

Output

9

Input

7
1 2 1 1 1 3 2

Output

7

Note

In the first example, Sonya can give pairs (11, 11), (11, 33), (11, 44), (11, 55), (44, 11), (44, 33), (55, 11), (55, 33), and (55, 44).

In the second example, Sonya can give pairs (11, 11), (11, 22), (11, 33), (22, 11), (22, 22), (22, 33), and (33, 22).

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 +7;
const int inf = 1e9;
ll _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last;
struct node {int u,v;}e[mxn];
int a[mxn] , vis[mxn];
bool b[mxn] ;
int main()
{
    while(cin>>n)
    {
        set<int>s;
        memset(b,0,sizeof(b));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++) cin>>a[i] , vis[ a[i] ]++ , s.insert( a[i] ) ;
        ans = s.size(); cnt = 0 ;
        for(int i=1;i<=n;i++)
        {
            if(!b[ a[i] ])
            {
                b[ a[i] ] = 1 ;
                vis[ a[i] ]--;
                if(!vis[ a[i] ]) ans--;
                cnt += ans ;
                /// cout<<cnt<< " " << ans<<endl;
            }
            else
            {
                vis[ a[i] ]--;
                if(!vis[ a[i] ]) ans--;
            }
        }
        cout<<cnt<<endl;
    }
}

D - Sonya and Matrix

 

Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.

Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.

The Manhattan distance between two cells (x1x1, y1y1) and (x2x2, y2y2) is defined as |x1−x2|+|y1−y2||x1−x2|+|y1−y2|. For example, the Manhattan distance between the cells (5,2)(5,2) and (7,1)(7,1) equals to |5−7|+|2−1|=3|5−7|+|2−1|=3.

 Example of a rhombic matrix.

Note that rhombic matrices are uniquely defined by nn, mm, and the coordinates of the cell containing the zero.

She drew a n×mn×m rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of n⋅mn⋅m numbers). Note that Sonya will not give you nn and mm, so only the sequence of numbers in this matrix will be at your disposal.

Write a program that finds such an n×mn×m rhombic matrix whose elements are the same as the elements in the sequence in some order.

Input

The first line contains a single integer tt (1≤t≤1061≤t≤106) — the number of cells in the matrix.

The second line contains tt integers a1,a2,…,ata1,a2,…,at (0≤ai<t0≤ai<t) — the values in the cells in arbitrary order.

Output

In the first line, print two positive integers nn and mm (n×m=tn×m=t) — the size of the matrix.

In the second line, print two integers xx and yy (1≤x≤n1≤x≤n, 1≤y≤m1≤y≤m) — the row number and the column number where the cell with 00 is located.

If there are multiple possible answers, print any of them. If there is no solution, print the single integer −1−1.

Examples

Input

20
1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4

Output

4 5
2 2

Input

18
2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1

Output

3 6
2 3

Input

6
2 1 0 2 1 2

Output

-1

Note

You can see the solution to the first example in the legend. You also can choose the cell (2,2)(2,2) for the cell where 00 is located. You also can choose a 5×45×4 matrix with zero at (4,2)(4,2).

In the second example, there is a 3×63×6 matrix, where the zero is located at (2,3)(2,3) there.

In the third example, a solution does not exist.

題意:

       構造一個n*m的數組,將給定的長爲N的序列的每個全部填入到數組中,使得數組中的每個數字X到0位置的曼哈頓距離等於X

題解:

  直接構造一個數組不好構造出來,那麼就理解爲是否存在一個n*m的數組,使得數組存的所有數剛好等於輸入的序列,而畫圖可以發現完整的圖形中,數i的個數一定是4*i

  那麼,我們就先記錄每個數出現的次數和最大值max,然後枚舉一下1~N,找到N的因子K,那麼因子的乘積K* (N/K)就是數組的大小,而最大值一定是在邊角上,

  那麼,通過圖形可以發現 0 點的座標之一 x (或者y) 一定是第一個不滿足4*I的數字,另一個就是y(或者x) = n+m-max-x,

  那麼,0點座標也就出來了,那我們再遍歷一下數組,看當前n*m的數組中需要每個數的個數是否都與給定數字的個數的相吻合,如果吻合就輸出,反之,就繼續遍歷

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e6 +7;
const int inf = 1e9;
int _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last;
struct node {int u,v;}e[mxn];
int a[mxn] , vis[mxn] ;
void solve()
{
    if(vis[0]!=1){
        cout<<-1<<endl;return ;
    }
    int l , r , x = 1, y; ok = 1 ;
    for(int i=1;i<=n;i++){
        if(vis[i]>4*i){
            cout<<-1<<endl;return ;
        }
        if(vis[i]<4*i) {
            x = i ; break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(n%i) continue ;
        m = n/i ; /// 列
        y = i-x + m - last ;
        memset(a,0,sizeof(a)) ;
        for(int j=1;j<=i;j++)
        {
            for(int k=1;k<=m;k++)
            {
                tmp = abs( j-x ) + abs( k-y );
                a[tmp]++;
            }
        }
        /*
        for(int ii=0;ii<=n;ii++)
            cout<<vis[ii]<<" * "<<a[ii]<<endl;
            */
        int flag = 1 ;
        for(int j=0;j<=last;j++){
            if(a[j]!=vis[j]){
                flag = 0 ; break;
            }
        }
        if(flag){
            cout<<i<<" "<<m<<endl<<x<<" "<<y<<endl;
            ok=0;break;
        }
    }
    if(ok) cout<<-1<<endl;
}
int main()
{
    tle;
    while(cin>>n)
    {
        memset(vis,0,sizeof(vis));
        last = 0 ;
        for(int i=1;i<=n;i++){
            cin>>k , vis[k]++;
            last = max(last,k);
        }
        /*
        for(int i=0;i<=n;i++)
            cout<<vis[i]<<" "<<i<<endl;
            */
        solve();
    }
}
/*
20
1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4
18
2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1
6
2 1 0 2 1 2
*/

E - Sonya and Ice Cream

 

Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.

Sonya lives in a city with nn junctions and n−1n−1 streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets.

Sonya has exactly kk friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.

Sonya wants all her ice cream shops to form a simple path of the length rr (1≤r≤k1≤r≤k), i.e. to be located in different junctions f1,f2,…,frf1,f2,…,fr and there is street between fifi and fi+1fi+1 for each ii from 11 to r−1r−1.

The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions aa and bb is equal to the sum of all the street lengths that you need to pass to get from the junction aa to the junction bb. So Sonya wants to minimize

 

maxamin1≤i≤rda,fimaxamin1≤i≤rda,fi

where aa takes a value of all possible nn junctions, fifi — the junction where the ii-th Sonya's shop is located, and dx,ydx,y — the distance between the junctions xx and yy.

Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than kk shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.

Input

The first line contains two integers nn and kk (1≤k≤n≤1051≤k≤n≤105) — the number of junctions and friends respectively.

Each of the next n−1n−1 lines contains three integers uiui, vivi, and didi (1≤ui,vi≤n1≤ui,vi≤n, vi≠uivi≠ui, 1≤d≤1041≤d≤104) — junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other.

Output

Print one number — the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most kk.

Examples

Input

6 2
1 2 3
2 3 4
4 5 2
4 6 3
2 4 6

Output

4

Input

10 3
1 2 5
5 7 2
3 2 6
10 6 3
3 8 1
6 4 2
4 1 6
6 9 4
5 2 5

Output

7

Note

In the first example, you can choose the path 2-4, so the answer will be 4.

 The first example.

In the second example, you can choose the path 4-1-2, so the answer will be 7.

 The second example.

題意:有一棵樹,然後,選擇其中的K個連續的點,問其餘點到這K個點任意一點的最小的最大距離

題解:從葉子節點不斷的刪除,set進行維護 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e6 +7;
const int inf = 1e9;
int _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last,nx;
struct node {int u,v;}e[mxn];
set<pair<int,int> >s , se[mxn];
int main()
{
    while(cin>>n>>k)
    {
        s.clear();
        for(int i=1;i<=n;i++) se[i].clear();
        for(int i=1;i<n;i++)
        {
            cin>>u>>v>>w;
            se[u].insert( make_pair(v,w) );
            se[v].insert( make_pair(u,w) );
        }
        for(int i=1;i<=n;i++)
            if( se[i].size()==1 )
                s.insert( make_pair( (*se[i].begin()).second,i ) );
        while(n>k || s.size()>2)
        {
            ans = (*s.begin()).first ;
            cnt = (*s.begin()).second ;
            s.erase(s.begin());
            nx = (*se[cnt].begin()).first ;
            se[nx].erase( se[nx].lower_bound(make_pair(cnt,0)) );
            n--;
            if(se[nx].size()==1)
                s.insert( make_pair( (*se[nx].begin()).second+ans,nx ) );
        }
        cout<<ans<<endl;
    }
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章