Codeforces Round #600 (Div. 2)

 

A. Single Push

 

You're given two arrays a[1n]a[1…n] and b[1n]b[1…n], both of the same length nn.

In order to perform a push operation, you have to choose three integers l,r,kl,r,k satisfying 1lrn1≤l≤r≤n and k>0k>0. Then, you will add kkto elements al,al+1,,aral,al+1,…,ar.

For example, if a=[3,7,1,4,1,2]a=[3,7,1,4,1,2] and you choose (l=3,r=5,k=2)(l=3,r=5,k=2), the array aa will become [3,7,3,6,3––––––,2][3,7,3,6,3_,2].

You can do this operation at most once. Can you make array aa equal to array bb?

(We consider that a=ba=b if and only if, for every 1in1≤i≤n, ai=biai=bi)

Input

The first line contains a single integer tt (1t201≤t≤20) — the number of test cases in the input.

The first line of each test case contains a single integer nn (1n100 0001≤n≤100 000) — the number of elements in each array.

The second line of each test case contains nn integers a1,a2,,ana1,a2,…,an (1ai10001≤ai≤1000).

The third line of each test case contains nn integers b1,b2,,bnb1,b2,…,bn (1bi10001≤bi≤1000).

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.

Output

For each test case, output one line containing "YES" if it's possible to make arrays aa and bb equal by performing at most once the described operation or "NO" if it's impossible.

You can print each letter in any case (upper or lower).

Example
input
Copy
4
6
3 7 1 4 1 2
3 7 3 6 3 2
5
1 1 1 1 1
1 2 1 3 1
2
42 42
42 42
1
7
6
output
Copy
YES
NO
YES
NO
Note

The first test case is described in the statement: we can perform a push operation with parameters (l=3,r=5,k=2)(l=3,r=5,k=2) to make aa equal to bb.

In the second test case, we would need at least two operations to make aa equal to bb.

In the third test case, arrays aa and bb are already equal.

In the fourth test case, it's impossible to make aa equal to bb, because the integer kk has to be positive.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
const int mxn = 1e5+10;
int n,m,k,ans,col,t,flag,a[mxn],b[mxn],cnt;
//pair <int,int> pa[mxn];
bool cmp(pair<int,int>x,pair<int,int>y){    return x.first>y.first;}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        flag = 0;
        m = 0 ;
        cnt = 0;
        for(int i=1;i<=n;i++)
        {
            cin>>k;
            a[i]=k-a[i];
            if(a[i]!=0)
                b[m++] = i;
            if(a[i]>0) cnt = a[i];
            if(a[i]<0)
                flag = 1;
        }
        if(flag)
            cout<<"NO"<<endl;
        else
        {
            if( a[b[0]] != cnt && m!=0) flag = 1;
            for(int i=1;i<m;i++)
            {
                if(b[i]-b[i-1]!=1  || a[b[i]] != cnt  || a[b[i-1]] !=cnt )
                    {flag = 1;break;}
            }
            if(flag)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}

 

 

 

B. Silly Mistake

The Central Company has an office with a sophisticated security system. There are 106106 employees, numbered from 11 to 106106.

The security system logs entrances and departures. The entrance of the ii-th employee is denoted by the integer ii, while the departure of the ii-th employee is denoted by the integer i−i.

The company has some strict rules about access to its office:

  • An employee can enter the office at most once per day.
  • He obviously can't leave the office if he didn't enter it earlier that day.
  • In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.

Any array of events satisfying these conditions is called a valid day.

Some examples of valid or invalid days:

  • [1,7,7,3,1,3][1,7,−7,3,−1,−3] is a valid day (11 enters, 77 enters, 77 leaves, 33 enters, 11 leaves, 33 leaves).
  • [2,2,3,3][2,−2,3,−3] is also a valid day.
  • [2,5,5,5,5,2][2,5,−5,5,−5,−2] is not a valid day, because 55 entered the office twice during the same day.
  • [4,4][−4,4] is not a valid day, because 44 left the office without being in it.
  • [4][4] is not a valid day, because 44 entered the office and didn't leave it before the end of the day.

There are nn events a1,a2,,ana1,a2,…,an, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.

You must partition (to cut) the array aa of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.

