A - Tit for Tat CodeForces - 1516A
生活随笔
收集整理的這篇文章主要介紹了
A - Tit for Tat CodeForces - 1516A
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
A - Tit for Tat CodeForces - 1516A
題意:
一個長度為 n 的數(shù)組,現(xiàn)在希望你對這個數(shù)組進(jìn)行至多k次操作:在數(shù)組中選擇 2 個不同的元素,將第一個元素加 1 ,然后從第二個元素減 1 。不過他有一個要求,進(jìn)行操作后數(shù)組中的所有元素都必須保持非負(fù)數(shù)。在進(jìn)行至多 k? 次操作之后,你能否獲得一個數(shù)組在字典序上最小呢?
題解:
要是的字典序最小,就要讓高位置盡可能減,低位置盡可能加,注意跳過0的情況
代碼:
#include<bits/stdc++.h> #define debug(a,b) printf("%s = %d\n",a,b); typedef long long ll; using namespace std;inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w; } const int maxn=400; int a[maxn]; int main() {int t;cin>>t;while(t--){memset(a,0,sizeof(a));int n,k;cin>>n>>k;for(int i=1;i<=n;i++){cin>>a[i];}int l=1,r=n;while(k&&l<r){while(a[l]==0)l++;if(l>=n)break;if(a[l]>0)a[l]--;a[r]++;k--; }for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的A - Tit for Tat CodeForces - 1516A的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #71
- 下一篇: 美的净水桶好用吗