ABC 371

· · 个人记录

ABC371

打崩了,嘿嘿。

A

大力粉桃

#include <bits/stdc++.h>

using namespace std;

char ac, bc, ab;

int main(){
  cin >> ab >> ac >> bc;
  if(ab == '>'){
    if(bc == '>') cout << 'B';
    else if(ac == '>') cout << 'C';
    else cout << 'A';
  }else{
    if(bc == '<') cout << 'B';
    else if(ac == '<') cout << 'C';
    else cout << 'A'; 
  }
  return 0;
}

B

标记数组

#include <bits/stdc++.h>

using namespace std;

const int N = 1e2 + 5;

int n, m, a[N];
char b[N];
int v[N], ans[N];

int main(){
  cin >> n >> m;
  for(int i = 1; i <= m; cin >> a[i] >> b[i], ++i);
  for(int i = 1; i <= m; ++i)
    if(b[i] == 'M' && v[a[i]] == 0)
      ans[i] = 1, v[a[i]] = 1;
  for(int i = 1; i <= m; ++i) cout << (ans[i] == 1 ? "Yes" : "No") << endl;
  return 0;
}

C

另一个 cxm 不会做,好耶。(但是 cxm 做出 F 了 \dots

枚举全排列。

#include <bits/stdc++.h>

using namespace std;

const int N = 8 + 5;

struct Edge{
  int to, c;
};

int n, mg, mh, g[N][N], h[N][N], a[N][N], ans = INT_MAX >> 1;

bool cmp(Edge &i, Edge &j){
  return i.c < j.c;
}

int main(){
  cin >> n >> mg;
  for(int i = 1, x, y; i <= mg; ++i){
    cin >> x >> y;
    g[x][y] = g[y][x] = 1;
  }
  cin >> mh;
  for(int i = 1, x, y; i <= mh; ++i){
    cin >> x >> y;
    h[x][y] = h[y][x] = 1;
  }
  for(int i = 1; i < n; ++i)
    for(int j = i + 1, x; j <= n; ++j){
      cin >> x;
      a[i][j] = a[j][i] = x;
    }
  int p[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  do{
    int cnt = 0;
    for(int i = 1; i <= n; ++i)
      for(int j = i + 1; j <= n; ++j)
        if(h[p[i]][p[j]] != g[i][j])
          cnt += a[p[i]][p[j]];
    ans = min(ans, cnt);
  }while(next_permutation(p + 1, p + n + 1));
  cout << ans;
  return 0;
}

D

二分 + 前缀和,都是板子型的,两个细节,所以赛时 WA 了两发。

#include <bits/stdc++.h>

#define int long long // 细节:#define int long long

using namespace std;

const int N = 2e5 + 5;

struct Vill{
  int x, p;
}p[N];

int n, fix[N];

bool cmp(Vill &i, Vill &j){
  return i.x < j.x;
}

int lowerbound(int x){
  int l = 1, r = n + 1; // 细节,手写二分时 r = n + 1
  while(l < r){
    int mid = l + r >> 1;
    if(p[mid].x >= x){
      r = mid;
    }else{
      l = mid + 1;
    }
  }
  return l;
}

int upperbound(int x){
  int l = 1, r = n + 1;// 细节,手写二分时 r = n + 1
  while(l < r){
    int mid = l + r >> 1;
    if(p[mid].x > x){
      r = mid;
    }else{
      l = mid + 1;
    }
  }
  return l;
}

signed main(){
  cin >> n;
  for(int i = 1; i <= n; ++i) cin >> p[i].x;
  for(int i = 1; i <= n; ++i) cin >> p[i].p;
  sort(p + 1, p + n + 1, cmp);
  for(int i = 1; i <= n; ++i) fix[i] = fix[i - 1] + p[i].p;
  int q; 
  cin >> q;
  while(q--){
    int l, r;
    cin >> l >> r;
    int u = lowerbound(l), v = upperbound(r) - 1;
    cout << fix[v] - fix[u - 1] << endl; // 前缀相减
  }
  return 0;
}

E

看到计数题,大力容斥:

显然,总共的情况是 \sum_{i = 1}^{i \leq n} \sum_{i = 1}^{i \leq n - i + 1} i。

对每个数值讨论:

如果有两个数值重复出现,那么设两个数值出现的位置为 i, j,答案会少 i \times (n - j + 1)。如果该数值有多个,那么暴力的去想,枚举 i, j,是一个 O(n^2) 的。优化的话就是:枚举 i,遍历一遍整个数值 vector 做个后缀和,统计位置在 i 之后的数 j 的 \sum n - j + 1。然后计算就行了,注意可能会重复减掉一个方案,要注意去重。

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e5 + 5;

int n, a[N], ans, dfix[N];

vector<int>cnt[N];

int function1(int x){ // 1 + 2 + ... + x = function1(x)
  return x + x * x >> 1;
}

signed main(){
  cin >> n;
  for(int i = 1; i <= n; cin >> a[i++]);
  for(int i = 1; i <= n; ++i) cnt[a[i]].push_back(i); // 存进 vector
  for(int i = 1; i <= n; ++i) ans += function1(n - i + 1); // 初始时的答案
  for(int i = 1; i <= n; ++i){
    dfix[cnt[i].size()] = 0;
    for(int j = cnt[i].size() - 1; j > -1; --j) dfix[j] = dfix[j + 1] + n - cnt[i][j] + 1; // 处理后缀和
    for(int j = 0; j < cnt[i].size(); ++j) ans -= (cnt[i][j] - (j > 0 ? cnt[i][j - 1] : 0)) * dfix[j + 1]; // 减去不合法答案 
  }
  cout << ans;
  return 0;
}

F

没看。

G

没看。

排名 1922,菜就多练。