题意:给出n个数,第i个数表示买第i张票的时间,第二行有n-1个数,第i个表示买第i和i+1张票需要的时间,8点开始买票,求要到几点才能买完所有的票
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260
思路:基础dp,状态转移方程dp[i] = min ( dp[i-1] + num[i], dp[i-2] + sum[i] ),i表示买第i张票的最小花费
注意点:无
以下为AC代码:
| Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
| 13674540 | 2015-05-11 22:46:33 | Accepted | 1260 | 0MS | 1592K | 3341 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
//#include <unordered_map>
//#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define RS(s) scanf ( "%s", s )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define PL() printf ( "\n" )
#define PSL(s) printf ( "%s\n", s )
#define rep(i,m,n) for ( int i = m; i < n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i > n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i < n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i > n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
int x, y, cnt;
node(){}
node( int _x, int _y ) : x(_x), y(_y) {}
node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
int dp[2005];
int s[2005];
int d[2005];
int main()
{
int ncase;
RDI ( ncase );
while ( ncase -- ){
clr ( dp, 0x3f3f3f3f );
clr ( s, 0 );
clr ( d, 0 );
int k;
RDI ( k );
REP ( i, 1, k ){
RDI ( s[i] );
}
REP ( i, 2, k ){
RDI ( d[i] );
}
dp[0] = 0;
dp[1] = s[1];
REP ( i, 2, k ){
dp[i] = min ( dp[i-1] + s[i], dp[i-2] + d[i] );
}
int ans = dp[k];
int H = 8;
int M = 0;
int S = 0;
H += ( ans / 3600 );
ans %= 3600;
M += ( ans / 60 );
ans %= 60;
S += ( ans );
int flag = 0;
while ( H > 12 ){
H -= 12;
flag ^= 1;
}
printf ( "%02d:%02d:%02d %s\n", H, M, S, flag?"pm":"am" );
}
return 0;
}

本文探讨了一个利用动态规划解决购票时间最优化的问题。通过实例分析,阐述了如何通过状态转移方程实现最优解计算,并提供了AC代码作为实践指导。文中详细解释了基础动态规划思想及应用,适合对算法优化感兴趣的读者深入学习。

被折叠的 条评论
为什么被折叠?



