1. 程式人生 > >Codeforces Round #499 (Div. 2) A Stages (java)

Codeforces Round #499 (Div. 2) A Stages (java)

型別:貪心
題意:給你一堆字元a-z(n個),然後選k個字元組裝起來,要求後一個字元不能是前一個字元的相鄰
題解:開個27的陣列,從a到z貪心,標記選取的前一個字元,記錄字元權值和,記錄選取個數,選取個數不達到k則輸出-1

import java.util.Scanner;

public class A{
    public static void main(String[] args) {
        int a[]=new int[27];
        Scanner kb = new Scanner(System.in);
        int n=kb.nextInt();
        int
t=kb.nextInt(); String str=kb.next(); char ch[]=str.toCharArray(); for(int i=0;i<ch.length;i++) { int b=ch[i]-'a'+1; a[b]++; } int bj=0; int bj1=-1; int sum=0; for(int i=1;i<=26&&bj<t;i++) { if
(a[i]>0&&bj1!=i-1) { sum+=i; bj1=i; bj++; } } if(bj<t) { System.out.println(-1); }else { System.out.println(sum); } kb.close(); } }

相關推薦

Codeforces Round #499 (Div. 2) A Stages (java)

型別:貪心 題意:給你一堆字元a-z(n個),然後選k個字元組裝起來,要求後一個字元不能是前一個字元的相鄰 題解:開個27的陣列,從a到z貪心,標記選取的前一個字元,記錄字元權值和,記錄選取個數

Codeforces Round #499 (Div. 2) C. Fly(java

型別:模擬 題意:從地球起飛->星球1降落->星球1起飛->星球2起飛->······->星球n-1起飛->地球降落 題解:逆過程模擬,如果費用存在<=

Codeforces Round #416 (Div. 2) A+B

src separate not sum redo swe tput output depend A. Vladik and Courtesy 2 seconds 256 megabytes At regular competition Vl

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模擬 枚舉

ces color 一次 name exit main cst space amp Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模擬 枚舉 題意 一個紅綠燈 按逆時針方向一次給出各個路口的左轉,

Codeforces Round #422 (Div. 2) A. I'm bored with life 暴力

out line leave c++ ren round ... from cif A. I‘m bored with life Holidays have finished. Thanks to the help of t

Codeforces Round #306 (Div. 2) A

return code add stl ext scan java pac 一個 題意 給一個字符串(長度<=10^5)。問當中有沒有一個”BA”和一個”AB”呢?假設都有而且它們不反復(即ABA不算),輸出YES。否則輸出NO。 思路 一開

Codeforces Round #FF (Div. 2) A. DZY Loves Hash

turn esp while integer each lov article opera title DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to ins

Codeforces Round #426 (Div. 2)A B C題+賽後小結

ase com || namespace inf exp test 鏈接 %d   最近比賽有點多,可是好像每場比賽都是被虐,單純磨礪心態的作用。最近講的內容也有點多,即便是點到為止很淺顯的版塊,刷了專題之後的狀態還是~"咦,能做,可是並沒有把握能A啊"。每場網絡賽,我似乎

Codeforces Round #422 (Div. 2) A-C

put math def flag tdi mem ostream bag cmp A. I‘m bored with life 水題 #include <iostream> #include <cstring> #include <

Codeforces Round #423 (Div. 2) A-C

include 組成 tro return for can sta har min A. Restaurant Tables 這裏看錯題意還wa了兩發.... 按題意模擬就行了 水題 #include <iostream> #include <

Codeforces Round #424 (Div. 2) A-C

namespace ios nim tmp main 一個 ... blog cin A. Unimodal Array 水題 #include <iostream> #include <cstring> #include <cst

Codeforces Round #363 (Div. 2) A-C

ani ++ string 表示 math ems 所有 output name A. Launch of Collider 找最近的R和L之間的距離 #include <iostream> #include <cstring> #inc

Codeforces Round #431 (Div. 2) A

... mda for index val because else into ces Where do odds begin, and where do they end? Where does hope emerge, and will they ever brea

Codeforces Round #447 (Div. 2) A】QAQ

pro ont 題解 problem fde c語言 span bits endif 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 C語言程序練習題 【代碼】 #include <bits/stdc++.h> using

Codeforces Round #197 (Div. 2) A. Helpful Maths【字符串/給一個連加計算式,只包含數字 1、2、3,要求重新排序,使得連加的數字從小到大】

asi man title problem beginning 排序 stand should cati A. Helpful Maths time limit per test 2 seconds memory limit per t

Codeforces Round #453 (Div. 2) a-c

!= force gpo n) return sizeof size nod amp A. Visiting a Friend 水題,但是需要註意段點初,及最後的位置 代碼如下: #include <iostream> #include <stdio.h

Codeforces Round #457 (Div. 2) A】 Jamie and Alarm Snooze

als lar clu efi out .com bit source fin 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 暴力往前走x分鐘就好。 直到出現7為止。 【代碼】 #include <bits/stdc++.h&

Codeforces Round #396(Div. 2) A. Mahmoud and Longest Uncommon Subsequence

color HA \n seq 字符 turn ces DC %s 【題意概述】   找兩個字符串的最長不公共子串。 【題目分析】   兩個字符串的最長不公共子串就應該是其中一個字符串本身,那麽判斷兩個字符串是否相等,如果相等,那麽肯定沒有公共子串,輸出“-1”.否則就

Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad

sca \n ret name nbsp style can turn ros 【題意概述】    在一個十字路口 ,給定紅綠燈的情況, 按逆時針方向一次給出各個路口的左轉,直行,右轉,以及行人車道,判斷汽車是否有可能撞到行人     【題目分析】   需要在邏輯上清晰