/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 5ms 532.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 3ms 532.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Wrong Answer 6ms 532.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;
    vector<int> t;
    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;

        queue<int> q, rem;

        for (auto a : ms)
        {
            if (a > y)
                break;
            int b = a + dif;
            int c = a - dif;
            if (ms.find(b) == ms.end() && ms.find(c) == ms.end())
                req--, rem.push(a);
            else
                q.push(a);
            if (req == 0)
                break;
        }

        while (req && !q.empty())
        {
            rem.push(q.front());
            q.pop();
            req--;
        }

        if (req)
            return false;

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

    return true;
}

set<int> getDifs(vector<int> arr)
{
    int n = arr.size();
    set<int> s;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            s.insert(arr[j] - arr[i]);
    return s;
}

void ignite()
{
    int n;
    cin >> n;
    vector<int> arr;
    for (int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        arr.push_back(a);
    }
    sort(all(arr));
    set<int> difs = getDifs(arr);
    int ans = 1;
    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 16:55:48
Judged At
2025-02-15 16:55:48
Judged By
Score
12
Total Time
6ms
Peak Memory
532.0 KiB