1. 程式人生 > 實用技巧 >Codeforces Round #687 (Div. 2) C

Codeforces Round #687 (Div. 2) C

題意:有一個小球需要從p開始 每次跳k個距離 問條出字串所用的最短時間;

給一個n, p, k 分別是:字串長度 ,小球開始位置, 跳躍長度;

再給出字串,字串中’1‘表示有一個平臺,’0‘表示空地(小球無法從此處跳走)

對字串有兩種操作:

1.將字串中的某個為'0'的點換成'1' 消耗 x秒

2.將字串第一個字元刪掉 消耗y秒(字串的長度 >= p);

思路: 字串每次刪除p的相對位置都會改變,從p點開始到結束所經過的點 都必須是’1’, 這是就需要記錄以p點開始需要變為‘1’ 的點

因為這是個等差數列,不難想到用字尾陣列記錄,接下來就可以計算出結果了。

程式碼:

 1 #include <iostream>
 2
#include <algorithm> 3 #include <queue> 4 #include <cstring> 5 #include <vector> 6 7 using namespace std; 8 typedef long long LL; 9 const int maxn = 2e5 + 7; 10 11 int a[maxn], sum[maxn]; 12 13 vector<int> v; 14 char str[maxn]; 15 int
main (){ 16 int T; 17 cin >> T; 18 while (T -- ){ 19 int n, p, k; 20 cin >> n >> p >> k; 21 scanf ("%s", str + 1); 22 int x, y; 23 cin >> x >> y; 24 //sum[0] = 0; 25 LL ans = 0x7f7f7f7f; 26 for
(int i = 1; i<= n; i ++ ) sum[i] = str[i] == '0'; 27 for (int i = n; i >= 1; i -- ){ 28 // sum[i] = 0; 29 sum[i] += i + k > n ? 0 : sum[i+k]; 30 } 31 for (int i = p; i <= n; i ++ ){ 32 ans = min(ans, 1ll * (i - p) * y + 1ll * sum[i] * x); 33 } 34 cout << ans << endl; 35 36 } 37 return 0; 38 }