简单总结一下,这场考试还是挺梦幻的。

T1

一眼二分,但是写挂了(不过最近几次考试跟疯了一样的考gcd)

事实上不需要二分也可以解决,大概过程如下:

首先设以 ii 为分母,然后枚举在以 ii 为分母的情况下所有的可能性。

当然不是所有了

操作大概如下:

题中给出 ff 为期望浮点数,将其乘以枚举的 ii ,得到下面这些。

设有 f=zy\large f=\frac{z}{y} ,那么乘完之后就有 z×iy\large \frac{z \times i}{y} 。

然后枚举偏移量 ,枚举的所有偏移量为 ±1和0\pm 1 和 0 , 至于为啥,因为这样就够了 。

然后可以得到 z×i±yy\Large \frac{z \times i \pm y}{y} 这样一个东西,现在我们要还原他。

把他除以 ii ,得到了 z×i±yy×i\Large \frac{z \times i \pm y}{y \times i} ,可以把这个数算出来变成浮点数。

然后与给定期望的 ff 进行对比,如果答案比上次优就更新答案。

Code:Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>
using namespace std;
#define ll long long

double _abs(double x){
return x*(x<0?-1:1);
}

double f;
ll ans1,ans2,n;
ll up,down;

int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);

scanf("%lld%lf",&n,&f);

long double Dlt=100;
for(int i=1;i<=n;i++){
ll tmp=f*i;//计算期望值
for(int j=-1;j<=1;j++){
ll tmp1=tmp+j;//枚举偏移量±1和0
long double x=1.0*tmp1/i;//将偏移后的值还原
if(_abs(x-f)-Dlt<0){//如果比上次优
up=tmp1,down=i;//更新答案
Dlt=_abs(x-f);
}
}
}

ll k=(ll)__gcd(up,down);//约分
if(!k) up=0,down=1;
else up/=k,down/=k;
printf("%lld %lld\n",up,down);

return 0;
}

T2

梦幻 T2 。

贪心的去排序,队伍里可以处理的先处理掉,然后再把剩下的序列(此时剩下的序列都是类似 b,gb,g 多次重复或单个的了)按照“女多左男多右”的排序思想进行一次 sortsort ,然后按题意不断消去,统计个数后再与前面列内处理的答案加上就可以了。

Code:Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<bits/stdc++.h>
using namespace std;
#define ll long long

#define maxn 100001

char s[maxn*10];
ll n;

struct rec{
ll girl,boy;
}z[maxn];

bool operator<(const rec &a,const rec &b)
{
if(a.girl>a.boy&&b.girl>b.boy) return a.boy<b.boy;//男生多的放序列右侧
else if(a.girl>a.boy&&b.girl<=b.boy) return true;//相对交换
else if(a.girl<=a.boy&&b.girl>b.boy) return false;//相对交换
else if(a.girl<=a.boy&&b.girl<=b.boy) return a.girl>b.girl;//女生多放序列左侧
}

int main()
{
scanf("%lld",&n);

ll ans=0;

for(int i=1;i<=n;i++){
scanf("%s",s+1);
ll len=strlen(s+1);
ll x=0,y=0;
for(int j=1;j<=len;j++){
if(s[j]=='g') g++;
else if(x) g--,ans++;
else y++;
}
z[i].girl=x;
z[i].boy=y;
}

sort(z+1,z+n+1);

ll ret=0;

for(int i=1;i<=n;i++){
ll Dlt=min(ret,z[i].boy);
ans+=Dlt;
ret=ret-Dlt+z[i].girl;
}

printf("%lld\n",ans*2);

return 0;
}

这有份爆搜代码,复杂度是 O(n!)O(n!) 的,我也贴在这里吧(还有部分分解法)。

Code2:Code2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll cnt1,cnt2,ans,n;
stack <char> stk;
stack <char> tmp;
string s[100001];
bool vis[100001];
bool flag=true ;
char c;

void dfs(ll st,ll num){
if(num==n){
for(int i=0;i<s[st].size();i++) stk.push(s[st][i]);
ll b=0,tot=0;
while(stk.size()){
tmp.push(stk.top());
if(stk.top()=='b') b++;
else if((stk.top()=='g')&&b>0) b--,tot++;
// cout<<stk.top();
stk.pop();
}
// puts("");
while(tmp.size()){
stk.push(tmp.top());
tmp.pop();
}
ans=max(ans,tot);
for(int i=0;i<s[st].size();i++) stk.pop();
return ;
}
vis[st]=true ;
for(int i=0;i<s[st].size();i++) stk.push(s[st][i]);
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i,num+1);
for(int i=0;i<s[st].size();i++) stk.pop();
vis[st]=false ;
}

int main()
{
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);

