比赛链接 https://codeforces.com/contest/1328
比赛记录 https://blog.csdn.net/cheng__yu_/article/details/105395197
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int t;
ll a,b;
int main()
{
cin>>t;
while(t--)
{
cin>>a>>b;
ll ans=b-(a%b);
if(a%b==0)
ans=0;
cout<<ans<<endl;
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,maxm=1e5+5;
const int inf=0x7f7f7f7f,mod=998244353;
int t;
ll n,k,a[maxn];
int main()
{
for(ll i=1;i<=maxn;++i)
a[i]=(1+i)*i/2;
cin>>t;
while(t--)
{
cin>>n>>k;
int p1=lower_bound(a+1,a+maxn,k)-a-1;
int p2=k-a[p1];
p1=n-p1-1;
p2=n-p2+1;
//cout<
for(int i=1;i<=n;++i)
if(i==p1||i==p2)
printf("b");
else
printf("a");
puts("");
}
return 0;
}
题意:把一个 3 进制的数拆分成两个数,要求最大值最小
思路:遇到 2 就平均分,遇到第一个 1 的时候分给其中一个,后面的数都分给第二个
实现:就两种状态,遇到 1 之后,改变状态,只运行一种方法就行了
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,maxm=1e5+5;
const int inf=0x7f7f7f7f,mod=998244353;
int t,n;
string s;
int main()
{
cin>>t;
while(t--)
{
cin>>n>>s;
s="0"+s;
string ans1,ans2;
bool f=0;
for(int i=1;i<=n;++i)
{
if(f)
{
ans2+=s[i];
ans1+="0";
continue;
}
if(s[i]=='2')
ans1+="1",ans2+="1";
else if(s[i]=='1')
ans1+="1",ans2+="0",f=1;
else
ans1+="0",ans2+="0";
}
cout<<ans1<<"\n"<<ans2<<"\n";
}
return 0;
}
题意:给 n 个动物围成一圈染色,要求相邻的不同动物不能染相同的颜色。相同动物可以随便染色
#include <bits/stdc++.h>
using namespace std;
int solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
if (count(a.begin(), a.end(), a[0]) == n) {
cout << 1 << endl;
for (int i = 0; i < n; ++i) {
cout << 1 << " ";
}
cout << endl;
return 0;
}
if (n % 2 == 0) {
cout << 2 << endl;
for (int i = 0; i < n; ++i) {
cout << i % 2 + 1 << " ";
}
cout << endl;
return 0;
}
for (int i = 0; i < n; ++i) {
if (a[i] == a[(i + 1) % n]) {
vector<int> ans(n);
for (int j = 0, pos = i + 1; pos < n; ++pos, j ^= 1) {
ans[pos] = j + 1;
}
for (int j = 0, pos = i; pos >= 0; --pos, j ^= 1) {
ans[pos] = j + 1;
}
cout << 2 << endl;
for (int pos = 0; pos < n; ++pos) {
cout << ans[pos] << " ";
}
cout << endl;
return 0;
}
}
cout << 3 << endl;
for (int i = 0; i < n - 1; ++i) {
cout << i % 2 + 1 << " ";
}
cout << 3 << endl;
return 0;
}
int main() {
int q;
cin >> q;
for (int qq = 0; qq < q; qq++) {
solve();
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,inf=1e9;
int head[maxn],cnt;
struct Edge
{
int nxt,to;
}edges[maxn<<1];
void add(int u,int v)
{
edges[++cnt].to=v;
edges[cnt].nxt=head[u];
head[u]=cnt;
}
int fa[maxn][25],depth[maxn];
void dfs(int u,int f)
{
fa[u][0]=f;
depth[u]=depth[fa[u][0]]+1;
for(int i=1;i<=20;++i)
fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u];i!=-1;i=edges[i].nxt)
{
int v=edges[i].to;
if(v!=f) dfs(v,u);
}
}
int lca(int u,int v)
{
if(depth[u]>depth[v]) swap(u,v);
int dis=depth[v]-depth[u];
for(int j=0;dis;j++)
{
if(dis&1) v=fa[v][j];
dis>>=1;
}
if(u==v) return u;
for(int i=20;i>=0;--i)
{
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
}
return fa[u][0];
}
bool cmp(int a,int b)
{
return depth[a]<depth[b];
}
int n,m,k,v[maxn];
int main()
{
cin>>n>>m;
memset(head,-1,sizeof(head));
cnt=-1;
for(int i=1;i<=n-1;++i)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(1,0);
while(m--)
{
cin>>k;
for(int i=1;i<=k;++i) cin>>v[i];
sort(v+1,v+1+k,cmp);
int mx=v[k];
bool ok=1;
for(int i=1;i<k;++i)
{
if(lca(v[i],mx)==v[i]||lca(v[i],mx)==fa[v[i]][0]) continue;
ok=false;
break;
}
puts(ok?"YES":"NO");
}
return 0;
}
题意:每次只能让最大的数减 1 ,或者让最小的数加 +1,问至少需要操作几次才能够让数组有 k 个数字相等
思路:排完序后,枚举每一个位置,前后统一一下答案
a n s = { a [ i ] × i − ∑ j = 1 i a [ i ] − ( i − k ) ( i ≥ k ) ∑ j = i + 1 n a [ i ] − a [ i ] × ( n − i ) − ( n − i + 1 − k ) ( i + k − 1 ≤ n ) a [ i ] × i − ∑ j = 1 i a [ i ] + ∑ j = i + 1 n a [ i ] − a [ i ] × ( n − i ) − ( n − k ) ans= \begin{cases} a[i]\times i - \sum_{j=1}^ia[i]- (i-k) \ (i\ge k)\\ \sum_{j=i+1}^{n}a[i]-a[i]\times (n-i)- (n-i+1-k) \ (i+k-1\le n)\\ a[i]\times i - \sum_{j=1}^ia[i] + \sum_{j=i+1}^{n}a[i]-a[i]\times (n-i)- (n-k)\\ \end{cases} ans=⎩⎪⎨⎪⎧a[i]×i−∑j=1ia[i]−(i−k) (i≥k)∑j=i+1na[i]−a[i]×(n−i)−(n−i+1−k) (i+k−1≤n)a[i]×i−∑j=1ia[i]+∑j=i+1na[i]−a[i]×(n−i)−(n−k)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,maxm=1e5+5;
const int inf=0x7f7f7f7f,mod=998244353;
int n,k;
ll a[maxn],pref[maxn];
map<int,int> m;
int main()
{
cin>>n>>k;
bool ok=0;
for(int i=1;i<=n;++i)
{
cin>>a[i];
m[a[i]]++;
if(m[a[i]]>=k)
ok=1;
}
if(ok)
{
puts("0");
return 0;
}
sort(a+1,a+1+n);
for(int i=1;i<=n;++i)
pref[i]=pref[i-1]+a[i];
ll ans=9e18;
for(int i=1;i<=n;++i)
{
if(i>=k)
ans=min(ans,a[i]*i-pref[i]-(i-k));
if(i+k-1<=n)
ans=min(ans,pref[n]-pref[i]-a[i]*(n-i)-(n-i+1-k));
ans=min(ans,a[i]*i-pref[i]+pref[n]-pref[i]-a[i]*(n-i)-(n-k));
}
cout<<ans<<"\n";
return 0;
}