Codeforces-706C Hard problem
題目大意:
給你n個字串,讓你把它排序成字典序,但是任何兩個字串之間不能交換,只能對每個字串進行翻轉操作,並且第i個字串翻轉的時候,會消耗c[i]的能量,然後問你,將這些字串排成字典序的時候產生的最小能量。如果不能排成字典序則輸出-1
解題思路:
明顯是個DP,對於每個字串只有兩個選擇,翻轉和不翻轉,搜尋的話是2^100000複雜度明顯搞笑。亦可賽艇
DP做,就是與前面一個字串進行比較
可以設dp[i][j]表示第j個字串為i狀態時所消耗的最小能量。i只為0和1
那麼狀態轉移方程就很明顯了。不知道狀態轉移的可以自己小推一下。
程式碼:
#include <cmath> #include <string> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; //#define DEBUG 1 const int maxn = 100005; const long long INF = 1e16; string tmp, str[2][maxn]; long long c[maxn], dp[2][maxn]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> c[i]; for (int i = 0; i < n; ++i) { cin >> str[0][i]; tmp = str[0][i]; reverse(tmp.begin(), tmp.end()); str[1][i] = tmp; } dp[0][0] = 0; dp[1][0] = c[0]; for (int i = 1; i < n; ++i) { int flag = 0; dp[0][i] = dp[1][i] = INF; if (str[0][i] >= str[0][i - 1] && dp[0][i-1] != -1) { dp[0][i] = dp[0][i - 1]; flag = 1; } if (str[0][i] >= str[1][i - 1] && dp[1][i-1] != -1) { dp[0][i] = min(dp[0][i], dp[1][i-1]); flag = 1; } if (!flag) dp[0][i] = -1; flag = 0; if (str[1][i] >= str[0][i - 1] && dp[0][i - 1] != -1) { dp[1][i] = dp[0][i - 1] + c[i]; flag = 1; } if (str[1][i] >= str[1][i - 1] && dp[1][i - 1] != -1) { dp[1][i] = min(dp[1][i], dp[1][i-1] + c[i]); flag = 1; } if (!flag) dp[1][i] = -1; } if (dp[0][n - 1] == -1 && dp[1][n - 1] == -1) { cout << "-1" << endl; } else if (dp[0][n - 1] == -1) { cout << dp[1][n - 1] << endl; } else if (dp[1][n - 1] == -1) { cout << dp[0][n - 1] << endl; } else { cout << min(dp[0][n - 1], dp[1][n - 1]) << endl; } #ifdef DEBUG system("pause"); #endif // DEBUG return 0; }
相關推薦
CodeForces 706C Hard problem
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given n strings consist
Codeforces-706C Hard problem
題目大意: 給你n個字串,讓你把它排序成字典序,但是任何兩個字串之間不能交換,只能對每個字串進行翻轉操作,並且第i個字串翻轉的時候,會消耗c[i]的能量,然後問你,將這些字串排成字典序的時候產生的最小能量。如果不能排成字典序則輸出-1 解題思路: 明顯是個DP,對於每個字串
【CodeForces - 706C】Hard problem(dp,字典序)
題幹: Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given&nbs
Codeforces Round #360 (Div. 2)C. NP-Hard Problem
並且 pri baidu code int str 兩個 printf 染色 題意:給出一個無向圖,問是否可以是二分圖, 思路:染色就行了,二分圖又稱作二部圖,是圖論中的一種特殊模型。 設G=(V,E)是一個無向圖,如果頂點V可分割為兩個互不相交的子集(A,B),並且圖中的
「專題訓練」Hard problem(Codeforces Round #367 Div. 2 C)
題意與分析 題意:給出\(n\)個字串,可以反轉任意串,反轉每個串都有其對應的花費\(c_i\)。經過操作後是否能滿足字串\(\forall i \in [1,n] \text{且} i \in R_+, str[i]\ge str[i-1]\),若能輸出最小花費,否則輸出-1。 分析:經過各種字串dp血虐
706C】Hard problem(dp,字典序)
題幹: Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is g
CodeForces 706 C. Hard problem(dp)
Description 給出n個字串,翻轉第i個串需要一個代價c[i],問使得這n個字串保持升序所需的最小代價 Input 第一行一整數n表示字串數量,之後n個整數c[i]表示翻轉第i個串所需花費
Codeforces Round #367 (Div. 2) C. Hard problem (dp)
Note In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller. In the third sample
Codeforces Round#413 Problem A - C
false font vector for循環 type als 不存在 pac 鏈接 [寫在前面感(亂)嘆(七)人(八)生(糟)的話] 本想借此機會一口氣玩到藍名,結果,A題寫炸(少判了一種情況),C題寫炸(辜負了我5分鐘狂敲出來的線段樹),結果又掉Rating..
http://codeforces.com/problemset/problem/768/B B. Code For 1
scan 通過 class 題目 num continue lower scn printf 1 #include <bits/stdc++.h> 2 using namespace std; 3 long long bit[70]; 4 void
HDU - 5858 Hard Problem (simpson積分)
asr \n int show turn push first 陰影部分 bubuko 原題鏈接 題意: 給定一個邊長為n的正方形,求陰影部分面積 思路: 現將圖形順時針旋轉 45° 然後建立坐標系,寫出陰影部分方程,用Simpson積分算一下就行
Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)
scanf scan first ces while n-1 class 連續 ble 網上的大多是用樹的直徑做的,但是一些比較巧妙的做法,來自https://www.cnblogs.com/qldabiaoge/p/9315722.html。 首先用set數組維護每一個節
Codeforces contest 1064 problem D Labyrinth —— 帶位置資訊的bfs
You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either i
Codeforces contest 1059 problem E——dfs+倍增+類似樹鏈剖分
You are given a rooted tree on n vertices, its root is the vertex number 1. The i-th vertex contains a number wi. Split it into the minimum possib
Codeforces contest 1059 problem D Nature Reserve —— 圓包含所有點的最小半徑
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (xi,yi). In order to protect them, a
Codeforces contest 1033 problem C Permutation Game——SG
After a long day, Alice and Bob decided to play a little game. The game board consists of n cells in a straight line, numbered from 1 to n, where
I - Xtreme NP-hard Problem?! Gym - 101806X 暴力+思維
題目連結:http://codeforces.com/gym/101806/problem/X 題意: 給你n個點m條邊,要求你求出走k步到點n的最短路。 做法:
ZCMU 1795: wjw的hard problem(dfs+思維)
ZCMU 1795: wjw的hard problem Time Limit: 1 Sec Memory Limit: 128 MB Submit: 69 Solved: 21 [Submit][Status][Web Board] Description ly最
【ZCMU1795】wjw的hard problem(dfs)
題目連結 1795: wjw的hard problem Time Limit: 1 Sec Memory Limit: 128 MB Submit: 70 Solved: 22 [Submit][Status][Web Board] Description
Codeforces 660C Hard Process【二分】經典題!好題!
C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You