Codeforces Round #598 (Div. 3)

 A. Payment Without Change

You have aa coins of value nn and bb coins of value 11. You always pay in exact change, so you want to know if there exist such xx and yy that if you take xx (0xa0≤x≤a) coins of value nn and yy (0yb0≤y≤b) coins of value 11, then the total value of taken coins will be SS.

You have to answer qq independent test cases.

Input

The first line of the input contains one integer qq (1q1041≤q≤104) — the number of test cases. Then qq test cases follow.

The only line of the test case contains four integers aa, bb, nn and SS (1a,b,n,S1091≤a,b,n,S≤109) — the number of coins of value nn, the number of coins of value 11, the value nn and the required total value.

Output

For the ii-th test case print the answer on it — YES (without quotes) if there exist such xx and yy that if you take xx coins of value nn and yycoins of value 11, then the total value of taken coins will be SS, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example
input
Copy
4
1 2 3 4
1 2 3 6
5 2 6 27
3 3 5 18
output
Copy
YES
NO
NO
YES

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,k,ans,cnt,t;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>ans>>cnt;
        ll l = n*ans+m;
        if(l<cnt)
            cout<<"NO"<<endl;
        else
        {
            if( cnt/ans < n )
            {
                if( cnt-cnt/ans*ans<=m ) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
            else
            {
                if( cnt-n*ans<=m ) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}

 

B. Minimize the Permutation

You are given a permutation of length nn. Recall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

You can perform at most n1n−1 operations with the given permutation (it is possible that you don't perform any operations at all). The ii-th operation allows you to swap elements of the given permutation on positions ii and i+1i+1. Each operation can be performed at most once. The operations can be performed in arbitrary order.

Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.

You can see the definition of the lexicographical order in the notes section.

You have to answer qq independent test cases.

For example, let's consider the permutation [5,4,1,3,2][5,4,1,3,2]. The minimum possible permutation we can obtain is [1,5,2,4,3][1,5,2,4,3] and we can do it in the following way:

  1. perform the second operation (swap the second and the third elements) and obtain the permutation [5,1,4,3,2][5,1,4,3,2];
  2. perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation [5,1,4,2,3][5,1,4,2,3];
  3. perform the third operation (swap the third and the fourth elements) and obtain the permutation [5,1,2,4,3][5,1,2,4,3].
  4. perform the first operation (swap the first and the second elements) and obtain the permutation [1,5,2,4,3][1,5,2,4,3];

Another example is [1,2,4,3][1,2,4,3]. The minimum possible permutation we can obtain is [1,2,3,4][1,2,3,4] by performing the third operation (swap the third and the fourth elements).

Input

The first line of the input contains one integer qq (1q1001≤q≤100) — the number of test cases. Then qq test cases follow.

The first line of the test case contains one integer nn (1n1001≤n≤100) — the number of elements in the permutation.

The second line of the test case contains nn distinct integers from 11 to nn — the given permutation.

Output

For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.

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

Recall that the permutation pp of length nn is lexicographically less than the permutation qq of length nn if there is such index ini≤n that for all jj from 11 to i1i−1 the condition pj=qjpj=qj is satisfied, and pi<qipi<qi. For example:

  • p=[1,3,5,2,4]p=[1,3,5,2,4] is less than q=[1,3,5,4,2]q=[1,3,5,4,2] (such i=4i=4 exists, that pi<qipi<qi and for each j<ij<i holds pj=qjpj=qj),
  • p=[1,2]p=[1,2] is less than q=[2,1]q=[2,1] (such i=1i=1 exists, that pi<qipi<qi and for each j<ij<i holds pj=qjpj=qj).

 

#include<bits/stdc++.h>
using namespace std;
const int mxn = 1e2+5;
int vis[mxn],n,arr[mxn],m,cnt,ans,t,k;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            cin>>arr[i];
        for(int i=1;i<=n;i++)
        {
            for(int j=n;j>1;j--)
            {
                if(arr[j]<arr[j-1] && !vis[j-1])
                    swap(arr[j],arr[j-1]),vis[j-1] = 1;
            }
        }
        for(int i=1;i<=n;i++)
            cout<<arr[i]<<" ";
        cout<<endl;
    }
    return 0;
}

 

 C. Platforms Jumping

There is a river of width nn. The left bank of the river is cell 00 and the right bank is cell n+1n+1 (more formally, the river can be represented as a sequence of n+2n+2 cells numbered from 00 to n+1n+1). There are also mm wooden platforms on a river, the ii-th platform has length cici (so the ii-th platform takes cici consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed nn.

You are standing at 00 and want to reach n+1n+1 somehow. If you are standing at the position xx, you can jump to any position in the range [x+1;x+d][x+1;x+d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 00 and n+1n+1belong to wooden platforms.

You want to know if it is possible to reach n+1n+1 from 00 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.

Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).

For example, if n=7n=7, m=3m=3, d=2d=2 and c=[1,2,1]c=[1,2,1], then one of the ways to reach 88 from 00 is follow:

The first example: n=7n=7.
Input

The first line of the input contains three integers nn, mm and dd (1n,m,d1000,mn1≤n,m,d≤1000,m≤n) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.

The second line of the input contains mm integers c1,c2,,cmc1,c2,…,cm (1cin,i=1mcin1≤ci≤n,∑i=1mci≤n), where cici is the length of the ii-th platform.

Output

If it is impossible to reach n+1n+1 from 00, print NO in the first line. Otherwise, print YES in the first line and the array aa of length nn in the second line — the sequence of river cells (excluding cell 00 and cell n+1n+1).

If the cell ii does not belong to any platform, aiai should be 00. Otherwise, it should be equal to the index of the platform (11-indexed, platforms are numbered from 11 to mm in order of input) to which the cell ii belongs.

Note that all aiai equal to 11 should form a contiguous subsegment of the array aa of length c1c1, all aiai equal to 22 should form a contiguous subsegment of the array aa of length c2c2, ..., all aiai equal to mm should form a contiguous subsegment of the array aa of length cmcm. The leftmost position of 22 in aa should be greater than the rightmost position of 11, the leftmost position of 33 in aa should be greater than the rightmost position of 22, ..., the leftmost position of mm in aa should be greater than the rightmost position of m1m−1.

See example outputs for better understanding.

Examples
input
Copy
7 3 2
1 2 1
output
Copy
YES
0 1 0 2 2 0 3 
input
Copy
10 1 11
1
output
Copy
YES
0 0 0 0 0 0 0 0 0 1 
input
Copy
10 1 5
2
output
Copy
YES
0 0 0 0 1 1 0 0 0 0 
Note

Consider the first example: the answer is [0,1,0,2,2,0,3][0,1,0,2,2,0,3]. The sequence of jumps you perform is 0245780→2→4→5→7→8.

Consider the second example: it does not matter how to place the platform because you always can jump from 00 to 1111.

Consider the third example: the answer is [0,0,0,0,1,1,0,0,0,0][0,0,0,0,1,1,0,0,0,0]. The sequence of jumps you perform is 056110→5→6→11.

 

#include <bits/stdc++.h>
#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
const int mxn = 1e6+5;
ll n,m,k,ans,cnt,col;
int a[mxn];
int main()
{
    while(cin>>n>>m>>k)
    {
        ans = 0;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i];
            ans+=a[i];
        }
        if(k==1)
        {
            if(ans==n)
            {
                cout<<"YES"<<endl;
                for(int i=1;i<=m;i++)
                {
                    while(a[i])
                    {
                        cout<<i<<" ";
                        a[i]--;
                    }
                }
                cout<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
        else if(ans+(k-1)*(m+1)<n)
            cout<<"NO"<<endl;
        else
        {
            cnt = n-ans;
            cout<<"YES"<<endl;
            for(int i=1;i<=m;i++)
            {
                for(int i=1;i<k && cnt;i++)
                {
                    cout<<0<<" ";
                    cnt--;
                }
                for(int j=1;j<=a[i];j++)
                {
                    while( a[i]  )
                    {
                        cout<<i<<" ";
                        ans--;
                        a[i]--;
                    }
                }
            }
            for(int i=1;i<=cnt;i++)
                cout<<0<<" ";
            cout<<endl;
        }
    }
    return 0;
}

 

 

D. Binary String Minimizing

output
standard output

You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1').

In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than kk moves? It is possible that you do not perform any moves at all.

Note that you can swap the same pair of adjacent characters with indices ii and i+1i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.

You have to answer qq independent test cases.

Input

The first line of the input contains one integer qq (1q1041≤q≤104) — the number of test cases.

The first line of the test case contains two integers nn and kk (1n106,1kn21≤n≤106,1≤k≤n2) — the length of the string and the number of moves you can perform.

The second line of the test case contains one string consisting of nn characters '0' and '1'.

It is guaranteed that the sum of nn over all test cases does not exceed 106106 (n106∑n≤106).

Output

For each test case, print the answer on it: the lexicographically minimum possible string of length nn you can obtain from the given one if you can perform no more than kk moves.

Example
input
Copy
3
8 5
11011010
7 9
1111100
7 11
1111100
output
Copy
01011110
0101111
0011111
Note

In the first example, you can change the string as follows: 110–––1101010–––111010011110–––1001110–––1100110–––111001011110110_11010→10_111010→011110_10→01110_110→0110_1110→01011110.

In the third example, there are enough operations to make the string sorted.

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int mxn = 1e6+5;
#define ll long long
#define s(x) scanf("%d",&x)
#define sc(x,y) scanf("%d %d",&x,&y)
#define sca(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define TLE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll n,m,k,ans,cnt,t,a[mxn];
string str,sub;
char ch;
int main()
{
    TLE;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>str;
        k=0;
        for(int i=0;i<n;i++)
        {
            if(str[i]=='0')
            {
                if(m>=i-k)
                {
                    swap( str[k],str[i] );
                    m -= i-k;
                    k++;
                }
                else
                {
                    swap( str[i],str[i-m] );
                    break;
                }
            }
        }
        cout<<str<<endl;
    }
    return 0;
}

 E. Yet Another Division Into Teams

 

There are nn students at your university. The programming skill of the ii-th student is aiai. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 21052⋅105 students ready for the finals!

Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of kk students with programming skills a[i1],a[i2],,a[ik]a[i1],a[i2],…,a[ik], then the diversity of this team is maxj=1ka[ij]minj=1ka[ij]maxj=1ka[ij]−minj=1ka[ij]).

The total diversity is the sum of diversities of all teams formed.

Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.

Input

The first line of the input contains one integer nn (3n21053≤n≤2⋅105) — the number of students.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109), where aiai is the programming skill of the ii-th student.

Output

In the first line print two integers resres and kk — the minimum total diversity of the division of students and the number of teams in your division, correspondingly.

In the second line print nn integers t1,t2,,tnt1,t2,…,tn (1tik1≤ti≤k), where titi is the number of team to which the ii-th student belong.

If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.

Examples
input
Copy
5
1 1 3 4 2
output
Copy
3 1
1 1 1 1 1 
input
Copy
6
1 5 12 13 2 15
output
Copy
7 2
2 2 1 1 2 1 
input
Copy
10
1 2 5 129 185 581 1041 1909 1580 8150
output
Copy
7486 3
3 3 3 2 2 2 2 1 1 1 
Note

In the first example, there is only one team with skills [1,1,2,3,4][1,1,2,3,4] so the answer is 33. It can be shown that you cannot achieve a better answer.

In the second example, there are two teams with skills [1,2,5][1,2,5] and [12,13,15][12,13,15] so the answer is 4+3=74+3=7.

In the third example, there are three teams with skills [1,2,5][1,2,5], [129,185,581,1041][129,185,581,1041] and [1580,1909,8150][1580,1909,8150] so the answer is 4+912+6570=74864+912+6570=7486.

 

