QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1092#693478#7628. Keyi LIkes ReadingDukerkiDukerkiFailed.2024-10-31 16:16:142024-10-31 16:24:04

详细

Extra Test:

Invalid Input

input:

9 19
12 9 9 8 6 5 1 4 3

output:


result:

FAIL Integer parameter [name=m] equals to 19, violates the range [1, 9] (stdin, line 1)

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#693478#7628. Keyi LIkes ReadingDukerkiAC ✓8ms3708kbC++202.0kb2024-10-31 16:14:412024-10-31 16:38:59

answer

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
int arr[14];
int ans = 0;
void check(int w)
{
    int maxnum = 0;
    int maxchoice = 0;
    int maxcnt = 0;
    int maxn = 0;
    int maxindex = 0;
    for (int i = 0; i < 13; i++)
    {
        if (maxn < arr[i]) {
            maxn = arr[i];
            maxindex = i;
        }
    }
    for (int i = 1; i < (1 << 14) - 1; i++)
    {
        int temp = (i | (1 << maxindex));
        int sum = 0;
        int cnt = 0;
        for (int j = 0; j < 13 && temp != 0; j++)
        {
            if (temp & 1)
            {
                sum += arr[j];
                cnt++;
            }
            temp >>= 1;
        }
        if (sum >= maxnum && sum <= w)
        {
            if (sum > maxnum) {
                maxchoice = (i | (1 << maxindex));
                maxcnt = cnt;
            }
            maxnum = sum;
            if (cnt > maxcnt) {
                maxcnt = cnt;
                maxchoice = (i | (1 << maxindex));
            }
        }
    }
    int sum = 0;
    for (int j = 0; j < 13 && maxchoice != 0; j++)
    {
        if (maxchoice & 1)
        {
            //if (arr[j] != 0) {
            //    cout << arr[j] << " ";
            //    sum += arr[j];
            //}
            arr[j] = 0;
        }
        maxchoice >>= 1;
    }
    //cout << "sum = "<<sum << endl;
    if (maxnum != 0)
    {
        ans++;
        check(w);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n, m, temp;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        arr[temp - 1]++;
        //cin >> arr[i];
    }
    sort(arr, arr + 13);
    //if (n == 6542) {
    //    for (int i = 0; i < 13; i++)
    //    {
    //        cout << arr[i] << " ";
    //    }
    //}
    check(m);
    cout << ans << endl;
    return 0;
}