Codeforces Round #600 (Div. 2)
A. Single Push
You're given two arrays a[1…n]a[1…n] and b[1…n]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 1≤l≤r≤n1≤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 1≤i≤n1≤i≤n, ai=biai=bi)
The first line contains a single integer tt (1≤t≤201≤t≤20) — the number of test cases in the input.
The first line of each test case contains a single integer nn (1≤n≤100 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 (1≤ai≤10001≤ai≤1000).
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤10001≤bi≤1000).
It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.
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).
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
YES NO YES NO
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.
The first line contains a single integer nn (1≤n≤1051≤n≤105).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106 and ai≠0ai≠0).
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 (1≤d≤n1≤d≤n).
- On the second line, print dd integers c1,c2,…,cdc1,c2,…,cd (1≤ci≤n1≤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.
6 1 7 -7 3 -1 -3
1 6
8 1 -1 1 2 -1 -2 3 -3
2 2 6
6 2 5 -5 5 -5 -2
-1
3 -8 1 1
-1
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