补题:Codeforces Round #575 (Div. 3)

比赛入口


C Robot Breakout

做法:有用的位置就只有不能往左走的最左边的点a,不能往右走最右边的点c…上下也是一样,无效的情况就是xc < xa, 上下等效…没想到…

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 1e5;
int main() {
  int t; read(t);
  int n, x, y, a, b, c, d;
  int A, B, C, D;
  while(t--) {
    read(n);
    A = D = -N;
    B = C = N;
    for(int i = 0; i < n; ++i) {
      read(x); read(y); read(a); read(b); read(c); read(d);
      if(!a && x > A) A = x;
      if(!b && y < B) B = y;
      if(!c && x < C) C = x;
      if(!d && y > D) D = y;
    }
    if(A <= C && D <= B) printf("1 %d %d\n", (A+C) / 2, (B+D) / 2);
    else puts("0");
  }
  return 0;
}

D2 RGB Substring (hard version)

做法:要找在当前字符串中长度为k的子串最少修改多少个字符能使该子串变成RGBRGB…的子串,分别以R、G、B开头做一次前缀和,然后更新每个前缀和的时候顺便更新答案就行啦。

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 3e5;
char a[N], s[] = "RGB";
int n, k, ans, dp[N];
void f(int j) {
  for(int i = 1; i <= n; ++i) {
    dp[i] = dp[i-1] + (a[i] != s[j]);
    if(i >= k) ans = min(ans, dp[i]-dp[i-k]);
    j = (j + 1) % 3;
  }
}
int main() {
  int t; read(t);
  while(t--) {
    scanf("%d%d %s", &n, &k, a+1);
    ans = n;
    for(int i = 0; i < 3; ++i) f(i);
    printf("%d\n", ans);
  }
  return 0;
}

发布了28 篇原创文章 · 获赞 14 · 访问量 2950

猜你喜欢

转载自blog.csdn.net/qq_43408978/article/details/104099642