• C. Road Optimization(dp)


    The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet.

    One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i. e. the path from Olymp City to Kstolop).

    The road from Kstolop to Olymp City is ℓℓ kilometers long. Each point of the road has a coordinate xx (0≤x≤ℓ0≤x≤ℓ), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate 00, and Olymp City is located in the point with coordinate ℓℓ.

    There are nn signs along the road, ii-th of which sets a speed limit aiai. This limit means that the next kilometer must be passed in aiai minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i. e. in the point with coordinate 00), which sets the initial speed limit.

    If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example:

    Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is 3⋅5+1⋅8+4⋅3+2⋅6=473⋅5+1⋅8+4⋅3+2⋅6=47 minutes.

    To optimize the road traffic, the Government of Mars decided to remove no more than kk road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible.

    The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above.

    Input

    The first line contains three integers nn, ℓℓ, kk (1≤n≤5001≤n≤500, 1≤ℓ≤1051≤ℓ≤105, 0≤k≤n−10≤k≤n−1), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove.

    The second line contains nn integers didi (d1=0d1=0, di

    The third line contains nn integers aiai (1≤ai≤1041≤ai≤104) — speed limits.

    Output

    Print a single integer — minimal possible time to drive from Kstolop to Olymp City in minutes, if you remove no more than kk road signs.

    题意:有一条长为l的路,有n个路标,每个路标i有位置坐标d[i]和到下一个路标的路程里每公里所需的时间a[i],求最多能移除k个路标的所用的最短时间。(位于坐标为0的路标不能移除)

    判断是不是dp:移走第i个路标的最佳方案是不是在移走第i-1个路标的方案的基础上,如果不是就是dp,是就是贪心

    思路:

    用dp[i][0][j]来表示到第i+1个路标,已经移除了j个路标,而且第i个路标被移除了的最短时间

    用dp[i][1][j]来表示到第i+1个路标,已经移除了j个路标,而且第i个路标没有被移除了的最短时间

    我们要求最短时间必须要找出来最后一个没有被移除的路标,这样才能算出到下一个坐标所花费的时间(没有移除的坐标的a[i]*他们之间的距离)

    dp[i][1][j]的没有被移除的路标就是i,那么他的状态转移方程就是到达第i个路标所需的最短时间(第i-1个路标有没有被移除)再加上到第i+1个路标所需的时间(a[i]*(d[i+1]-d[i]))

    dp[i][1][j]=min(dp[i-1][0][j],dp[i-1][1][j])+a[i]*(d[i+1]-d[i]);

    对dp[i][0][j]来说,i已经移除了,我们没找到最后一个被移除的路标,所以我们要列举i之前的所有坐标找最后一个没有被移除的坐标l,算出花费时间的最小值

    花费的时间是min(dp[ l ][ 1 ][ j - ( i - l )]+a[ l ]*(d[l+1]-d[i+1]));(因为最后一个没有被移除的路标如果是第l个的话,那么从l到i的所有路标都会被移除,那么后面就移除了(i-l)个路标,那么到l这的话是移除了j-(i-l)个路标)

    初始化:因为第0个位置的路标不能删除,所以我们初始化dp[0][1][0]=a[0]*(d[1]-d[0]),剩下的都是正无穷就行了。注意加上最后一个坐标a[n]=l。

    最后求答案的时候,因为是最多移除k个,我们要列举移除0~k个路标的情况取最小值就可以了

    1. /*
    2. .----------------. .----------------. .----------------. .----------------.
    3. | .--------------. || .--------------. || .--------------. || .--------------. |
    4. | | ________ | || | _________ | || | ____ ____ | || | ____ | |
    5. | | |_ ___ `. | || | |_ ___ | | || ||_ \ / _|| || | .' `. | |
    6. | | | | `. \ | || | | |_ \_| | || | | \/ | | || | / .--. \ | |
    7. | | | | | | | || | | _| _ | || | | |\ /| | | || | | | | | | |
    8. | | _| |___.' / | || | _| |___/ | | || | _| |_\/_| |_ | || | \ `--' / | |
    9. | | |________.' | || | |_________| | || ||_____||_____|| || | `.____.' | |
    10. | | | || | | || | | || | | |
    11. | '--------------' || '--------------' || '--------------' || '--------------' |
    12. '----------------' '----------------' '----------------' '----------------'
    13. */
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include
    24. #include
    25. #include
    26. #define int long long
    27. #define lowbit(x) x&(-x)
    28. #define PI 3.1415926535
    29. #define endl "\n"
    30. using namespace std;
    31. typedef long long ll;
    32. typedef pair<int,int> pii;
    33. int gcd(int a,int b){
    34. return b>0 ? gcd(b,a%b):a;
    35. }
    36. /*
    37. int dx[8]={-2,-2,-1,1,2,2,-1,1};
    38. int dy[8]={-1,1,2,2,1,-1,-2,-2};
    39. int dx[4]={0,-1,0,1};
    40. int dy[4]={-1,0,1,0};
    41. int dx[8]={-1,1,0,0,-1,-1,1,1};
    42. int dy[8]={0,0,-1,1,-1,1,-1,1};
    43. */
    44. /*void init(){
    45. int con=0;
    46. for(int i=1;i<=15;i++)con+=i;
    47. }
    48. */
    49. /*
    50. void sjdhfb(){
    51. int djbhff=0;
    52. for(int dd=1;dd<=4;dd++){
    53. djbhff+=dd;
    54. }
    55. }
    56. */
    57. //int e[N],ne[N],h[N],idx,w[N];
    58. /*void add(int a,int b,int c){
    59. e[idx]=b;
    60. w[idx]=c;
    61. ne[idx]=h[a];
    62. h[a]=idx++;
    63. }
    64. */
    65. const int N=500+10;
    66. int n,x,k;
    67. int a[N],d[N];
    68. int dp[N][2][N];
    69. void sove(){
    70. cin>>n>>x>>k;
    71. d[n]=x;//在第n个位置的坐标为x
    72. for(int i=0;i>d[i];
    73. for(int i=0;i>a[i];
    74. memset(dp,0x3f,sizeof dp);
    75. dp[0][1][0]=a[0]*(d[1]-d[0]);
    76. for(int i=1;i
    77. for(int j=0;j<=k;j++){
    78. dp[i][1][j]=min(dp[i-1][0][j],dp[i-1][1][j])+a[i]*(d[i+1]-d[i]);//第i个被拆了的最短时间是前一段的最短时间+后面的时间
    79. for(int l=0;l
    80. if(j>=(i-l))dp[i][0][j]=min(dp[i][0][j],dp[l][1][j-(i-l)]+a[l]*(d[i+1]-d[l+1]));
    81. }
    82. }
    83. }
    84. int ans=0x3f3f3f3f;
    85. for(int i=0;i<=k;i++){
    86. ans=min(ans,min(dp[n-1][0][i],dp[n-1][1][i]));
    87. }
    88. cout<
    89. }
    90. signed main(){
    91. ios::sync_with_stdio(false);
    92. cin.tie() ,cout.tie() ;
    93. int t=1;
    94. // cin>>t;
    95. // init();
    96. // sjdhfb();
    97. // init();
    98. while(t--){
    99. sove();
    100. }
    101. return 0;
    102. }

  • 相关阅读:
    【七夕特别篇】七夕已至,让爱闪耀
    聚观早报 | 达达集团正式回归京东;每日优鲜便利购业务被收购
    wgcna 官网教程II.Consensus analysis of female and male liver expression data
    Spring-IOC-FactoryBean机制(难点且重点)
    iOS 组件化的创建 -创建 cocoapods 库
    Soot之 遍历Control Flow Graph
    人脸检测5种方法
    uniapp微信小程序外壳内联H5实现支付
    深拷贝和浅拷贝
    数字化时代,VR全景如何助力商企抢占市场份额?
  • 原文地址:https://blog.csdn.net/qq_61903556/article/details/126703651