CF1342D. Multiple Testcases(优先队列)

So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests… Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem!

Initially, each test in that problem is just an array. The maximum size of an array is . For simplicity, the contents of arrays don’t matter. You have tests — the -th test is an array of size (1≤≤).

Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than 1 arrays of size greater than or equal to 1 (≥1), no more than 2 arrays of size greater than or equal to 2, …, no more than arrays of size greater than or equal to . Also, 1≥2≥⋯≥.

So now your goal is to create the new testcases in such a way that:

each of the initial arrays appears in exactly one testcase;
for each testcase the given conditions hold;
the number of testcases is minimum possible.
Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase.

Input
The first line contains two integers and (1≤,≤2⋅105) — the number of initial tests and the limit for the size of each array.

The second line contains integers 1,2,…, (1≤≤) — the sizes of the arrays in the original tests.

The third line contains integers 1,2,…, (≥1≥2≥⋯≥≥1); is the maximum number of arrays of size greater than or equal to you can have in a single testcase.

Output
In the first line print a single integer (1≤≤) — the minimum number of testcases you can achieve.

Each of the next lines should contain the description of a testcase in the following format:

1 2 … (1≤≤) — the testcase includes arrays, is the size of the -th array in that testcase.

Each of the initial arrays should appear in exactly one testcase. In particular, it implies that the sum of over all testcases should be equal to .

Note that the answer always exists due to ≥1 (and therefore 1≥1).

If there are multiple answers, you can output any one of them.

Examples
inputCopy
4 3
1 2 2 3
4 1 1
outputCopy
3
1 2
2 1 3
1 2
inputCopy
6 10
5 8 1 10 8 7
6 6 4 4 3 2 2 2 1 1
outputCopy
2
3 8 5 7
3 10 8 1
inputCopy
5 1
1 1 1 1 1
5
outputCopy
1
5 1 1 1 1 1
inputCopy
5 1
1 1 1 1 1
1
outputCopy
5
1 1
1 1
1 1
1 1
1 1
Note
In the first example there is no way to distribute the tests into less than 3 testcases. The given answer satisfies the conditions: each of the testcases includes no more than 4 arrays of size greater than or equal to 1 and no more than 1 array of sizes greater than or equal to 2 and 3.

Note that there are multiple valid answers for this test. For example, testcases with sizes [[2],[1,2],[3]] would also be correct.

However, testcases with sizes [[1,2],[2,3]] would be incorrect because there are 2 arrays of size greater than or equal to 2 in the second testcase.

Note the difference between the third and the fourth examples. You can include up to 5 arrays of size greater than or equal to 1 in the third example, so you can put all arrays into a single testcase. And you can have only up to 1 array in the fourth example. Thus, every array should be included in a separate testcase.

题意:
有n个数。划分成一些组。要求是每个组内大于大于等于 i i i的数不超过 c [ i ] c[i] c[i]
求划分出最少的组数和构成方案。

思路:
气死了,代码中没加while,赛中一直T在13组。连续两场比赛D题都是赛后改了一点就A了(上次的DP也是因为不够优化,fst了)。但是理论上没有while最多也不就是 n l o g n nlogn nlogn吗?可能是优先队列中加了 v e c t o r vector vector导致效率变低的吧。

很明显可以想到,尽量满足 i i i值大的数最优。因为 i i i越大, c [ i ] c[i] c[i]越小,限制既然越大,那么先安排,后面再让限制小的插入。用一个优先队列维护。

ACNEW
把vector放到外面,常数优化极大。
定义 v e c t o r < i n t > G [ i ] vectorG[i] vector<int>G[i]代表最多放 i i i个的数字有哪些。
然后按照能放的数目从小到大维护即可。

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;

struct Node {
    int num,pos;
    bool operator < (const Node &rhs)const {
        return num > rhs.num;
    }
};
priority_queue<Node>q;

vector<int>G[maxn];
vector<int>ans[maxn];
int a[maxn],c[maxn];

int main() {
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    for(int i = 1;i <= m;i++) {
        scanf("%d",&c[i]);
    }
    for(int i = 1;i <= n;i++) {
        if(a[i] >= m) {
            G[c[m]].push_back(a[i]);
        }
        else {
            G[c[a[i]]].push_back(a[i]);
        }
    }
    int cnt = 0;
    for(int i = 1;i <= n;i++) {
        for(int j = 0;j < G[i].size();j++) {
            int v = G[i][j];
            if(q.empty()) {
                q.push({1,++cnt});
                ans[cnt].push_back(v);
            } else {
                Node now = q.top();q.pop();
                if(now.num < i) {
                    now.num++;
                    ans[now.pos].push_back(v);
                    q.push(now);
                } else {
                    q.push(now);
                    q.push({1,++cnt});
                    ans[cnt].push_back(v);
                }
            }
        }
    }
    printf("%d\n",q.size());
    for(int i = 1;i <= cnt;i++) {
        printf("%d ",ans[i].size());
        for(int j = 0;j < ans[i].size();j++) {
            printf("%d ",ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;

const int maxn = 2e5 + 7;
int c[maxn],b[maxn];

struct Node {
    vector<int>a;
    int cnt;
    bool operator < (const Node &rhs)const {
        return cnt > rhs.cnt;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    for(int i = 1;i <= n;i++) {
        cin >> b[i];
    }
    for(int i = 1;i <= k;i++) {
        cin >> c[i];
    }
    sort(b + 1,b + 1 + n);
    priority_queue<Node>q;
    Node a;
    a.cnt = 0;a.a.clear();
    q.push(a);
    for(int i = n;i >= 1;i--) {
        Node now = q.top();q.pop();
        if(now.cnt < c[b[i]]) {
            while(now.cnt < c[b[i]]) {
                now.a.push_back(b[i]);
                now.cnt++;
                i--;
            }
            i++;
            q.push(now);
        }
        else {
            Node tmp;
            tmp.cnt = 0;
            while(tmp.cnt < c[b[i]]) {
                tmp.cnt++;
                tmp.a.push_back(b[i]);
                i--;
            }
            i++;
            q.push(tmp);
            q.push(now);
        }
    }
    
    cout << q.size() << endl;
    while(!q.empty()) {
        Node now = q.top();q.pop();
        cout << now.cnt << ' ';
        for(int i = 0;i < now.cnt;i++) {
            cout << now.a[i] << ' ';
        }
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(#,codeforces,#,二叉堆)