zoukankan      html  css  js  c++  java
  • Codeforces Round #598 (Div. 3) E

     

    There are nn students at your university. The programming skill of the ii-th student is aiai. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 21052⋅105 students ready for the finals!

    Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of kk students with programming skills a[i1],a[i2],,a[ik]a[i1],a[i2],…,a[ik], then the diversity of this team is maxj=1ka[ij]minj=1ka[ij]maxj=1ka[ij]−minj=1ka[ij]).

    The total diversity is the sum of diversities of all teams formed.

    Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.

    Input

    The first line of the input contains one integer nn (3n21053≤n≤2⋅105) — the number of students.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109), where aiai is the programming skill of the ii-th student.

    Output

    In the first line print two integers resres and kk — the minimum total diversity of the division of students and the number of teams in your division, correspondingly.

    In the second line print nn integers t1,t2,,tnt1,t2,…,tn (1tik1≤ti≤k), where titi is the number of team to which the ii-th student belong.

    If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.

    Examples
    input
    Copy
    5
    1 1 3 4 2
    
    output
    Copy
    3 1
    1 1 1 1 1 
    
    input
    Copy
    6
    1 5 12 13 2 15
    
    output
    Copy
    7 2
    2 2 1 1 2 1 
    
    input
    Copy
    10
    1 2 5 129 185 581 1041 1909 1580 8150
    
    output
    Copy
    7486 3
    3 3 3 2 2 2 2 1 1 1 
    
    Note

    In the first example, there is only one team with skills [1,1,2,3,4][1,1,2,3,4] so the answer is 33. It can be shown that you cannot achieve a better answer.

    In the second example, there are two teams with skills [1,2,5][1,2,5] and [12,13,15][12,13,15] so the answer is 4+3=74+3=7.

    In the third example, there are three teams with skills [1,2,5][1,2,5], [129,185,581,1041][129,185,581,1041] and [1580,1909,8150][1580,1909,8150] so the answer is 4+912+6570=74864+912+6570=7486.

     

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <cmath>
    using namespace std;
    #define mem(s,t) memset(s,t,sizeof(s))
    #define pq priority_queue
    #define pb push_back
    #define fi first
    #define se second
    #define ac return 0;
    #define ll long long
    #define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
    #define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    #define rep(n) for(int i=1;i<=n;i++)
    #define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
    #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    #define lc now<<1
    #define rc now<<1|1
    #define ls now<<1,l,mid
    #define rs now<<1|1,mid+1,r
    #define half no[now].l+((no[now].r-no[now].l)>>1)
    #define ll long long
    #define inf 0x3f3f3f3f
    const int mxn = 2e5+10;
    ll n,m,k,ans,cnt,col;
    int dp[mxn],pro[mxn],flag[mxn];//dp[] 最小化  pro[] 下标 flag 分组记录
    string str,ch ;
    pair <int,int> mp[mxn];
    bool cmp(pair<int,int>x,pair<int,int>y) {return x.first>y.first;}
    int main()
    {
        while(cin>>n)
        {
            for(int i=1;i<=n;i++)
            {
                cin>>mp[i].first , mp[i].second=i;
                dp[i] = inf;
            }
            sort(mp+1,mp+1+n,cmp);
            dp[0] = 0;
            for(int i=3;i<=n;i++)
            {
                if(i-3>=0 && dp[i]>dp[i-3]+mp[i-2].first-mp[i].first)
                {
                    dp[i] = dp[i-3]+mp[i-2].first-mp[i].first;
                    pro[i] = i-3;
                }
                if(i-4>=0 && dp[i]>dp[i-4]+mp[i-3].first-mp[i].first)
                {
                    dp[i] = dp[i-4]+mp[i-3].first-mp[i].first;
                    pro[i] = i-4;
                }
                if(i-5>=0 && dp[i]>dp[i-5]+mp[i-4].first-mp[i].first)
                {
                    dp[i] = dp[i-5]+mp[i-4].first-mp[i].first;
                    pro[i] = i-5;
                }
            }
            ans = pro[n] , col = n,cnt = 1;
            while(col)
            {
                while(col>ans)
                {
                    flag[mp[col].second] = cnt;
                    col--;
                }
                col = ans ;
                ans = pro[col];
                cnt++;
            }
            cout<<dp[n]<<" "<<--cnt<<endl;
            for(int i=1;i<=n;i++)
                cout<<flag[i]<<" ";
            cout<<endl;
        }
        return 0;
    }
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <cmath>
    using namespace std;
    #define mem(s,t) memset(s,t,sizeof(s))
    #define pq priority_queue
    #define pb push_back
    #define fi first
    #define se second
    #define ac return 0;
    #define ll long long
    #define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
    #define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    #define rep(n) for(int i=1;i<=n;i++)
    #define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
    #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    #define lc now<<1
    #define rc now<<1|1
    #define ls now<<1,l,mid
    #define rs now<<1|1,mid+1,r
    #define half no[now].l+((no[now].r-no[now].l)>>1)
    #define ll long long
    #define inf 0x3f3f3f3f
    const int mxn = 2e5+10;
    ll n,m,k,ans,cnt,col;
    int dp[mxn],pro[mxn],flag[mxn];
    string str,ch ;
    pair <int,int> mp[mxn];
    int main()
    {
        while(cin>>n)
        {
            memset(dp,inf,sizeof(dp));
            for(int i=1;i<=n;i++)
                cin>>mp[i].first , mp[i].second=i;
            sort(mp+1,mp+1+n); dp[1] = 0;
            for(int i=1;i<=n;i++)
            {
                for(int k=2;k<=4 && i+k<=n;k++)
                {
                    int ans = mp[i+k].first-mp[i].first;  //计算区间差值
                    if(dp[i+k+1]>dp[i]+ans)
                    {
                        dp[i+k+1] = dp[i]+ans;
                        pro[i+k+1] = i ; //存储方案 i ~ i+k 为一组
                    }
                }
            }
            cnt = 1 , col = n+1;
            while(col!=1)
            {
                for(int i=col-1;i>=pro[col];i--)
                {
                    flag[mp[i].second] = cnt;
                }
                cnt++;
                col=pro[col];
            }
            cout<<dp[n+1]<<" "<<--cnt<<endl;
            for(int i=1;i<=n;i++)
                cout<<flag[i]<<" ";
            cout<<endl;
        }
        return 0;
    }

     

    所遇皆星河
  • 相关阅读:
    【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
    【2018.04.27 C与C++基础】关于switch-case及if-else的效率问题
    【2018.04.19 ROS机器人操作系统】机器人控制:运动规划、路径规划及轨迹规划简介之一
    March 11th, 2018 Week 11th Sunday
    March 10th, 2018 Week 10th Saturday
    March 09th, 2018 Week 10th Friday
    March 08th, 2018 Week 10th Thursday
    March 07th, 2018 Week 10th Wednesday
    ubantu之Git使用
    AMS分析 -- 启动过程
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11830604.html
Copyright © 2011-2022 走看看