Alibaba - UVa 1632 dp
2014-07-27 12:56
330 查看
Alibaba the famous character of our childhood stories would like to be immortal in order to keep bringing happiness to children. In order to rich this status he needs to prove that he is still able to do some unusual
things. There are n treasures, (n<=10000) each in a different place located along a straight road. Each treasure has a time limit, after that it vanishes. Alibaba must take all the n treasures, and he must do it quickly. So he needs to figure out the order
in which he should take the treasures before their deadlines starting from the most favorable position. Alibaba has the list of places and deadlines of the treasures. A place i is located at distance di from the leftmost end of the road. The time
it takes to take a treasure is instantaneous. Alibaba must find the smallest time by which he can take all the treasures.
in increasing order of the locations. White spaces can occur freely between the numbers in the input. The input data are correct.
this is not possible then the output is "No solution".
题意:你可以在任意点开始,然后单位时间移动一个单位距离,然后问你能否在每个物品消失前拿到他们,注意当你正好在那个时间拿到是不行的。
思路:dp[i][j][0]表示拿i到j的物品最后停在i点的最少时间,dp[i][j][1]表示拿i到j的物品最后停在j点的最少时间。
AC代码如下:
things. There are n treasures, (n<=10000) each in a different place located along a straight road. Each treasure has a time limit, after that it vanishes. Alibaba must take all the n treasures, and he must do it quickly. So he needs to figure out the order
in which he should take the treasures before their deadlines starting from the most favorable position. Alibaba has the list of places and deadlines of the treasures. A place i is located at distance di from the leftmost end of the road. The time
it takes to take a treasure is instantaneous. Alibaba must find the smallest time by which he can take all the treasures.
Input
The program input is from a text file. Each data set in the file stands for a particular set of treasures. For each set of treasures the input contains the number of treasures, and the list of pairs place - deadlinein increasing order of the locations. White spaces can occur freely between the numbers in the input. The input data are correct.
Output
For each set of data the program prints the result to the standard output on a separate line. The solution is represented by the smallest time by which Alibaba can take all the treasures before they vanish. Ifthis is not possible then the output is "No solution".
Sample Input
5 1 3 3 1 5 8 8 19 10 15 5 1 5 2 1 3 4 4 2 5 3
Sample Output
11 No solution
题意:你可以在任意点开始,然后单位时间移动一个单位距离,然后问你能否在每个物品消失前拿到他们,注意当你正好在那个时间拿到是不行的。
思路:dp[i][j][0]表示拿i到j的物品最后停在i点的最少时间,dp[i][j][1]表示拿i到j的物品最后停在j点的最少时间。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[10010][10010][2],num[10010][2]; int main() { int n,i,j,k; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { scanf("%d%d",&num[i][0],&num[i][1]); dp[i][i][0]=0;dp[i][i][1]=0; } for(i=n;i>=1;i--) for(j=i+1;j<=n;j++) { dp[i][j][0]=min(dp[i+1][j][0]+num[i+1][0]-num[i][0],dp[i+1][j][1]+num[j][0]-num[i][0]); if(dp[i][j][0]>=num[i][1]) dp[i][j][0]=1000000000; dp[i][j][1]=min(dp[i][j-1][1]+num[j][0]-num[j-1][0],dp[i][j-1][0]+num[j][0]-num[i][0]); if(dp[i][j][1]>=num[j][1]) dp[i][j][1]=1000000000; } k=min(dp[1] [0],dp[1] [1]); if(k<1000000000) printf("%d\n",k); else printf("No solution\n"); } }
相关文章推荐
- UVa 1632 Alibaba (区间dp)
- [UVA 1632]Alibaba[区间DP]
- UVA - 1632 Alibaba 区间DP
- Alibaba UVA - 1632(区间dp+滚动数组)
- Uva 1632 alibaba 区间dp
- UVA 1632 Alibaba——dp
- UVA 1632-Alibaba(DP)
- UVA - 1632 Alibaba 区间dp
- UVA-1632 Alibaba (区间DP)
- UVA-1632 Alibaba (区间DP+滚动数组)
- UVA 1632 Alibaba(区间dp)
- UVA 1632(p306)----Alibaba
- uva1632Alibaba
- uva 1632——Alibaba
- UVa1632 dp 滚动数组 紫书习题9-8
- UVa 1632 Alibaba
- UVA 1632 区间DP
- UVa #1632 Alibaba (习题9-8)
- 1632 - Alibaba(DP)
- Uva-1632 Alibaba(贪心)