hdu4286
2016-05-07 11:39
211 查看
C - You Are the One
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there
are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D,
because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first
get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?
Input
The first line contains a single integer T, the number of test cases. For each case, the first line is n (0 < n <= 100)
The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
Output
For each test case, output the least summary of unhappiness .
Sample Input
2
5
1
2
3
4
5
5
5
4
3
2
2
Sample Output
Case #1: 20
Case #2: 24
/**
题目大意:
有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],
如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],
因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,
可以通过小黑屋来改变男屌的出场顺序
注意 :这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男
屌的出场顺序
解题思路 :
dp[i][j]表示区间[i,j]的最小总不开心值
把区间[i,j]单独来看,则第i个人可以是第一个出场,也可以是最后一个出场(j-i+1)
,也可以是在中间出场(1 ~ j-i+1)
不妨设他是第k个出场的(1<=k<=j-i+1),那么根据栈后进先出的特点,以及题目要求
原先男的是排好序的,那么::
第 i+1 到 i+k-1 总共有k-1个人要比i先出栈,
第 i+k 到j 总共j-i-k+1个人在i后面出栈
举个例子吧:
有5个人事先排好顺序 1,2,3,4,5
入栈的时候,1入完2入,2入完3入,如果我要第1个人第3个出场,那么入栈出栈顺序是这样的:
1入,2入,3入,3出,2出,1出(到此第一个人就是第3个出场啦,很明显第2,3号人要在1先出
,而4,5要在1后出)
这样子, 动态转移方程 就出来啦,根据第i个人是第k个出场的,将区间[i,j]分成3个部分
dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
(sum[j]-sum[i+k-1])*k 表示 后面的 j-i-k+1个人是在i后面才出场的,那么每个人的不开心值
都会加个 unhappy,sum[i]用来记录前面i个人的总不开心值,根据题目,每个人的unhappy是个
累加的过程 ,多等一个人,就多累加一次
*/
/**
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=105,inf=0x3f3f3f3f;
int dp
,sum
,d
;
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int n;
int t;
scanf("%d\n",&t);
for(int cas=1;cas<=t;cas++)
{
scanf("%d",&n);
sum[0]=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&d[i]);
sum[i]=sum[i-1]+d[i];
}
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
dp[i][j]=inf;
for(int p=1; p<=n; p++)
{
for(int i=1; i<=n-p+1; i++)
{
int j=i+p-1;
for(int k=1; k<=p; k++)/// i到j的长度为k,j-i+1=p,所以k从1到p
dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*d[i]+(sum[j]-sum[i+k-1])*k);
///dp[i][j]表示区间i到j最小总不开心值
}
}
printf("Case #%d: %d\n",cas,dp[1]
);
}
return 0;
}
*/
#include<cstring>
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
int dp[110][110];
int a[110],sum[110];
int n;
int solve(int i,int j)
{
int &ans=dp[i][j];///引用,ans值变,dp值也变
if(ans!=-1) return ans;
if(i>=j) return 0;/// dp[i][j]表示区间i-j最小愤怒值总和,i==j时,i就是这个区间第一个上台,前面没有人,愤怒值为0;
ans=1<<30;
for(int k=1;k<=j-i+1;k++){
ans=min(ans,solve(i+1,i+k-1)+solve(i+k,j)+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
}
return ans;
}
int main()
{
int t,iCase=1;
cin>>t;
while(t--){
cin>>n;
sum[0]=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
memset(dp,-1,sizeof dp);
printf("Case #%d: %d\n",iCase++,solve(1,n));
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there
are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D,
because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first
get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?
Input
The first line contains a single integer T, the number of test cases. For each case, the first line is n (0 < n <= 100)
The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
Output
For each test case, output the least summary of unhappiness .
Sample Input
2
5
1
2
3
4
5
5
5
4
3
2
2
Sample Output
Case #1: 20
Case #2: 24
/**
题目大意:
有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],
如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],
因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,
可以通过小黑屋来改变男屌的出场顺序
注意 :这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男
屌的出场顺序
解题思路 :
dp[i][j]表示区间[i,j]的最小总不开心值
把区间[i,j]单独来看,则第i个人可以是第一个出场,也可以是最后一个出场(j-i+1)
,也可以是在中间出场(1 ~ j-i+1)
不妨设他是第k个出场的(1<=k<=j-i+1),那么根据栈后进先出的特点,以及题目要求
原先男的是排好序的,那么::
第 i+1 到 i+k-1 总共有k-1个人要比i先出栈,
第 i+k 到j 总共j-i-k+1个人在i后面出栈
举个例子吧:
有5个人事先排好顺序 1,2,3,4,5
入栈的时候,1入完2入,2入完3入,如果我要第1个人第3个出场,那么入栈出栈顺序是这样的:
1入,2入,3入,3出,2出,1出(到此第一个人就是第3个出场啦,很明显第2,3号人要在1先出
,而4,5要在1后出)
这样子, 动态转移方程 就出来啦,根据第i个人是第k个出场的,将区间[i,j]分成3个部分
dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
(sum[j]-sum[i+k-1])*k 表示 后面的 j-i-k+1个人是在i后面才出场的,那么每个人的不开心值
都会加个 unhappy,sum[i]用来记录前面i个人的总不开心值,根据题目,每个人的unhappy是个
累加的过程 ,多等一个人,就多累加一次
*/
/**
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=105,inf=0x3f3f3f3f;
int dp
,sum
,d
;
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int n;
int t;
scanf("%d\n",&t);
for(int cas=1;cas<=t;cas++)
{
scanf("%d",&n);
sum[0]=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&d[i]);
sum[i]=sum[i-1]+d[i];
}
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
dp[i][j]=inf;
for(int p=1; p<=n; p++)
{
for(int i=1; i<=n-p+1; i++)
{
int j=i+p-1;
for(int k=1; k<=p; k++)/// i到j的长度为k,j-i+1=p,所以k从1到p
dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*d[i]+(sum[j]-sum[i+k-1])*k);
///dp[i][j]表示区间i到j最小总不开心值
}
}
printf("Case #%d: %d\n",cas,dp[1]
);
}
return 0;
}
*/
#include<cstring>
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
int dp[110][110];
int a[110],sum[110];
int n;
int solve(int i,int j)
{
int &ans=dp[i][j];///引用,ans值变,dp值也变
if(ans!=-1) return ans;
if(i>=j) return 0;/// dp[i][j]表示区间i-j最小愤怒值总和,i==j时,i就是这个区间第一个上台,前面没有人,愤怒值为0;
ans=1<<30;
for(int k=1;k<=j-i+1;k++){
ans=min(ans,solve(i+1,i+k-1)+solve(i+k,j)+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
}
return ans;
}
int main()
{
int t,iCase=1;
cin>>t;
while(t--){
cin>>n;
sum[0]=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
memset(dp,-1,sizeof dp);
printf("Case #%d: %d\n",iCase++,solve(1,n));
}
return 0;
}
相关文章推荐
- 第一回使用Android Studio时你应该知道的一切配置
- 毕设笔记--CSS样式修改
- CodeForces 670D2 Magic Powder - 2
- 动态获得类的属性来构建模型
- 3.26日第六次作业,第10章质量,11章人力
- 未将对象引用设置到对象的实例
- Python数据分析示例(2)Day3
- 回调函数之类相关
- 【Android】通用系列 —— 用简单通用的方式操作ListView
- Xamarin开发Android---提示、跳转、传递数值
- SpringMvc学习与使用
- centos7开放端口
- Apache与Tomcat的关系和区别
- RPC, REST 与 SOCKET
- 八大排序算法 之 快速排序(填坑法)
- TortoiseGit 使用教程
- 关于Java中Scanner对象的hasNext()方法对实现Readable接口的对象中的read()方法调用的探讨
- HDU 5159 Card (概率DP)
- STM32位带操作----待更
- 【Ubuntu】搭建LAMP服务器Apache+MySQL+PHP