For example, if n=8n=8 and a=[1,1,1,2,1,2,3,3]a=[1,−1,1,2,−1,−2,3,−3] then he can partition it into two contiguous subarrays which are valid days: a=[1,1 | 1,2,1,2,3,3]a=[1,−1 | 1,2,−1,−2,3,−3].

Help the administrator to partition the given array aa in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.

Input

The first line contains a single integer nn (1n1051≤n≤105).

The second line contains nn integers a1,a2,,ana1,a2,…,an (106ai106−106≤ai≤106 and ai0ai≠0).

Output

If there is no valid partition, print 1−1. Otherwise, print any valid partition in the following format:

  • On the first line print the number dd of days (1dn1≤d≤n).
  • On the second line, print dd integers c1,c2,,cdc1,c2,…,cd (1cin1≤ci≤n and c1+c2++cd=nc1+c2+…+cd=n), where cici is the number of events in the ii-th day.

If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.

Examples
input
Copy
6
1 7 -7 3 -1 -3
output
Copy
1
6
input
Copy
8
1 -1 1 2 -1 -2 3 -3
output
Copy
2
2 6
input
Copy
6
2 5 -5 5 -5 -2
output
Copy
-1
input
Copy
3
-8 1 1
output
Copy
-1
Note

In the first example, the whole array is a valid day.

In the second example, one possible valid solution is to split the array into [1,1][1,−1] and [1,2,1,2,3,3][1,2,−1,−2,3,−3] (d=2d=2 and c=[2,6]c=[2,6]). The only other valid solution would be to split the array into [1,1][1,−1], [1,2,1,2][1,2,−1,−2] and [3,3][3,−3] (d=3d=3 and c=[2,4,2]c=[2,4,2]). Both solutions are accepted.

In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
const int mxn = 1e5+10;
int n,m,k,ans,col,t,flag,a[mxn],b[mxn],cnt;
//pair <int,int> pa[mxn];
bool cmp(pair<int,int>x,pair<int,int>y){    return x.first>y.first;}
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            a[i]=b[i] = 0;
        set<int>st;
        map<int,int>mp;
        flag = 0; m = 0,col = 0;int mx = 0;
        for(int i=1;i<=n;i++)
        {
            col = 0;
            cin>>k;
            mp[k]++;
            if(mp[k]>1) {flag = 1;continue;}
            if(k>0)
                st.insert(k);
            else
            {
                if(st.find(-k)!=st.end())
                    st.erase(-k);
                else
                    flag = 1;
            }
            //cout<<st.size()<<"           "<<endl;
            if(st.size()==0)
            {
                for(map<int,int>::iterator it = mp.begin();it!=mp.end();it++)
                {
                    if(it->second>1)
                        col = 1;
                }
                if(col)
                    flag = 1;
                else
                    b[m++] = i;
                mp.clear();
            }
        }
        if(flag || st.size()!=0)
            cout<<-1<<endl;
        else
        {
            cout<<m<<endl;
            cout<<b[0];
            for(int i=1;i<m;i++)
                cout<<" "<<b[i]-b[i-1];
            cout<<endl;
        }

    }
    return 0;
}

 

 C. Sweets Eating

Tsumugi brought nn delicious sweets to the Light Music Club. They are numbered from 11 to nn, where the ii-th sweet has a sugar concentration described by an integer aiai.

Yui loves sweets, but she can eat at most mm sweets each day for health reasons.

Days are 11-indexed (numbered 1,2,3,1,2,3,…). Eating the sweet ii at the dd-th day will cause a sugar penalty of (dai)(d⋅ai), as sweets become more sugary with time. A sweet can be eaten at most once.

The total sugar penalty will be the sum of the individual penalties of each sweet eaten.

Suppose that Yui chooses exactly kk sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get?

Since Yui is an undecided girl, she wants you to answer this question for every value of kk between 11 and nn.

Input

The first line contains two integers nn and mm (1mn200 0001≤m≤n≤200 000).

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai200 0001≤ai≤200 000).

Output

You have to output nn integers x1,x2,,xnx1,x2,…,xn on a single line, separed by spaces, where xkxk is the minimum total sugar penalty Yui can get if she eats exactly kk sweets.

