Educational Codeforces Round 130 (Rated for Div. 2) 题解A-C

比赛通道

A. Parkway Walk

可以选择在第一个房子直接休息到最大值,然后一路过去。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
	T x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){ if(c == '-') s = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
	a = x * s;
	return ;
}

int n; 
ll m; 

int main(){
  // freopen("hh.txt", "r", stdin); 
  int T; read(T);
  while(T--){
    read(n); read(m); 
    vector <int> a; 
    ll sum = 0; 
    for(int i = 1; i <= n; i++){
      int x; read(x);
      a.push_back(x); 
      sum += x; 
    }
    ll ans = max(0ll, sum - m); 
    cout << ans << endl; 
  }
  return 0;
}

B. Promo

用前缀和即可。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
	T x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){ if(c == '-') s = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
	a = x * s;
	return ;
}

int n, Q;
int a[N]; 

bool cmp(int a, int b){
  return a > b; 
}

ll sum[N]; 

int main(){
  // freopen("hh.txt", "r", stdin); 
  read(n), read(Q);
  for(int i = 1; i <= n; i++){
    read(a[i]); 
  }
  sort(a + 1, a + n + 1, cmp); 
  for(int i = 1; i <= n; i++)
    sum[i] = sum[i-1] + a[i]; 
  while(Q--){
    int l, r; 
    read(l), read(r); 
    ll ans = sum[l] - sum[l - r];
    cout << ans << endl;  
  }
  return 0;
}

C. awoo's Favorite Problem

可以发现,一次交换相当于把 \(b\) 左右移动,而 \(a\)\(c\) 的相对位置是不变的。所以,可以把字母 \(b\) 的移动区间存下来,排序后一一对应。(注意特判 \(b\) 的数量不一样的时候。)
接着,还要判断 \(a\)\(c\) 的相对位置是否可以匹配上,两个串在忽略 \(b\) 以后必须完全一样。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
	T x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){ if(c == '-') s = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
	a = x * s;
	return ;
}

int n; 
char s[N]; 
char h[N]; 

struct node{
  int l, r;

  bool operator < (const node &a) const{
    return l < a.l || (l == a.l && r < a.r); 
  } 

} t[N]; 
vector <int> G; 
stack <int> stac; 

int main(){
  // freopen("hh.txt", "r", stdin); 
  // freopen("out.txt", "w", stdout); 
  int T; read(T);
  while(T--){
    int num = 0; 
      // 记录 h 中 b 的位置
    read(n);
    scanf("%s", s + 1); 
    scanf("%s", h + 1); 
    G.clear(); 
    while(!stac.empty()) stac.pop(); 

    int lc = 0; 
    for(int i = 1; i <= n; i++){
      if(s[i] == 'b') stac.push(++num), t[num].l = lc + 1, t[num].r = 1e9;  
      else if(s[i] == 'c') lc = i; 
      else if(s[i] == 'a'){
        while(!stac.empty()){
          t[stac.top()].r = i - 1; 
          stac.pop(); 
        }
      }
    }

    bool flag = 1; 
    int p = 1, q = 1; 
    while(p <= n && q <= n){
      while(s[p] == 'b') p++; 
      while(h[q] == 'b') q++; 
      if(s[p] != h[q]) flag = 0; 
      p++, q++; 
    }

    for(int i = 1; i <= n; i++)
      if(h[i] == 'b') G.push_back(i); 

    sort(t + 1, t + num + 1); 

    if(num != G.size()) flag = 0; 
    int sol = 0, now = 1;  // 消耗第几个 b
    while(now <= num && sol < G.size()){
      // printf("now: %d sol: %d\n", now, sol); 
      if(G[sol] >= t[now].l && G[sol] <= t[now].r){
        now++; sol++; 
      }
      else{
        flag = 0; 
        break; 
      }
    } 
    if(flag) puts("YES");
    else puts("NO"); 
  }

  return 0;
}

posted @ 2022-11-03 21:36  雪之下,树之旁  阅读(18)  评论(0编辑  收藏  举报