Codeforces Global Round 23 (A-E1)个人题解

A-Maxmina

给定一个01串,我们可以将k个数变为他们的最大值(k个数变成1个数),或者将相邻的两个数变为他们的最小值(2个数变成1个数),询问是否可以将这个01串变成仅含有一个1的串。

很容易证明,只要01串中有一个1,这个操作就是成立的。

点击查看代码
/*
 * @Author: Fczhao
 * @Date: 2022-10-15 22:36:19
 */
#include <bits/stdc++.h>
#define CF int T;std::cin >> T;while(T--){solve();}
using namespace std;
using LL = long long;
void solve(){
  int n, k; cin >> n >> k;
  vector<int> a(n);
  for(int i = 0; i < n; i++) cin >> a[i];
  bool ok = false;
  for(int i = 0; i < n; i++) if(a[i] == 1) ok = true;
  cout << (ok ? "YES\n" : "NO\n");
}
signed main(){
  #ifdef FCZHAO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
  #endif
  ios::sync_with_stdio(false);
  CF
  return 0;
}

B-Rebellion

给定一个01数组,我们可以令 \(a_j\) 加上 \(a_i\),然后将 \(a_i\) 删除,以上称之为一次操作,询问最少需要多少次操作使该数组单调不减。

很好理解,本题的最终状态是一个前面 a 个 0,后面 b 个 1 的01数组,每一次操作可以理解为将一个0和一个1交换位置,使用双指针从两边向中间扫一遍即可。

点击查看代码
/*
 * @Author: Fczhao
 * @Date: 2022-10-15 22:39:14
 */
#include <bits/stdc++.h>
#define CF int T;std::cin >> T;while(T--){solve();}
using namespace std;
using LL = long long;
void solve(){
  int n; cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++) cin >> a[i];
  int cnt = 0;
  int l = 0, r = n - 1;
  while(l < r) {
    while(l < n && a[l] == 0) ++l;
    while(r >= 0 && a[r] == 1) --r;
    if(l < r && l < n && r >= 0) {
      ++cnt;
      ++l, --r;
    }
  }
  cout << cnt << endl;
}
signed main(){
  #ifdef FCZHAO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
  #endif
  ios::sync_with_stdio(false);
  CF
  return 0;
}

C-Permutation Operations

施工中......

posted @ 2022-10-16 12:09  fczhao  阅读(170)  评论(0)    收藏  举报