CNMARBLE - Trò chơi với những viên bi

Tác giả: ll931110

Ngôn ngữ: C++

#include <iostream>
#include <queue>
using namespace std;

struct rec
{
    int val,pos;
    bool operator<(const rec& r) const 
    {
        return val < r.val;
    };
};

int main()
{
    int n,m,x;
    rec tmp;
    rec r[200001];
    priority_queue <rec> heap;
    
//    freopen("cn.in","r",stdin);
//    freopen("cn.ou","w",stdout);
    
    scanf("%d%d", &n, &m);
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &x);
        tmp.val = x;
        tmp.pos = i;
        heap.push(tmp);
        sum += x;
    };
    
    for (int i = 0; i < sum/m; i++)
    {
        int count = 0;        
        for (int j = 0; j < m; j++)
        {
          r[count] = heap.top();
          heap.pop();
          printf("%d ", r[count].pos + 1);                     
          r[count].val--;
          if (r[count].val > 0) count++;
        };
        
        for (int j = 0; j < count; j++) heap.push(r[j]);        
        printf("\n");
    };
};

Download