Examples
input
Copy
9 2
6 19 3 4 4 2 6 7 8
output
Copy
2 5 11 18 30 43 62 83 121
input
Copy
1 1
7
output
Copy
7
Note

Let's analyze the answer for k=5k=5 in the first example. Here is one of the possible ways to eat 55 sweets that minimize total sugar penalty:

  • Day 11: sweets 11 and 44
  • Day 22: sweets 55 and 33
  • Day 33 : sweet 66

Total penalty is 1a1+1a4+2a5+2a3+3a6=6+4+8+6+6=301⋅a1+1⋅a4+2⋅a5+2⋅a3+3⋅a6=6+4+8+6+6=30. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats 55 sweets, hence x5=30x5=30.

 

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
const int mxn = 2e5+10;
int n,m,k,ans,col,t,flag;
ll a[mxn],sum[mxn],cnt;
//pair <int,int> pa[mxn];
bool cmp(pair<int,int>x,pair<int,int>y){    return x.first>y.first;}
int main()
{
    TLE;
    while(cin>>n>>m)
    {
        a[0] = 0;
        set<int>st;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
            sum[i] = sum[i-1]+a[i];
        for(int i=1;i<=n;i++)
        {
            if(i<=m)
                cout<<sum[i]<<" ";
            else
            {
                cout<<sum[i-m]+sum[i]<<" ";
                sum[i] = sum[i-m]+sum[i] ;
            }
        }

        cout<<endl;
    }
    return 0;
}

 

D. Harmonious Graph 

 

You're given an undirected graph with nn nodes and mm edges. Nodes are numbered from 11 to nn.

The graph is considered harmonious if and only if the following property holds:

  • For every triple of integers (l,m,r)(l,m,r) such that 1l<m<rn1≤l<m<r≤n, if there exists a path going from node ll to node rr, then there exists a path going from node ll to node mm.

In other words, in a harmonious graph, if from a node ll we can reach a node rr through edges (l<rl<r), then we should able to reach nodes (l+1),(l+2),,(r1)(l+1),(l+2),…,(r−1) too.

What is the minimum number of edges we need to add to make the graph harmonious?

Input

The first line contains two integers nn and mm (3n200 0003≤n≤200 000 and 1m200 0001≤m≤200 000).

The ii-th of the next mm lines contains two integers uiui and vivi (1ui,vin1≤ui,vi≤n, uiviui≠vi), that mean that there's an edge between nodes uuand vv.

It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes).

Output

Print the minimum number of edges we have to add to the graph to make it harmonious.

Examples
input
Copy
14 8
1 2
2 7
3 4
6 3
5 7
3 8
6 8
11 12
output
Copy
1
input
Copy
200000 3
7 9
9 8
4 5
output
Copy
0
Note

In the first example, the given graph is not harmonious (for instance, 1<6<71<6<7, node 11 can reach node 77 through the path 1271→2→7, but node 11 can't reach node 66). However adding the edge (2,4)(2,4) is sufficient to make it harmonious.

In the second example, the given graph is already harmonious.

 

 和Codeforces Round ##599 D题 0-1 MST 类似

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
const int mxn = 2e5+10;
int n,m,k,ans,col,t,flag,x,y,vis[mxn];
ll a[mxn],sum[mxn],cnt;
vector<int>G[mxn];
//pair <int,int> pa[mxn];
bool cmp(pair<int,int>x,pair<int,int>y){    return x.first>y.first;}
void DFS(int in)
{
    vis[in] = 1;
    for(vector<int>::iterator it=G[in].begin();it!=G[in].end();it++)
    {
        if(!vis[*it])
            DFS(*it);
    }
    col = max(col,in);
}
int main()
{
    TLE;
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y;
            G[x].pb(y);
            G[y].pb(x);
        }
        int i = 1;ans = 0 ;
        while(i<n)
        {
            col = 0 ;
            if(!vis[i]) DFS(i);
            for(int j=i;j<col;j++)
            {
                if(!vis[j])
                    DFS(j),ans++;
            }
            i = col + 1;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

 

 

posted @ 2019-11-17 08:10  __MEET  阅读(465)  评论(0编辑  收藏  举报