HDU 3480 Division
2018-06-17 21:13:21来源:未知 阅读 ()
Division
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5344 Accepted Submission(s):
2115
Let T be a set of integers. Let the MIN be the minimum integer in T and MAX be the maximum, then the cost of set T if defined as (MAX – MIN)^2. Now given an integer set S, we want to find out M subsets S1, S2, …, SM of S, such that
and the total cost of each subset is minimal.
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000). N is the number of elements in S (may be duplicated). M is the number of subsets that we want to get. In the next line, there will be N integers giving set S.
#include<cstdio> #include<cstring> #include<algorithm> const int MAXN=10001,INF=1e9+10; using namespace std; inline int read() { char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int dp[MAXN][MAXN],s[MAXN][MAXN],a[MAXN]; int mul(int x){return x*x;} int main() { int Test=read(),cnt=0; while(Test--) { int N=read(),M=read(); for(int i=1;i<=N;i++) a[i]=read();sort(a+1,a+N+1); for(int i=1;i<=N;i++) dp[1][i]=mul(a[i]-a[1]),s[1][i]=1; for(int i=2;i<=M;i++) { s[i][N+1]=N-1;//边界 for(int j=N;j>=i;j--) { int mn=INF,mnpos=-1; for(int k=s[i-1][j];k<=s[i][j+1];k++) { if(dp[i-1][k]+mul(a[j]-a[k+1])<mn) { mn=dp[i-1][k]+mul(a[j]-a[k+1]); mnpos=k; } } dp[i][j]=mn; s[i][j]=mnpos; } } printf("Case %d: %d\n",++cnt,dp[M][N]); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:CSP201612-1:中间数
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash