Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- bool sh=0;
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t=1;
- if (!sh) cin>>t;
- for (int go=0;go<t;++go){
- int n,q; cin>>n>>q;
- vector <int> a(n); for (int &i: a) cin>>i;
- vector <vector <pii> > dp(n, vector <pii>(2));
- dp[0][0] = {1,q};
- if (q < a[0]){
- dp[0][0] = {1, q-1};
- }
- dp[0][1] = {0,q};
- vector <vector<int>> way(n, vector <int>(2,-1));//j=0<->taken on (i-1) ; j = 1 <-> skip on (i-1)
- for (int i = 1; i < n; ++i){
- //take
- //A
- int A=-1,B,C=-1,D, qA, qB, qC, qD;
- A = dp[i-1][0].first + 1;
- qA = dp[i-1][0].second;
- if (dp[i-1][0].second < a[i]) qA--;
- //B
- B = dp[i-1][1].first + 1;
- qB = dp[i-1][1].second;
- if (dp[i-1][1].second < a[i]) qB--;
- if (dp[i-1][0].second > 0 && dp[i-1][1].second > 0){
- if (A > B){
- dp[i][0] = {A, qA};
- }
- else{
- dp[i][0] = {B, qB};
- }
- }
- else if (dp[i-1][0].second > 0){
- dp[i][0] = {A, qA};
- }
- else if (dp[i-1][1].second > 0){
- dp[i][0] = {B, qB};
- }
- else{
- dp[i][0] = dp[i-1][0];
- }
- if (dp[i][0] == pii{A, qA}){
- way[i][0] = 0;
- }
- else {
- way[i][0] = 1;
- }
- //skip
- //C
- C = dp[i-1][0].first;
- qC = dp[i-1][0].second;
- D = dp[i-1][1].first;
- qD = dp[i-1][1].second;
- if (C > D){
- dp[i][1] = {C, qC};
- way[i][1] = 0;
- }
- else{
- dp[i][1] = {D, qD};
- way[i][1] = 1;
- }
- }
- if (sh){
- cout<<"dp\n";
- for (int i = 0; i < n; ++i){
- for (int j = 0; j < 2; ++j){
- cout<<"("<<dp[i][j].first<<","<<dp[i][j].second<<") ";
- }
- cout<<"\n";
- }
- cout<<"\n";
- }
- string ans;
- int j;
- if (dp[n-1][0].first > dp[n-1][1].first){
- ans += '1';
- j=0;
- }
- else{
- ans += '0';
- j=1;
- }
- for (int i=n-1;i>0;--i){
- if (way[i][j] == 0){
- ans += '1';
- j = 0;
- }
- else {
- ans += '0';
- j = 1;
- }
- }
- //if (!sh)
- reverse(ans.begin(), ans.end());
- if (sh) cout<<"ans\n";
- cout<<ans<<"\n";
- }
- }
- /*
- 2 1
- 1 2
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement