zoukankan      html  css  js  c++  java
  • Codeforces 1256E Yet Another Division Into Teams

    思路:

    这题参考的自带题解的思路:

    Let’s sort all students by their programming skills but save the initial indices to restore the answer.
    Now we can understand that we don’t need to compose the team of size greater than 5 because in this case we can split it into more teams with fewer participants and obtain the same or even less answer.
    Now we can do the standard dynamic programming dpi— the minimum total diversity of the division if we divided the first i students (in sorted order). Initially, dp0=0, all other values of dp are +∞. Because of the fact above, we can do only three transitions (0-indexed):
    dpi+3=min(dpi+3,dpi+ai+2−ai);
    dpi+4=min(dpi+4,dpi+ai+3−ai);
    dpi+5=min(dpi+5,dpi+ai+4−ai).
    The answer is dpn and we can restore it by standard carrying parent values (as a parent of the state we can use, for example, the number of participants in the team).

    用这个思路去写,有一点需要注意,对dp[]进行初始化时,设定的INF应该满足1e9 < INF < INT_MAX-1e9,否则在进行计算时,会造成溢出现象;(笔者之前使用的memset(dp,127,sizeof(dp)),就出现了test10错误的现象)

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> p;
    #define fi first
    #define sc second
    #define mem(a,x) memset(a,x,sizeof(a))
    #define rp(i,n) for(int i=0;i<n;i++)
    #define rpn(i,n) for(int i=1;i<=n;i++)
    const int MAX_N=200005;
    const int INF=int(1e9) +10;
    p a[MAX_N];
    int dp[MAX_N];//dp[i]前i个人最小的total diversity
    int t[MAX_N];
    int out[MAX_N];//用于输出 
    int main(){
    	int n;
    	scanf("%d",&n);
    	rpn(i,n){
    		scanf("%d",&a[i].fi);
    		a[i].sc=i;
    	}
    	sort(a+1,a+n+1);
    	fill(dp+1,dp+MAX_N,INF);
    	for(int i=0;i<n;i++){
    		for(int j=3;j<=5&&i+j<=n;j++){
    			int diff=a[i+j].fi-a[i+1].fi;
    			if(dp[i]+diff<dp[i+j]){
    				dp[i+j]=dp[i]+diff;
    				t[i+j]=j;//以第i+j人为结尾的队有t[i+j]人 
    			}
    		}
    	}
    	int ans=0;
    	for(int i=n;i>=3;){
    		ans++;//第ans队,有t[i]人 
    		int num=t[i];
    		while(num--) out[a[i--].sc]=ans;
    	}
    	printf("%d %d
    %d",dp[n],ans,out[1]);
    	for(int i=2;i<=n;i++) printf(" %d",out[i]);
    	return 0;
    }
    
  • 相关阅读:
    table 如何不越过父级div
    sqlite3_column_type 与 SQLITE_NULL的区别
    lua 协程的理解
    linux 信号
    linux 查看文件夹大小
    linux 僵屍进程
    软件架构的理解
    jquery正则表达式
    linux C遍历目录下文件
    linux 进程间同步互斥
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308885.html
Copyright © 2011-2022 走看看