跳到正文

题解

题解:P14477 图寻中国

Sharkat_Star约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P14477

版本

  1. v1当前

解题思路

因为要求可能的最多胜利局数,所以我们希望胜利局数越多越好(这不是废话吗喂),也就是失败局数越少越好。

最好的情况当然是连胜,可以证明,同样的局数下,连胜的情况一定比非连胜的情况胜利局数多(这个也很好想到,不做赘述)。

那我们现在的目标就是尽可能制造连胜。但是题面对连胜的局数给出了限制:连胜局数不能超过最长连胜局数 aa(要不然怎么叫最长连胜呢 … …)

所以,当连胜局数达到 aa 局时,接下来必须失败至少一局。因为要使失败局数尽可能少,我们就按失败一局算。

题面里还有一个限制:最长连败局数为 bb,那么至少要有一次持续 bb 局的连败出现。我们希望它只出现一次就够了。

那么一个最优的对决情况序列就是形如:

11…1⏟a个100…0⏟b个011…1⏟a个1011…1⏟a个10…⏟n个数 \underbrace{\underbrace{11\dots 1}_{a\text{个}1}\underbrace{00\dots 0}_{b\text{个}0}\underbrace{11\dots 1}_{a\text{个}1}0\underbrace{11\dots 1}_{a\text{个}1}0\dots}_{n\text{个数}}

我们要求序列中 aa 的数量,即:

num=a+⌊n−a−ba+1⌋×a+(n−a−b) mod (a+1) num=a+\lfloor\frac{n-a-b}{a+1}\rfloor\times a+(n-a-b)\bmod (a+1)

观察序列可以理解这个式子。

AC Code

C++
#include<bits/stdc++.h>
using namespace std;
int n,a,b,ans;
inline long long read(){
	char ch=getchar();
	long long res=0;
	int f=1;
	while(!isdigit(ch)){
		if(ch=='-'){
			f=-1;
		}
		ch=getchar();
	}
	while(isdigit(ch)){
		res=(res<<1)+(res<<3)+(ch^48);
		ch=getchar();
	}
	return res*f;
}
inline void print(long long x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9){
		print(x/10);
	}
	putchar((x%10)^48);
}
//上面是快读快写
inline void work(int n,int a,int b){
	int temp=n-a-b;
	ans=a+temp/(a+1)*a+temp%(a+1);
}
int main(){
	n=read();
	a=read();
	b=read();
	work(n,a,b);
	print(ans);
	return 0;
}
/*
 ____   _                     _
|  __| | |                   | |  __           _    __/\__
| |__  | |___   _____   ___  | | / /  _____   | |   \    /
|__  | |  _  | |  _  | |  _| | |/ /  |  _  | |   |  /____\
 __| | | | | | | |_| | | |   | |\ \  | |_| |  | |
|____| |_| |_| |___|_| |_|   |_| \_\ |___|_|  |_|           */
原文