题解:P15630 [2019 KAIST RUN Spring] Gosu
link
Solution
Part 1: Floyd,Dijkstra,和 BFS
我们可以把这个当成一个图,然后对每个人跑一遍最短路,最短路中最长的路最短的那个人就是 Gosu。
结果与时间复杂度:
- Floyd:
O(n^3) ; - Dijkstra:
O(n^3 \log n) ; - BFS(普通):
O(n^3) ; - BFS(带剪枝):
O(n^2) 到O(n^3) 。
没有一个是过的。
Part 2: 正解
可以证明如果一个人的获胜次数最多,那么他的弱点要么是
:::info[证明]
假设获胜最多的人
所以获胜次数最多的人的弱点要么是
如果获胜最多的人把其他人都击败了,那么他的弱点是
Code
:::success[code]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18;
const i128 Inf=1e35;
str s;
ll n,mx,pos;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s;
ll cnt=count(s.begin(),s.end(),'W');
if(cnt==n-1)
{
cout<<"1 "<<i;
return 0;
}
if(cnt>mx) mx=cnt,pos=i;
}
cout<<"2 "<<pos;
}
:::