A.World Final? World Cup! (I)【2023牛客寒假算法基础集训营1】

A.World Final? World Cup! (I)

原题链接

题意

  • AB两队ABABABABAB形式轮流罚球共10次,给出01串代表每次罚球的结果,问是否能在第x球后知道比赛结果,能则输出x,若10轮内没有分出胜负则输出-1

思路

  1. 胜利条件:胜球数多
  2. 如果第x球后就知道比赛结果,获胜的一队的【进球数】便大于失败的一队的【进球数+剩余罚球机会】(即使后面罚球中该队全不中,另一队全中,也不能够对结果产生影响) -> 需要记录各自【进球数】和【剩余罚球机会】
  3. 如果10轮内不能分出胜负即最后【罚球机会】均为0 且 【进球数】相等

代码

点击查看代码
#include<iostream>

using namespace std;

#define prep(i,a,b) for(int i = (a); i <= (b); i ++)
#define rrep(i,a,b) for(int i = (a); i >= (b); i --)

typedef long long LL;
const char nl = '\n';
int T, n, m;
char a[11];
int aw,bw,ar = 5,br = 5;

void solve() {
    aw = 0,bw = 0,ar = 5,br = 5;	//每次需要进行初始化
	cin >> a + 1;	//字符串的输入
	prep(i,1,10){
	//奇偶判断AB
		if(i%2)ar--;	//罚球机会--
		else br--;

		if(a[i] == '1'){
			if(i%2)aw++;	//进球数++
			else bw++;
		}
        if(aw > bw + br || bw > aw + ar){
            cout << i << nl;
            return;
        }
	}
	if(aw == bw)cout << -1 << nl;

}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> T;
	while (T--) {
		solve();
	}

	return 0;
}
jiangly
void solve() {
    std::string s;
    std::cin >> s;
     
    int res[2] = {5, 5};	//residue剩余		//记录剩余场次
    int score[2] = {0, 0};		//记录得分
    for (int i = 0; i < 10; i++) {
        int x = i % 2;
        res[x]--;
        score[x] += s[i] - '0';	//可以省去判断
        if (score[0] + res[0] < score[1] || score[1] + res[1] < score[0]) {
            std::cout << i + 1 << "\n";
            return;
        }
    }
     
    std::cout << -1 << "\n";
}

细节

  1. 记得初始化
posted @ 2023-01-18 17:16  Keith-  阅读(29)  评论(0编辑  收藏  举报