Submission #175589

#TimeUsernameProblemLanguageResultExecution timeMemory
175589juggernautGift (IZhO18_nicegift)C++14
100 / 100
759 ms134876 KiB
//Just try and the idea will come #include<bits/stdc++.h> #define int long long int using namespace std; int n,k,sum,mx,i,a[1000001],mn,sz,res,cnt[1000001],j,tmp; vector<pair<int,int>>vec[1000001]; vector<int>ans[1000001]; bool flag; main(){ scanf("%lld%lld",&n,&k); for(i=1;i<=n;i++)scanf("%lld",&a[i]),mx=max(mx,a[i]),sum+=a[i]; if(sum%k!=0||mx>sum/k){ puts("-1"); return 0; } tmp=0; for(i=1;i<=n;i++){ tmp+=a[i]; if(tmp>=sum/k){ vec[sz++].push_back({a[i]-tmp+sum/k,i}); tmp-=sum/k; if(tmp)vec[sz].push_back({tmp,i}); }else vec[sz].push_back({a[i],i}); } while(!flag){ mn=1e18; for(i=0;i<sz;i++)mn=min(mn,vec[i].back().first); cnt[res]=mn; flag=0; for(i=0;i<sz;i++){ ans[res].push_back(vec[i].back().second); if(vec[i].back().first==mn)vec[i].pop_back(); else vec[i].back().first-=mn; flag|=vec[i].empty(); } if(flag)for(i=0;i<sz;i++)if(!vec[i].empty())runtime_error(0); res++; } printf("%lld\n",res); for(i=0;i<res;i++){ printf("%lld",cnt[i]); for(j=0;j<k;j++)printf(" %lld",ans[i][j]); puts(""); } }

Compilation message (stderr)

nicegift.cpp:9:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
nicegift.cpp: In function 'int main()':
nicegift.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld%lld",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~~~~
nicegift.cpp:11:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1;i<=n;i++)scanf("%lld",&a[i]),mx=max(mx,a[i]),sum+=a[i];
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...