A. Grass Field
有一个大小为 2×2 的草地,一次可以把某行某列的草除掉(1变0)问要几次
答:就0,1,2三种答案
int t;
cin>>t;
while(t--)
{
int an=0;
int x;
for(int i=1;i<=4;i++)
{
cin>>x;
if(x)an++;
}
if(an==0)cout<<"0\n";
else
if(an==4)cout<<"2\n";
else cout<<"1\n";
}
B. Permutation
答:要使pi*d=pi+1 的对数越多,d必然选2
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 2e5+5;
bool vis[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n;
memset(vis,0,sizeof(vis));
cin>>n;
cout<<2<<'\n';
//k=n/2;
int cn=0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
cn=i;
while(cn<=n)
{
cout<<cn<<' ';
vis[cn]=1;
cn=cn*2;
}
}
}
cout<<'\n';
}
return 0;
}
C - Schedule Management
日程管理
有 n 个工人和 m 个任务。工人从 1 到 n 编号。每个任务 i 都有一个值 ai——精通该任务的工人的编号。
答:二分+贪心,枚举每个工人工作时间
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 2e5 + 5;
vector<int>v[N];
int n, m;
bool check(int k)//每个人的工作时间
{
ll now;
ll need = 0;
for (int i = 1; i <= n; i++)
{
now = v[i].size();
// cout<<i<<' '<<now<<endl;
if (now <= k)
{
need -= ((ll)(k - now) / 2);
}
else
{
need += (ll)(now - k);
}
}
if (need <= 0)return 1;
return 0;
}
void solve()
{
cin >> n >> m;//n个工人,m个工作
int x;
for (int i = 1; i <= m; i++)
{
cin >> x;
v[x].push_back(i);
}
int l = 0, r = m * 2;
int ans = 0;
while (l <= r)
{
int mid = (l + r) / 2;
// cout<<"l r "<<l<<' '<<r<<endl;
if (check(mid))
{
r = mid - 1;
ans = mid;
}
else
{
l = mid + 1;
}
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
memset(v, 0, sizeof(v));
}
return 0;
}
D - Permutation Restoration
答:另一个帖子写了