제출 #50858

#제출 시각아이디문제언어결과실행 시간메모리
50858NicksechkoGift (IZhO18_nicegift)C++14
100 / 100
1307 ms89064 KiB
//Solution by Zhusupov Nurlan
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string.h>
#include <stdio.h>
#include <assert.h>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;
typedef pair<LL, int> PII;

#define endl '\n'
#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base LL(1e9 + 7)
#define fname ""
#define sz 2000 * 1002
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define mp make_pair

LL mx, n, k, a[sz];
LL s;
set <PII> cur;
priority_queue <PII> w;
pair <LL, LL> g[sz];
vector <pair<LL, VI> > ans;
PII e[sz];

int main()
{
//    freopen(fname"in", "r", stdin);
//    freopen(fname"out", "w", stdout);

  scanf("%d %d", &n, &k);
  w.push(mp(0, 0));
  for (int i = 1; i <= n; i++)
  {
      scanf("%lld", &a[i]);
      s += a[i];
      mx = max(mx, a[i]);
      PII buf = mp(a[i], i);
      if (cur.size() < k || (*cur.begin()) < buf) {
        if (cur.size() == k) {
          w.push(*cur.begin());
          cur.erase(*cur.begin());
        }
        cur.insert(buf);
      } else {
        w.push(buf);
      }
  }
  if (s % k != 0 || mx > s / k) {
    cout << -1;
    return 0;
  }

  vector <int> p;
  for (int i = 0; i < k; i++) p.pb(0);

  int vv = 0;
  while (1) {
    PII l = *cur.begin(), r = w.top(), ma = *cur.rbegin();
    LL diff = min(l.F, max(s/k - r.F, (s/k - ma.F)/2));
    s -= diff * k;
    auto it = cur.begin();
    for (int i = 0; i < k; i++, it++) {
      p[i] = (*it).S;
    }
    ans.pb(mp(diff, p));
    /*
    for (auto x : cur) {
      cerr << x.F << " " << x.S << endl;
    }
    cerr << "&" << endl;
    for (auto x : w) {
      cerr << x.F << " " << x.S << endl;
    }*/
    if (s == 0) break;
    for (int i = 0; i < k; i++) {
      PII l = *cur.rbegin(), r = w.top();
      cur.erase(l);
      l.F -= diff;
      if (l > r) {
        e[i] = l;
      } else {
        e[i] = r;
        w.pop();
        if (l.F > 0)
          w.push(l);
      }
    }
    for (int i = 0; i < k; i++) {
      cur.insert(e[i]);
    }
    //cerr << "$" << endl;
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) {
    printf("%lld ", ans[i].F);
    for (int j = 0; j < ans[i].S.size(); j++)
      printf("%d ", ans[i].S[j]);
    puts("");
  }
}

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

nicegift.cpp: In function 'int main()':
nicegift.cpp:60:24: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'LL* {aka long long int*}' [-Wformat=]
   scanf("%d %d", &n, &k);
                  ~~    ^
nicegift.cpp:60:24: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'LL* {aka long long int*}' [-Wformat=]
nicegift.cpp:68:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (cur.size() < k || (*cur.begin()) < buf) {
           ~~~~~~~~~~~^~~
nicegift.cpp:69:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (cur.size() == k) {
             ~~~~~~~~~~~^~~~
nicegift.cpp:123:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<long long int, std::vector<int> > >::size_type {aka long unsigned int}' [-Wformat=]
   printf("%d\n", ans.size());
                  ~~~~~~~~~~^
nicegift.cpp:124:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ans.size(); i++) {
                   ~~^~~~~~~~~~~~
nicegift.cpp:126:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < ans[i].S.size(); j++)
                     ~~^~~~~~~~~~~~~~~~~
nicegift.cpp:86:7: warning: unused variable 'vv' [-Wunused-variable]
   int vv = 0;
       ^~
nicegift.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &k);
   ~~~~~^~~~~~~~~~~~~~~~~
nicegift.cpp:64:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       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...