欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces 1342 D. Multiple Testcases

程序员文章站 2024-03-17 08:09:52
...

题目入口
题意:给你n个数的数列m和k个数的数列c,要求将m序列分成经量少的组,每组必须满足不超过c[i]个数大于等于i的条件。
Codeforces 1342 D. Multiple Testcases
Codeforces 1342 D. Multiple Testcases

思路

  • 用sum[i]记录大于等于i的个数,那么ans = max(ans,sum[i] / c[i]),其中sum[i] / c[i]上取整。
  • 再将原序列m升序排列,直接将其i % ans 对号入座进入答案组中。

代码实现:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define int long long
#define lowbit(x) (x &(-x))
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double EXP = 1e-8;
const ll   MOD = 1e9 + 7;
const int  N = 2e5 + 5;

int n, k, num;
int m[N], c[N], sum[N];
vector<int> ans[N];

signed main()
{
    IOS;
    cin >> n >> k;
    for(int i = 0; i < n; i ++){
        cin >> m[i];
        sum[m[i]] ++;
    }
    sort(m, m + n);
    for(int i = 1; i <= k; i ++)
        cin >> c[i];

    for(int i = k; i; i --){
        sum[i] += sum[i + 1];
        double tmp = sum[i] * 1.0 / c[i];
        if(tmp > (int)tmp) tmp ++;
        num = max(num, (int)tmp);
    }

    for(int i = 0; i < n; i ++)
        ans[i % num].push_back(m[i]);

    cout << num << endl;
    for(int i = 0; i < num; i ++){
        cout << ans[i].size();
        for(auto it : ans[i]) cout << " " << it;
        cout << endl;
    }

    return 0;
}

优先队列不熟悉,找了好久的规律,嗐~
学长说这个题还可以用背包做,%%%.

相关标签: 比赛&训练