zoukankan      html  css  js  c++  java
  • POJ2355 Railway tickets DP优化

      题目链接:http://poj.org/problem?id=2355

      简单的DP,转移方程:f[i][j]=Min { f[i][k] + C(n) },注意到f[i][j]是单调递增的,用二分搜索优化即可。注意到此题有个trick,那就是s>e!!!

     1 //STATUS:C++_AC_16MS_252KB
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<iostream>
     7 #include<string>
     8 #include<algorithm>
     9 #include<vector>
    10 #include<queue>
    11 #include<stack>
    12 #include<map>
    13 using namespace std;
    14 #define LL __int64
    15 #define pii pair<int,int>
    16 #define Max(a,b) ((a)>(b)?(a):(b))
    17 #define Min(a,b) ((a)<(b)?(a):(b))
    18 #define mem(a,b) memset(a,b,sizeof(a))
    19 #define lson l,mid,rt<<1
    20 #define rson mid+1,r,rt<<1|1
    21 const int N=10010,INF=0x3f3f3f3f,MOD=1000000007;
    22 const double DNF=100000000000;
    23 
    24 int d[N];
    25 LL f[N];
    26 int l1,l2,l3,c1,c2,c3,n,s,e;
    27 
    28 LL binary(int low,int i,int cost)
    29 {
    30     int l=s,r=i,mid;
    31     while(l<r)
    32     {
    33         mid=(l+r)>>1;
    34         if(d[mid]>=low)r=mid;
    35         else l=mid+1;
    36     }
    37     return f[l]+cost;
    38 }
    39 
    40 int main()
    41 {
    42  //   freopen("in.txt","r",stdin);
    43     int i,j,t;
    44     while(~scanf("%d%d%d%d%d%d",&l1,&l2,&l3,&c1,&c2,&c3))
    45     {
    46         mem(f,INF);
    47         scanf("%d%d%d",&n,&s,&e);
    48         for(i=2;i<=n;i++)
    49             scanf("%d",&d[i]);
    50         if(s>e)t=s,s=e,e=t;
    51 
    52         f[s]=0;
    53         for(i=s+1;i<=e;i++){
    54             t=d[i]-l1;
    55             f[i]=Min(f[i],binary(t,i,c1));
    56             t=d[i]-l2;
    57             f[i]=Min(f[i],binary(t,i,c2));
    58             t=d[i]-l3;
    59             f[i]=Min(f[i],binary(t,i,c3));
    60         }
    61 
    62         printf("%I64d\n",f[e]);
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    iOS MJRefresh的使用 (列表上拉加载更多)
    iOS 后台任务
    ios Alamofire网络插件的使用
    springboot shiro ehcache redis 简单使用
    android BottomNavigationView 简单使用
    iOS ksyhttpcache音视频缓存
    springboot 简单使用shiro登录
    springboot 使用 mybatis + mapper
    [LeetCode] Permutations
    【经典算法】回溯算法
  • 原文地址:https://www.cnblogs.com/zhsl/p/2982318.html
Copyright © 2011-2022 走看看