Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define inf 2000000000
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define Mini(x) *min_element(all(x))
- #define Maxi(x) *max_element(all(x))
- #define fixed(n) fixed << setprecision(n)
- #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
- #define cin(v) for (auto&i:v) cin >> i;
- #define cout(v) for (auto&i:v) cout << i << " ";
- #define clr(memo, x) memset(memo, x, sizeof memo)
- #define updmin(a, b) a = min(a, b)
- #define updmax(a, b) a = max(a, b)
- #define vi vector < int >
- #define vl vector < ll >
- #define vc vector < char >
- #define vs vector < string >
- #define v2i vector < vector < int > >
- #define v2l vector < vector < int > >
- #define seti set < int >
- #define setl set < ll >
- #define mapii map < int , int >
- #define mapll map < ll , ll >
- #define mapli map < ll , int >
- #define mapci map < char , int >
- #define mapsi map < string , int >
- #define pll pair < ll , ll >
- #define pii pair < int , int >
- #define range(l,r,x) for(int i=l ; i < r ; i+=x)
- #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- vector < string > ternary= {"NO\n" , "YES\n"};
- void Zainab(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- /*================================ solution ================================ */
- vector < pair < ll , ll > > vec;
- vector < vector < ll > > dp ;
- int n ;
- const ll invalid=2e18 ;
- // 0 -> close
- // 1 -> open
- ll rec(int idx , int open ){
- if(idx >= n-1 ) {
- if(idx == n-1){
- if(open)
- return vec[idx].fi;
- return invalid;
- }
- return invalid;
- }
- ll &ret = dp[idx][open];
- if(~ret)
- return ret ;
- ret = invalid;
- if(open){
- // close ?
- ret = min(ret , rec(idx+1 , 0 ) + vec[idx].fi );;
- // remain open
- ret = min(ret , rec(idx+1 , 1));
- }
- else {
- ret = min(ret , rec(idx+2 , 1) -vec[idx].fi);
- }
- return ret ;
- }
- vector < int > ans ;
- int teams = 1 ;
- void build (int idx , int open){
- if(idx >= n ){
- return ;
- }
- ll ret = invalid ;
- // end of interval
- ll mn1 = rec(idx+1 , 0 ) + vec[idx].fi ;
- ll mn2 = rec(idx+1 , 1 );
- // start of interval
- ll mn3 = rec(idx+2 , 1 ) - vec[idx].fi;
- ret = min({mn1 , mn2 , mn3});
- if(ret == mn1){
- ans[vec[idx].se] = teams;
- teams++;
- build(idx+1 , 0);
- }
- else if(ret == mn2){
- build(idx+1 , 1);
- }
- else {
- // ans[vec[idx].se ] = -teams ;
- build(idx+2 , 1);
- }
- }
- void myCode(){
- cin >> n ;
- vec.assign(n,{0,0});
- for(int i =0 , x ; i < n ; i++){
- cin >> x;
- vec[i] = {x , i};
- }
- ans.assign(n,0);
- dp.assign(n+1 , vector < ll > ( 2 , -1));
- sort(all(vec));
- cout << rec( 0 , 0 ) << " ";
- build(0,0);
- cout << teams << nl;
- cout(ans);
- }
- int main(){
- FastCode ;
- Zainab() ;
- int testCase=1;
- // cin >> testCase ;
- for(int i=1 ; i<= testCase ; i++){
- myCode();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement