Codeforces Round #558 (Div. 2)
作者:互联网
A. Eating Soup
The three friends, Kuro, Shiro, and Katie, met up again! It's time for a party...
What the cats do when they unite? Right, they have a party. Since they wanted to have as much fun as possible, they invited all their friends. Now nn cats are at the party, sitting in a circle and eating soup. The rules are simple: anyone having finished their soup leaves the circle.
Katie suddenly notices that whenever a cat leaves, the place where she was sitting becomes an empty space, which means the circle is divided into smaller continuous groups of cats sitting next to each other. At the moment Katie observes, there are mm cats who left the circle. This raises a question for Katie: what is the maximum possible number of groups the circle is divided into at the moment?
Could you help her with this curiosity?
You can see the examples and their descriptions with pictures in the "Note" section.
题意:\(n\)个点围成一圈,从中取走\(m\)个点.问剩下的点最多被空格分成几个组.
题解:
1. 如果\(m=0\),为\(1\)组.
2. 找规律发现,如果 \(m<=n/2\) 时,最多为 \(m\) 组.否则为 \(n-m\) 组.
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
if(m==0) cout<<1<<endl;
else if(m<=n/2) cout<<m<<endl;
else{
cout<<n-m<<endl;
}
return 0;
}
B1. Cat Party (Easy Edition)
B2. Cat Party (Hard Edition)
This problem is same as the next one, but has smaller constraints.
Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.
For each of the nn days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the ii-th day has a ribbon with color uiui. Shiro wants to know the largest number xx, such that if we consider the streak of the first xx days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining x−1x−1will have the same number of occurrences.
For example, consider the following sequence of uiui: [2,2,1,1,5,4,4,5][2,2,1,1,5,4,4,5]. Then x=7x=7 makes a streak, since if we remove the leftmost ui=5ui=5, each ribbon color will appear exactly twice in the prefix of x−1x−1 days. Note that x=8x=8 doesn't form a streak, since you must remove exactly one day.
Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.
题意: 给一组数,求最大的 \(x\) ,使得从前 \(x\) 个数中,取走一个数后,剩下的 \(x-1\) 个数中每一种数出现的个数都相等.
题解:
B1中数的范围是1~10,可以直接暴力. 枚举每一种数,检查减少一个这个数的数量,剩下每一种数的数量是否都相等.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int b[11];
int a[N];
int cnt;
int ck(){
for(int i=1;i<=10;i++){
if(b[i]==0) continue;
b[i]--;
int f=1;
int tmp=0;
for(int j=1;j<=10;j++){
if(b[j]==0) continue;
if(tmp==0) tmp=b[j];
if(tmp!=b[j]) f=0;
}
b[i]++;
if(f) return 1;
}
return 0;
}
int main(){
int n;
scanf("%d",&n);
int ans=1;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
if(b[a[i]]==0) cnt++;
b[a[i]]++;
if(ck()){
ans=i;
}
}
cout<<ans<<endl;
return 0;
}
B2的数的范围变大.暴力肯定超时.
分析一下,发现以下四种情况,符合条件:
- \(n\) 为1
- 每一种数都只出现一次.
- 一种数只出现一次,其他数出现的次数都相等.
- 某种数出现的次数比其他数的数量都多一个,且其他的数的数量都相等.
可以用一个数组统计数的次数的个数.如 \(1, 2, 3, 4, 4\) .f[1]=1, f[2]=1, f[3]=1, f[4]=2. cnt[2]=1,cnt[1]=3.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int f[N],cnt[N];
int main(){
int n;
cin>>n;
int tmp,ans;
int mx=0;
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
cnt[f[tmp]]--;
f[tmp]++;
cnt[f[tmp]]++;
mx=max(mx,f[tmp]);
bool ok=false;
if(cnt[1]==1&&i==1) ok=true;//n为1.
else if(cnt[1]==i) ok=true;//每一种数都只出现一次.
else if(cnt[1]==1&&cnt[mx]*mx==i-1) ok=true;
//一种数只出现一次,其他数出现的次数都相等.
else if(cnt[mx]==1&&cnt[mx-1]*(mx-1)==i-mx) ok=true;
//某种数出现的次数比其他数的数量都多一个,且其他的数的数量都相等.
if(ok) ans=i;
}
cout<<ans<<endl;
return 0;
}
标签:streak,558,int,house,Codeforces,they,Div,circle,Shiro 来源: https://www.cnblogs.com/-yjun/p/10845169.html