답안 #335058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335058 2020-12-11T03:29:28 Z amunduzbaev Gift (IZhO18_nicegift) C++14
0 / 100
40 ms 47340 KB
#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 ll long long 
#define ld long double 
#define pii pair<int, int>
#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<pii>v[N];
vector<ll>ans[N]; 
int a[N];

void solve(){
	int n, k, mx, sum;
	cin>>n>>k;
	for(int i=0; i<n; i++){
		cin>>a[i];
		mx = max(a[i], mx);
		sum += a[i];
	}
	if(mx * k > sum || sum % k != 0){
		cout<<-1;
		return;
	}
	
	int c = 0, cnt = 0;
	int size = sum/k;
	
	for(int i=0; i<n; i++){
		if(c + a[i] >= size){
			int x = c + a[i] - size;
			v[cnt++].pb({a[i] - x, i + 1});
			c = (a[i] - (a[i] - x));
			v[cnt].pb({c, i + 1});
        }
		else{
			c += a[i];
			v[cnt].pb({a[i],i+1});
		}
	}
	bool f = 1;
	cnt = 0;
	while(f){
		int mn = inf;
		for(int i = 0; i < k; i++)  mn = min(mn, v[i].back().ff);
		ans[cnt].pb(mn);
		for(int i = 0; i < k; i++){
			pii x = v[i].back();
			v[i].pop_back();
			x.ff -= mn;
			ans[cnt].pb(x.ss);
			if(x.ff > 0) v[i].pb(x);
		}
		f = (v[0].empty() ? 0:1);
		cnt++;
	}
	cout<<cnt<<"\n";
	for(int i = 0; i < cnt; i++){
		for(int j = 0; j < sz(ans[i]); j++){
			cout<<ans[i][j]<<" ";
		}cout<<"\n";
	}
	return;
}

/*

4 2
2 3 3 2

*/

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

Compilation message

nicegift.cpp: In function 'void solve()':
nicegift.cpp:33:8: warning: 'mx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |  if(mx * k > sum || sum % k != 0){
      |     ~~~^~~
nicegift.cpp:26:16: warning: 'sum' may be used uninitialized in this function [-Wmaybe-uninitialized]
   26 |  int n, k, mx, sum;
      |                ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 47340 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 47340 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 47340 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 40 ms 47336 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 47340 KB Jury has the answer but participant has not
2 Halted 0 ms 0 KB -