/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 540.0 KiB
#3 Accepted 3ms 332.0 KiB
#4 Accepted 2ms 540.0 KiB
#5 Accepted 2ms 332.0 KiB
#6 Accepted 2ms 332.0 KiB
#7 Accepted 1ms 540.0 KiB
#8 Accepted 1ms 540.0 KiB
#9 Wrong Answer 2ms 540.0 KiB
#10 Wrong Answer 1ms 540.0 KiB

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
/// Welcome to Nasif's Code
#define bug(var) cout << #var << " " << var << endl;
#define all(q) (q).begin(), (q).end()
#define FastRead             \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
const int MOD = (int)998244353;
const int MAX = 1e6;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
bool walk(int limit, int sz, int dif, vector<int> arr)
{
    if (sz < 2)
        return false;

    multiset<int> ms;
    for (auto it : arr)
        ms.insert(it);

    while (limit--)
    {
        int x = *ms.begin();
        int y = x + dif;

        ms.erase(ms.find(x));

        if (ms.find(y) == ms.end())
            return false;

        ms.erase(ms.find(y));

        int req = sz - 2;

        map<int, int> mp;

        for (auto it : ms)
            mp[it]++;

        queue<int> sp, rem;

        for (auto a : ms)
        {
            if (req == 0)
                break;
            if (mp[a] == 0)
                continue;
            if (a > y)
                break;
            mp[a]--;
            int b = a + dif;
            if (mp[b])
            {
                mp[b]--;
                sp.push(a);
                sp.push(b);
            }
            else
                rem.push(a), req--;
        }

        while (!sp.empty() && req)
        {
            int cur = sp.front();
            sp.pop();
            rem.push(cur);
            req--;
        }

        if (req)
            return false;

        while (!rem.empty())
        {
            int cur = rem.front();
            ms.erase(ms.find(cur));
            rem.pop();
        }
    }

    return true;
}

void ignite()
{
    int n, ans = 1;
    set<int> difs;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    sort(all(arr));
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            difs.insert(arr[j] - arr[i]);
    for (int i = 1; i < n; i++)
    {
        if (n % i == 0)
        {
            for (auto dif : difs)
                if (walk(i, n / i, dif, arr))
                {
                    ans = i;
                    break;
                }
        }
    }
    cout << ans << endl;
}

int main()
{
    FastRead
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        int tc = 1;
    cin >> tc;
    while (tc--)
        ignite();

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1162 Roy and Maximum Partition
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-15 17:17:14
Judged At
2025-02-15 17:17:14
Judged By
Score
30
Total Time
3ms
Peak Memory
540.0 KiB