N Problems During K Days
生活随笔
收集整理的這篇文章主要介紹了
N Problems During K Days
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://codeforces.com/contest/1157/problem/D
題意:找到一個長度為k的元素大于0的數組,并且,整個數組的和等于n
題解:因為數組嚴格遞增,數組最小和k*(k+1)/2,而i-n的最小和為(k-i+1)*(k-i+2)/2,對于這個公式可以加上一個首項,增加i-n的和,對第i個元素在i-n的和不應該大于剩下未分配的n,當然應該滿足
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int #define endl "\n" using namespace std; typedef long long ll; //typedef __int128 lll; const int N=100000+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; ll t,n,m,k,p,l,r,u,v; int ans,cnt,flag,temp,sum; int a[N]; char str; struct node{}; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//while(t--){scanf("%I64d%I64d",&n,&k);if(n<k*(k+1)/2){cout<<"NO"<<endl;return 0;}for(int i=1;i<=k;i++){ll base=(k-i+1ll)*(k-i+2ll)/2;ll num=max(1ll,i==1?(n-base)/(k-i+1ll)+1:min(2ll*a[i-1],(n-base)/(k-i+1ll)+1));//cout<<base<<" "<<num<<endl;if(num>a[i-1]&&(num<=a[i-1]*2||i==1)){n-=num;a[i]=num;}else{cout<<"NO"<<endl;return 0;}}if(n){cout<<"NO"<<endl;return 0;}cout<<"YES"<<endl;for(int i=1;i<=k;i++)printf("%d%c",a[i]," \n"[i==k]);//}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結
以上是生活随笔為你收集整理的N Problems During K Days的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Increasing Subsequen
- 下一篇: Minimum Array