tju 4070 简单dp
来源:互联网 发布:nginx upload module 编辑:程序博客网 时间:2024/04/30 04:26
Time Limit: 1.0 Seconds Memory Limit:65536K
Total Runs: 84 Accepted Runs:76
There is a one-dimensional road. The road is separated into N consecutive parts. The parts are numbered 0 through N-1, in order. MJ is going to walk from part 0 to part N-1.
MJ also noticed that each part of the road has a color: either red, green, or blue. Part 0 is red.
MJ is going to perform a sequence of steps. Each step must lead in the positive direction. That is, if his current part is i, the next step will take him to one of the parts i+1 through N-1, inclusive. His steps can be arbitrarily long. However, longer steps are harder: a step of length j costs j*j energy.
Additionally, MJ wants to step on colors in a specific order: red, green, blue, red, green, blue, ... That is, he starts on the red part 0, makes a step to a green part, from there to a blue part, and so on, always repeating red, green, and blue in a cycle. Note that the final part N-1 also has some color and thus MJ must reach it in a corresponding step.
You are given a String road containing N elements. For each i, element i of road is the color of part i: 'R' represents red, 'G' green, and 'B' blue. If MJ can reach part N-1 in the way described above, return the smallest possible total cost of doing so. Otherwise, return -1.
Input
First line is a integer T, the number of test cases.
The next T lines, each line is a string S represent the road.The length of S is no longer than 20.
Output
For each test case, output the smallest possible total cost or -1 if it's impossible.
Sample Input
4RGGGBRGBRGBRGBRBBGGGRRRBRRBGGGBBBBR
Sample Output
88-150
Source: TJU Team Selection 2014 Round1
题意:每次走步的花费为长度的平方,必须按照红绿蓝的顺序走,问走到路的尽头的最小花费
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX 27using namespace std;int n;char road[MAX];int dp[MAX][MAX];int get ( char ch ){ if ( ch == 'R' ) return 0; if ( ch == 'G' ) return 1; else return 2;}int main ( ){ scanf ( "%d" , &n ); while ( n -- ) { scanf ( "%s" , road ); memset ( dp , 0x3f , sizeof ( dp ) ); int INF = dp[0][0]; int ans = INF; dp[0][0] = 0; int len = strlen ( road ); for ( int i = 1 ; i <= len ; i++ ) for ( int j = i ; j < len ; j++ ) { if ( get ( road[j] ) == i%3 ) for ( int k = 0 ; k < j ; k++ ) if ( get ( road[k] ) == (i-1)%3 ) dp[i][j] = min ( dp[i][j] , dp[i-1][k] + (j-k)*(j-k) ); if ( j == len-1 ) ans = min ( ans , dp[i][j] ); } if ( ans >= INF ) puts ( "-1" ); else printf ( "%d\n" , ans ); }}
- tju 4070 简单dp
- 【DP】 TJU 4087
- 【TJU】4087 box DP
- 【tju】【dp】调整队形
- 【DP】 TJU 4074 && CF 319C
- TJU-3977-Probability I(概率DP)
- TJU 3474
- TJU Sequence
- TJU Vacation
- TJU Change
- TJU Antimonotonicity
- TJU Price
- TJU 4087
- 简单DP
- 简单dp
- 简单dp
- 简单DP
- 简单dp
- MAC 下搭建Python分析环境 (安装 Enthought Canopy Express)
- LeetCode --- 70. Climbing Stairs
- Android自定义控件实战—滚动选择器PickerView(滚动变化字体)
- 2014134027谢天
- sqlite3 带占位符插入数据
- tju 4070 简单dp
- tcp/ip状态图
- 图解classloader加载class的流程及自定义ClassLoader
- Java Iterable、Iterator、iterator区别
- c++ map unordered_map
- WPF-MVVM模式学习笔记4——Lambda表达式学习
- [BZOJ 1856][SCOI 2010]字符串(组合数学)
- 微信支付案例 JSAPI支付案例 支付案例源码
- Leetcode: Remove Duplicates from Sorted Array