답안 #378860

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
378860 2021-03-17T06:30:32 Z Seanliu Gift (IZhO18_nicegift) C++14
0 / 100
2 ms 1004 KB
#include <iostream>
#include <map>
#include <deque>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e2 + 326;
map<int, deque<int>> mp;
vector<vector<int>> ans;

int N, K, arr[maxN];
int sm[maxN];

struct Obj{
	int num;
	deque<int> poss;
	Obj(){}
	Obj(int num, deque<int> dq): num(num), poss(dq){}
};

deque<Obj> has[maxN];

signed main(){
	cin >> N >> K;
	for(int i = 1; i <= N; i++){
		cin >> arr[i];
		if(!mp.count(arr[i])){
			mp[arr[i]] = deque<int>();
		}
		mp[arr[i]].push_back(i);
	}
	for(auto &[v, vec] : mp){
		while(vec.size() >= K){
			vector<int> tmp = vector<int>();
			tmp.push_back(v);
			for(int i = 0; i < K; i++){
				tmp.push_back(vec.front());
				vec.pop_front();
			}
			ans.push_back(tmp);
		}
		if(vec.size()){
			has[vec.size()].emplace_back(v, vec);
			sm[vec.size()] += v;
		}
	}
	for(int i = 1; i < K && i <= (K - i); i++){
		if(sm[i] != sm[K - i]){
			cout << -1 << endl;
			return 0;
		}
		while(has[i].size()){
			vector<int> tmp = vector<int>();
			tmp.push_back(min(has[i].front().num, has[K - i].front().num));
			for(int x : has[i].front().poss) tmp.push_back(x);
			for(int x : has[K - i].front().poss) tmp.push_back(x);
			ans.push_back(tmp);
			if(tmp[0] == has[i].front().num) has[i].pop_front();
			else {
				has[i][0].num -= tmp[0];
			}
			if(tmp[0] == has[K - i].front().num) has[K - i].pop_front();
			else {
				has[K - i][0].num -= tmp[0];
			}
		}
	}
	cout << ans.size() << endl;
	for(auto v : ans){
		for(int x : v) cout << x << " ";
		cout << endl;
	}
}

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:33:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |  for(auto &[v, vec] : mp){
      |            ^
nicegift.cpp:34:20: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   34 |   while(vec.size() >= K){
      |         ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB n=4
2 Incorrect 1 ms 620 KB Same heap occurs twice
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB n=4
2 Incorrect 1 ms 620 KB Same heap occurs twice
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB n=4
2 Incorrect 1 ms 620 KB Same heap occurs twice
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1004 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB n=4
2 Incorrect 1 ms 620 KB Same heap occurs twice
3 Halted 0 ms 0 KB -