zoukankan      html  css  js  c++  java
  • Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)

      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.
    
    Input
      The 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.
    
    Output
      Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.
    题面看这里

    题目很简单,主要是自己WA的有点离谱鸭!

    先放上错误的代码吧

     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)//就错在这里!!
    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

    直接把前后两个字符做比较了,改的时候加了一个数来记录上一次选择的那个字符。

    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

    附上自己错的一组后台数据

    以后还是要认真点!

    50 13
    qwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa
  • 相关阅读:
    json2.js和wcf传递Date对象问题
    使用WebClient发送POST请求
    我也质疑下petshop
    sql server 2005 游标使用小例
    关于SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY
    小错误大麻烦
    SQL Server启动出现“指定的服务未安装”的解决方法
    非常不错的一个网站
    objectdatasource 未能找到带参数的非泛型方法
    SQLServer日志文件收缩
  • 原文地址:https://www.cnblogs.com/chen-tian-yuan/p/10611182.html
Copyright © 2011-2022 走看看