1.[Codeforces Round #662 (Div. 2)]
C. Pinkie Pie Eats Patty-cakes
题目大意:
求通过对数组进行排列使得可以使相同馅饼的最小距离达到的最大值
解题思路:
1.先求出相同馅饼的最大数量
2.求出有相同馅料最大数量的馅饼种类的数量
3.通过数学分析,推出公式(n-cnt*maxx)/(maxx-1)+cnt-1
(其中n-cnt*maxx为减去最大数量的所有相同馅料之后剩下的值,再除于间隔maxx-1,再加上相同馅料最大数量的馅饼种类的数量-1的值,即可得到最小距离可以达到的最大值)
代码:
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <iterator>
#include<math.h>
#define debug() puts("what the fuck")
#define ll long long
#define INF 0x3f3f3f3f
#define inf 0x3ffffffffffff
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int maxn=8000009;
const double pi = acos(-1.0);
const int N=40010;
using namespace std;
ll a[1000020];
map<ll,ll>mp;
void solve()
{
mp.clear();
ll n,cnt=0,maxx=0;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
mp[a[i]]++;
maxx=max(maxx,mp[a[i]]);
}
for(int i=1; i<=n; i++)
{
if(mp[i]==maxx)
cnt++;
}
cout<<(n-cnt*maxx)/(maxx-1)+cnt-1<<endl;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
2.[Codeforces Round #662 (Div. 2)]
B. Applejack and Storages
题目大意:
给出n根木棍的长度,有q次询问,每次‘+’代表添加一根给定长度的木棍,‘-’代表减少一根给定长度的木棍,求是否可以组成一个长方形和一个正方形。
解题思路:
一开始想用暴力+优化解决,结果T了,又好好看了看数据范围,发现行不通,后来发现直接模拟就好了qwq。用map来记录某长度的木棍的数量,用c2,c4来记录有两根相同长度的木棍的数量和有四根相同长度的木棍的长度数量,然后模拟。
代码:
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <iterator>
#include<math.h>
#define debug() puts("what the fuck")
#define ll long long
#define INF 0x3f3f3f3f
#define inf 0x3ffffffffffff
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int maxn=8000009;
const double pi = acos(-1.0);
const int N=40010;
using namespace std;
map<int,int>mp;
int a[1000020];
int main()
{
int n,c2=0,c4=0;
cin>>n;
mp.clear();
for(int i=1; i<=n; i++)
{
cin>>a[i];
mp[a[i]]++;
if(mp[a[i]]==4)
c4++,mp[a[i]]=0; //记得清零,以免对接下来剩余的木棍的计数产生影响
if(mp[a[i]]==2)
c2++;
}
int q;
cin>>q;
while(q--)
{
char ch;
int x;
getchar();
cin>>ch>>x;
if(ch=='+')
{
mp[x]++;
if(mp[x]==4)
c4++,mp[x]=0; //记得清零,以免对接下来剩余的木棍的计数产生影响
if(mp[x]==2)
c2++;
}
else
{
mp[x]--;
if(mp[x]==-1)
mp[x]=3;
if(mp[x]==3)
c4--;
if(mp[x]==1)
c2--;
}
if(c4>=2||(c4==1&&c2>2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
3.Codeforces Round #655 (Div. 2) C. Omkar and Baseball
解题思路:
关键点是分析出并证得最多需要2个特殊交换来排序排列。如果数组已经排好序,则需要0个特殊交换即可;如果可以在排列中选择一个子序列,使得子序列中包含的所有元素都不匹配,除该子序列之外的元素都匹配,则需要1个特殊的交换来对数组进行排序;否则,若在该子序列中包含的元素中含有匹配的元素,则需要2个特殊的交换来对数组进行排序
代码:
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <iterator>
#include<math.h>
#define debug() puts("what the fuck")
#define ll long long
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int maxn=8000009;
const double pi = acos(-1.0);
using namespace std;
int a[200020];
int main()
{
ll t,flag;
cin>>t;
ll n;
while(t--)
{
flag=0;
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
int i=1,j=n;
while(a[i]==i&&i<=n)
i++;
while(a[j]==j&&j>=1)
j--;
if(i==(n+1)&&j==0)
cout<<0<<endl;
else
{
for(int k=i; k<=j; k++)
{
if(a[k]==k)
{
flag=1;
break;
}
}
if(flag==0)
cout<<1<<endl;
else
cout<<2<<endl;
}
}
return 0;
}