USACO 12月 2021-2022 December Contest Bronze 题解

你好啊

要准备usaco的铁铁们可以参考这个文章哦!USACO题库 - 比Usaco Training更好用的网站_GeekAlice的博客-CSDN博客

我刚比完赛,决定写一个这次青铜2021-22赛季的题解

我死在了第一题的最后一个测试点 

真的好无语

话不多说,先把题解发出来吧!


第一题

Problem 1. Lonely Photo

最后一个贼坑的测试点11注意哈


#include
#define ll long long int
using namespace std;
char s[500005];
ll g[500005];
ll h[500005];
int main(){
    ll n;
    cin>>n; 
    scanf("%s",s+1);
    for(ll i=1;i<=n;i++)
    {
        if(s[i]=='G')
        {
            g[i]=1+g[i-1];
            h[i]=h[i-1];
        }
        else
        {
            g[i]=g[i-1];
            h[i]=1+h[i-1];
        }
    }
    ll ans=0;
    for(ll l=1;l<=n-2;l++)
    {
        for(ll r=l+2;r<=n;r++)
        {
            if(g[r]-g[l-1]==1||h[r]-h[l-1]==1) ans++;
            if(g[r]-g[l-1]>=2&&h[r]-h[l-1]>=2) break;
        }
    }
    cout<

第二题

Problem 2. Air Cownditioning

前缀和sum[i]=p[j]−t[i−1]

轻松AC掉

#include 
using namespace std;

int main() {
	int N;
	cin >> N;
	vector p(N + 1), t(N + 1), d(N + 2);
	for (int i = 1; i <= N; ++i)
		cin >> p[i];
	for (int i = 1; i <= N; ++i)
		cin >> t[i];
	for (int i = 1; i <= N; ++i)
		d[i] = p[i] - t[i];
	int ans = 0;
	for (int i = 0; i <= N; ++i)
		ans += abs(d[i] - d[i + 1]);
	cout << ans / 2;
}

Problem 3. Walking Home

#include 
#include 
#include 

using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  vector g(n);
  for(int i = 0; i < n; i++) cin >> g[i];
  int ret = 0;
  if(k >= 1) {
    bool urcorner = true;
    bool dlcorner = true;
    for(int i = 0; i < n; i++) {
      if(g[0][i] == 'H' || g[i][n-1] == 'H') urcorner = false;
      if(g[i][0] == 'H' || g[n-1][i] == 'H') dlcorner = false;
    }
    ret += urcorner;
    ret += dlcorner;
  }
  if(k >= 2) {
    // use column j
    for(int j = 1; j < n-1; j++) {
      bool valid = true;
      for(int i = 0; i < n; i++) {
        if(g[i][j] == 'H') valid = false;
        if(i < j && g[0][i] == 'H') valid = false;
        if(i > j && g[n-1][i] == 'H') valid = false;
      }
      ret += valid;
    }
    // use row i
    for(int i = 1; i < n-1; i++) {
      bool valid = true;
      for(int j = 0; j < n; j++) {
        if(g[i][j] == 'H') valid = false;
        if(j < i && g[j][0] == 'H') valid = false;
        if(j > i && g[j][n-1] == 'H') valid = false;
      }
      ret += valid;
    }
  }
  if(k >= 3) {
    for(int i = 1; i < n-1; i++) {
      for(int j = 1; j < n-1; j++) {
        // RDRD
        bool valid = g[i][j] == '.';
        for(int a = 0; a < n; a++) {
          if(a <= i && g[a][j] == 'H') valid = false;
          if(a >= i && g[a][n-1] == 'H') valid = false;
          if(a <= j && g[0][a] == 'H') valid = false;
          if(a >= j && g[i][a] == 'H') valid = false;
        }
        ret += valid;
        valid = g[i][j] == '.';
        // DRDR
        for(int a = 0; a < n; a++) {
          if(a <= i && g[a][0] == 'H') valid = false;
          if(a >= i && g[a][j] == 'H') valid = false;
          if(a <= j && g[i][a] == 'H') valid = false;
          if(a >= j && g[n-1][a] == 'H') valid = false;
        }
        ret += valid;
      }
    }
  }
  cout << ret << "\n";
}
int main() {
  int t;
  cin >> t;
  while(t--) solve();
}

你可能感兴趣的:(USACO,Contest,c++,算法,usaco)