划分数-贪心-模拟

 

#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
const int mxn = 2e5+10;
ll n,m,k,ans,cnt,col;
int dp[mxn],pro[mxn],flag[mxn];
string str,ch ;
pair <int,int> mp[mxn];
int main()
{
    while(cin>>n)
    {
        memset(dp,inf,sizeof(dp));
        for(int i=1;i<=n;i++)
            cin>>mp[i].first , mp[i].second=i;
        sort(mp+1,mp+1+n); dp[1] = 0;
        for(int i=1;i<=n;i++)
        {
            for(int k=2;k<=4 && i+k<=n;k++)
            {
                int ans = mp[i+k].first-mp[i].first;  //计算区间差值
                if(dp[i+k+1]>dp[i]+ans)
                {
                    dp[i+k+1] = dp[i]+ans;
                    pro[i+k+1] = i ;                  //存储方案 i ~ i+k 为一组
                }
            }
        }
        cnt = 1 , col = n+1;
        while(col!=1)
        {
            for(int i=col-1;i>=pro[col];i--)
            {
                flag[mp[i].second] = cnt;
            }
            cnt++;
            col=pro[col];
        }
        cout<<dp[n+1]<<" "<<--cnt<<endl;
        for(int i=1;i<=n;i++)
            cout<<flag[i]<<" ";
        cout<<endl;
    }
    return 0;
}

 

F. Equalizing Two Strings

You are given two strings ss and tt both of length nn and both consisting of lowercase Latin letters.

In one move, you can choose any length lenlen from 11 to nn and perform the following operation:

  • Choose any contiguous substring of the string ss of length lenlen and reverse it;
  • at the same time choose any contiguous substring of the string tt of length lenlen and reverse it as well.

Note that during one move you reverse exactly one substring of the string ss and exactly one substring of the string tt.

Also note that borders of substrings you reverse in ss and in tcan be different, the only restriction is that you reverse the substrings of equal length. For example, if len=3len=3 and n=5n=5, you can reverse s[13]s[1…3] and t[35]t[3…5], s[24]s[2…4] and t[24]t[2…4], but not s[13]s[1…3]and t[12]t[1…2].

Your task is to say if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves.

You have to answer qq independent test cases.

Input

The first line of the input contains one integer qq (1q1041≤q≤104) — the number of test cases. Then qq test cases follow.

The first line of the test case contains one integer nn (1n21051≤n≤2⋅105) — the length of ss and tt.

The second line of the test case contains one string ss consisting of nn lowercase Latin letters.

The third line of the test case contains one string tt consisting of nn lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 21052⋅105 (n2105∑n≤2⋅105).

Output

For each test case, print the answer on it — "YES" (without quotes) if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves and "NO" otherwise.

Example
input
Copy
4
4
abcd
abdc
5
ababa
baaba
4
asdf
asdg
4
abcd
badc
output
Copy
NO
YES
NO
YES

 

 

//

 

 

 

所遇皆星河
原文地址:https://www.cnblogs.com/Shallow-dream/p/11828462.html