A 咪咪游戏

Solution



Code

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N = 4e5 + 5;
const ll mod = 19961993;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
static auto faster_iostream = []() {
    std::ios::sync_with_stdio(false);  // turn off sync
    std::cin.tie(nullptr);             // untie in/out streams
    return 0;
}();
string tmp = "mq";
int main(){
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    int now = 0;
    int flag = 0;
    for(int i = 0; i < s.size(); i++){
      if(s[i] != tmp[now]){
        flag = 1;
        break;
      }
      else {
        now = (now + 1) % tmp.size();
      }
    }
    //cout << flag << ' ' << now << "\n";
    cout << ((!flag && !now) ? "Yes\n" : "No\n");
  }
  return 0;
}

B 三角形

Solution




Code

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N = 1e6 + 5;
const ll mod = 19961993;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
static auto faster_iostream = []() {
    std::ios::sync_with_stdio(false);  // turn off sync
    std::cin.tie(nullptr);             // untie in/out streams
    return 0;
}();
ll a[105], b[N];
int main(){
  int n, k;
  cin >> n >> k;
  int cnt = 0;
  int cur = 0;
  priority_queue<int> q;
  q.push(0);
  while(n--){
    int m;
    cin >> m;
    for(int j = 1; j <= m; j++) cin >> a[j];
    sort(a + 1, a + 1 + m);
    cnt = 0, cur = 0;
    while(!q.empty())  b[++cnt] = q.top(), q.pop();
    for(int i = 1; i <= m; i++){
      for(int j = cnt; j >= 1; j--){
        if(cur < k) q.push(a[i] + b[j]), ++cur; // 堆小于k 放入
        else if(q.top() <= a[i] + b[j]) // 堆大小为k, 因为a[i] and b[j] 已经排序 后面不用看了
          break;
        else {
          q.pop(), q.push(a[i] + b[j]); // 更新
        }
      }
    }
  }
  cnt = 0;
  while(!q.empty()){
    b[++cnt] = q.top(), q.pop();
  }
  ll ans = 0;
  for(int i = 1; i <= cnt; i++){
    ans += b[i];
  }
  cout << ans << "\n";
  return 0;
}

C 区间加

Solution
















Code

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N = 1e6  + 5;
const ll mod = 998244355;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
static auto faster_iostream = []() {
    std::ios::sync_with_stdio(false);  // turn off sync
    std::cin.tie(nullptr);             // untie in/out streams
    return 0;
}();
ll dp[N];
ll a[N];
int main(){
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= n; i++) cin >> a[i];
  if(a[1] > m || a[1] < m - 1){
    cout << 0 << "\n";
    return 0;
  }
  dp[1] = 1;
  for(int i = 2; i <= n; i++){
    if(m - a[i] > i || a[i] > m || m - a[i] > n - i + 1){
      cout << 0 << "\n";
      return 0;
    }
    if(a[i - 1] - a[i] == 1){
      dp[i] = dp[i - 1];
    }
    else if(a[i] - a[i - 1] == 1){
      dp[i] = dp[i - 1] * (m - a[i - 1]) % mod;
    }
    else if(a[i] == a[i - 1]){
      dp[i] = dp[i - 1] + dp[i - 1] * (m - a[i - 1]) % mod;
      dp[i] %= mod;
    }
  }
  cout << dp[n] << "\n";
  return 0;
}

D 多元组

Solution






Code

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N = 1e5  + 5;
const ll mod = 1e9 + 7;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
static auto faster_iostream = []() {
    std::ios::sync_with_stdio(false);  // turn off sync
    std::cin.tie(nullptr);             // untie in/out streams
    return 0;
}();
ll t[55][N];
ll ans[55][N];
int a[N];
int b[N];
int lowbit(int x){
  return x & (-x);
}
void update(int id, int x, int add){
  while(x < N){
    t[id][x] += add;
    t[id][x] %= mod;
    x += lowbit(x);
  }
}
ll query(int id, int x){
  ll res = 0;
  while(x){
    res +=  t[id][x];
    res %= mod;
    x -= lowbit(x);
  }
  return res;
}
int main(){
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
  sort(b + 1, b + 1 + n);
  int len = unique(b + 1, b + 1 + n) - b - 1;
  for(int i = 1; i <= n; i++){
    a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b; // 离散化
  }
  for(int i = 1; i <= n; i++){
    ans[1][i] = 1; // 自己本身肯定可以组成一元组啦
  }
  for(int i = 1; i <= n; i++){
    update(1, a[i], 1); // 把自己本身构成的一元组插入树状数组
    for(int j = 2; j <= m; j++){
      ans[j][i] = ans[j][i] + query(j - 1, a[i] - 1); // j - 1元组里 小于等于a[i - 1]的都统计
      update(j, a[i], ans[j][i]); // 更新一下 a[i]所能构成的j元组
      ans[j][i] %= mod;
    }
  }
  ll cal = 0;
  for(int i = 1; i <= n; i++){
    //cout << cal << "\n";
    cal += ans[m][i];
    cal %= mod;
  }
  cout << cal << "\n";
  return 0;
}