scanf("%lld",&n);
for(int i=1;i<=n;i++) cin>>s[i];
for(int i=1;i<=n;i++)
if(s[i].size()!=1) {flag=false ;break ;}
if(flag){
for(int i=1;i<=n;i++)
s[i][0]=='g'?cnt1++:cnt2++;
return printf("%lld\n",(min(cnt1,cnt2))*2),0;
}
for(int i=1;i<=n;i++) dfs(i,1);
printf("%lld\n",ans*2);
return 0;
}

T3

诶呀 T3 这回可真是神仙打架了。

其实我的做法挺简单的,直接按题意模拟,拜访到固定叶节点后进行判断,如果已经有一条边连接到此点了就跳过这条边。

然而被 Wind_185mmHg 给 hack 了。

heckheck

1
2
3
4
5
3 2 2
1 2 114514
1 3 1919810
2 3 1
2 3

这个问题就出在 KruskalKruskal 算法上了。

按边权排序后第一条选择的边是 2 3 然而这样的话会发现没法构造一颗最小生成树,但是实际上是可以选择 1 2 和 1 3 这两条边的!

所以想了个法,特判。

但是这个特判实在是太难写了, 所以留给在看这篇博客的你吧!

只好贴上我不完美的代码了……

Code:Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
using namespace std;
#define ll long long

inline ll read(){
ll num=0;bool f=0;char c;
while((c=getchar())=='\n'||c=='\r'||c==' ');
c=='-'?f=1:num=c^48;
while(isdigit(c=getchar()))
num=(num<<3)+(num<<1)+(c^48);
return (f?-1:1)*num;
}

struct edge{
ll x,y,v;
friend bool operator <(const edge &a,const edge &b){
return a.v<b.v;
}
}f[200001];
bool poi[200001];
ll tot[200001];
ll fa[200001];
ll n,m,k;

ll find(ll x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void unions(ll x,ll y){
fa[y]=fa[x];
}

ll kruskal1(){//用来拿部分分的
ll cnt=0,ans=0;ll st,ed;
for(register int i=1;i<=m;i++){
st=find(f[i].x),ed=find(f[i].y);
if(st==ed) continue ;
ans+=f[i].v;
unions(st,ed);
if(++cnt==n-1) break;
}
return ans;
}

ll kruskal2(){//被hack的
ll cnt=0,ans=0;ll st,ed;
for(int i=1;i<=m;i++){
st=find(f[i].x),ed=find(f[i].y);
if((poi[f[i].x]&&tot[f[i].x])||
(poi[f[i].y]&&tot[f[i].y])) continue ;//叶节点特判!
if(st==ed) continue ;
ans+=f[i].v;
unions(st,ed);
tot[ed]++,tot[st]++;
if(++cnt==n-1) break;
}
if(cnt<n-1) ans=-1;
return ans;
}

int main()
{
freopen("c.in","r",stdin);
freopen("c.out","w",stdout);

n=read(),m=read(),k=read();
for(int i=1;i<=m;i++)
f[i].x=read(),f[i].y=read(),f[i].v=read();
for(int i=1;i<=k;i++) poi[read()]=true ;

sort(f+1,f+m+1);
for(int i=1;i<=m;i++) fa[i]=i;

if(k==0) return printf("%lld\n",kruskal1()),0;
else return printf("%lld\n",kruskal2()),0;

return 0;
}

T4

推式子, 好耶

好个头!

不会推了,建议左转找 5K_sync_closer 。

总之放上代码吧。

Code:Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define ll long long
#define maxn 200001

ll fc[maxn],cnt[maxn];
ll n,m,fac[maxn],kill;

ll bin_pow(ll x,ll y){//快速幂,求逆元用的
ll ret=1,bit=x;
while(y){
if(y&1) (ret*=bit)%=mod;
(bit*=bit)%=mod;
y>>=1;
}
return ret%mod;
}

int main()
{
scanf("%lld%lld",&n,&m);fac[0]=1;

for(int i=1;i<=n*2;i++)
fac[i]=(fac[i-1]*i%mod);
for(int i=0;i<=n*2;i++)
fc[i]=bin_pow(fac[i],mod-2);

for(int i=n/2+1;i<=n;i++){
ll x=(2*i-n-1)/2;
cnt[i]=fac[i-1]*bin_pow(fc[2],x)%mod*fc[x]%mod;
}
ll sum=0;

for(int i=1;i<=n;i++)
sum=(sum+cnt[i])%mod;
sum=bin_pow(sum,mod-2);
for(int i=1;i<=n;i++)
cnt[i]=cnt[i]*sum%mod;

for(int i=1;i<=m;i++){
scanf("%lld",&kill);
printf("%lld\n",cnt[n-kill+1]);
}

return 0;
}

EndEnd

结束了哦。

别看啦。

真的不走吗?

那勉为其难发一张图吧!