Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
CF鏈接??http://codeforces.com/contest/1011/problem/A Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.There are n stages available. The rocket must contain exactly k of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.
For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' — 26 tons.Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.InputThe first line of input contains two integers — n and k (1≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.The second line contains string s, which consists of exactly n lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.OutputPrint a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all. 題面看這里
題目很簡(jiǎn)單,主要是自己WA的有點(diǎn)離譜鴨!
先放上錯(cuò)誤的代碼吧
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int INF = 0x3f3f3f3f; 5 const int maxn = 55; 6 char s[maxn]; 7 int n, m; 8 int main() 9 { 10 cin >> n >> m >> s; 11 sort(s, s + n); 12 int has = 1; 13 int ans = s[0] - 'a' + 1; 14 for (int i = 1; i < n;i++) 15 { 16 if(has==m) break; 17 if(s[i]-s[i-1]>1)//就錯(cuò)在這里!! 18 { 19 ans += s[i] - 'a' + 1; 20 has++; 21 } 22 else 23 { 24 continue; 25 } 26 if(has==m) break; 27 } 28 if(has==m) cout << ans; 29 else cout << -1; 30 return 0; 31 } View Code直接把前后兩個(gè)字符做比較了,改的時(shí)候加了一個(gè)數(shù)來(lái)記錄上一次選擇的那個(gè)字符。
AC代碼
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int INF = 0x3f3f3f3f; 5 const int maxn = 55; 6 char s[maxn]; 7 int n, m; 8 int main() 9 { 10 cin >> n >> m >> s; 11 //cout << s << endl; 12 sort(s, s + n); 13 int has = 1; 14 int ans = s[0] - 'a' + 1; 15 int last = 0; 16 for (int i = 1; i < n;i++) 17 { 18 if(has==m) break; 19 if(s[i]-s[last]>1) 20 { 21 ans += s[i] - 'a' + 1; 22 has++; 23 last = i; 24 } 25 else 26 { 27 continue; 28 } 29 if(has==m) break; 30 } 31 if(has==m) cout << ans; 32 else cout << -1; 33 return 0; 34 } View Code附上自己錯(cuò)的一組后臺(tái)數(shù)據(jù)
以后還是要認(rèn)真點(diǎn)!
50 13 qwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa轉(zhuǎn)載于:https://www.cnblogs.com/chen-tian-yuan/p/10611182.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 栈(stack)和堆(heap)
- 下一篇: 分享一个测试图片的方式