dp greedy sortings *2000

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 1e12

const int N = 2e5 + 7;
int n, idx[N];
pair<int, int> a[N];
ll dp[N];

ll rec(int i)
{
    if (i == n)
        return 0;
    if(n - i < 3)
        return INF;
    ll &ret = dp[i];
    if (~ret)
        return ret;
    ret = a[n - 1].first - a[i].first;
    int low = i + 3, high = i + 5;
    for(low; low <= high; ++low)
    {
        ll ans = rec(low), cur = (a[low - 1].first - a[i].first);
        ret = min(ret, ans + cur);
    }
    return ret;
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i].first, a[i].second = i;
    sort(a, a + n);
    memset(dp, -1, sizeof(dp));
    ll ans = rec(0);
    cout << ans << ' ';
    int cnt = 1, lst = 0;
    idx[a[0].second] = cnt;
    // build
    int number = 1;
    for (int i = 1; i < n; ++i)
    {
        if (number > 2 and ans - (a[i - 1].first - a[lst].first) == dp[i])
        {
            cnt++;
            ans -= (a[i - 1].first - a[lst].first);
            lst = i;
            number = 1;
        }
        else
            number++;
        idx[a[i].second] = cnt;
    }
    cout << cnt << '\n';
    for (int i = 0; i < n; ++i)
        cout << idx[i] << ' ';
    cout << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    for (int i = 1; i <= tc; ++i)
        solve();
}
				 			     			     	  		 	  	


Comments

Submit
0 Comments
More Questions

1088B - Ehab and subtraction
1270B - Interesting Subarray
478C - Table Decorations
1304C - Air Conditioner
1311C - Perform the Combo
1519C - Berland Regional
361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation