제출 #335614

#제출 시각아이디문제언어결과실행 시간메모리
335614amunduzbaevGift (IZhO18_nicegift)C++14
100 / 100
798 ms148804 KiB
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define int long long
#define ll long long 
#define ld long double 
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 1e6+5;

vector<pll> ans[N];
vector<int> aa[N];
int a[N], sub[N];

void solve(){
	
	int n, k, sum = 0, mx = 0;
	bool f = 1;
	scanf("%lld %lld", &n, &k);
	for(int i=0;i<n;i++){
		scanf("%lld", &a[i]);
		sum += a[i];
		mx  = max(a[i], mx);
	}
	if(sum % k != 0 || sum / k < mx){
		printf("-1\n");
		return;
	}
	int cur = 0, cnt = 0, tmp = sum / k;
	for(int i=0;i<n;i++){
		if(cur + a[i] >= tmp){
			ans[cnt++].pb({tmp - cur, i+1});
			cur = cur + a[i] - tmp;
			if(cur) ans[cnt].pb({cur, i+1});
		}else{
			cur += a[i];
			ans[cnt].pb({a[i], i+1});
		}
	}
	cnt = 0;
	while(f){
		f = 0;
		int mn = 9e18;
		for(int i=0; i<k; i++)
			mn = min(ans[i][sz(ans[i]) -1].ff, mn);
		sub[cnt] = mn;
		for(int i=0; i<k; i++){
			pll tmp1 = ans[i][sz(ans[i]) -1];
			ans[i].pop_back();
			tmp1.ff -= mn;
			aa[cnt].pb(tmp1.ss);
			if(tmp1.ff) ans[i].pb(tmp1);
			f |= (!ans[0].empty());
		}
		cnt++;
	}
	printf("%lld\n", cnt);
	for(int i=0; i<cnt; i++){
		printf("%lld ", sub[i]);
		for(int j=0; j<sz(aa[i]); j++){
			printf("%lld ", aa[i][j]);
		}printf("\n");
	}
	
	return;
}

/*

4 2
2 3 3 2

*/

main(){
	fastios
	int t = 1;
	//cin>>t;
	while(t--) solve();
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

nicegift.cpp:87:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   87 | main(){
      |      ^
nicegift.cpp: In function 'void solve()':
nicegift.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |  scanf("%lld %lld", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |   scanf("%lld", &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...