CodeForces 496D Tennis Game (暴力枚举)

题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时,

游戏结束。现在给出n次对决的记录,问可能的s和t有多少种,并按s递增的方式输出。

析:如果枚举s 和 t,那么一定会超时的,所以我们考虑是不是可以不用全枚举。我们只要枚举 t ,然后每次都去计算 s。

首先我们先预处理两个人的获得第 i 分时是第几场比赛。然后每次枚举每个 t,每次我们都是加上t,所以总的时间复杂度为 n*logn。

完全可以接受,注意有几个坑,首先是数组不要越界,因为在t 比较大的时候,可能会超数组上限,再就是最后要判断是不是在最后一场结束时,

正好决出胜负,如果是提前决出胜负,那么也不是符合。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
vector<P> v;
int a[maxn], b[maxn];

int main(){
  while(scanf("%d", &n) == 1){
    v.clear();
    int cnt1 = 0, cnt2 = 0;
    memset(a, INF, sizeof a);
    memset(b, INF, sizeof b);
    for(int i = 1; i <= n; ++i){
      scanf("%d", &m);
      if(m == 1)  a[++cnt1] = i;
      else  b[++cnt2] = i;
    }
    for(int t = 1; t <= n; ++t){
      int i = 0, l = 0, r = 0;
      cnt1 = 0, cnt2 = 0;
      int c = 0;
      while(i < n){
        if(a[t+l] < b[t+r]){
          i = a[t+l];
          l += t;
          r = i - l;
          ++cnt1;  c = cnt1;
        }
        else {
          i = b[t+r];
          r += t;
          l = i - r;
          ++cnt2;  c = cnt2;
        }
      }
      if(i == n && cnt1 != cnt2 && max(cnt1, cnt2) == c)  v.push_back(P(c, t));
    }
    sort(v.begin(), v.end());
    printf("%d
", v.size());
    for(int i = 0; i < v.size(); ++i)
      printf("%d %d
", v[i].first, v[i].second);
  }
  return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/6516203.html