SRM 592 1A 2013.12.10

SRM 592 1A 2013.12.10

DIV 1

300

1.题目大意:有很多球,这些球只有三种颜色,按照给定的顺序插入,插入位置不同得分不同,得分=插入位置左右两边不同颜色球的个数之和。求最大得分。

2.解题思路:

(这道题是看了别人的代码,居然没有想到用集合set,因为是根据不同颜色的数量来计分的,应该用set啊,我真的笨死啦!)

维护两个集合left和right,用于表示待插入球的左边和右边的球的集合。左边颜色数+右边颜色数=此次得分。

然后决定当前的球放在左边还是右边,如果,左边没有这个颜色,就放在左边,否则放在右边。因为要尽量让两个集合中不同的颜色多,所以采用这种方式。

3.Code小技巧:


(1)String 类型的for循环,可以写成图中1

(2)三目运算符决定引用前面的内容还可以这样写,第一次见,图中2.

Problem Statement

    

Little Elephant from the Zoo of Lviv likesballs. He has some balls that he wants to arrange into a row on the table. Eachof those balls has one of three possible colors: red, green, or blue.

 

You are given a string S. This stringrepresents all of the balls Little Elephant has, in the order in which he willbe placing them onto the table. Red, green, and blue balls are represented bythe characters 'R', 'G', and 'B', respectively. Each time Little Elephantplaces a new ball onto the table, he may add it anywhere into the row ofalready placed balls.

 

Additionally, each time Little Elephantadds a ball to the table, he scores some points (possibly zero). The number ofpoints is calculated as follows:

If this is the first ball being placed onthe table, there are 0 points for it.

If he adds the current ball to one of theends of the row, the number of points scored is equal to the number ofdifferent colors of the balls on the table, excluding the current ball.

If he adds the current ball between twoother balls, the number of points scored is equal to the number of differentcolors of the balls before the current ball, plus the number of differentcolors of the balls after the current ball.

 

For example, suppose that the ballscurrently on the table form the row "GBBG". Little Elephant now wantsto add a new red ball ('R'). One of the options is to add it to the beginning.This scores 2 points and produces the row "RGBBG". Another option isto add it between "GBB" and "G". There are 2 distinctcolors in "GBB" and 1 in "G", so this move is worth 2+1 = 3points. This move produces the row "GBBRG".

 

Return the maximum total number of pointsthat Little Elephant can earn for placing the balls onto the table.

Definition

    

Class:

LittleElephantAndBalls

Method:

getNumber

Parameters:

string

Returns:

int

Method signature:

int getNumber(string S)

(be sure your method is public)

    

 

Constraints

-

S will contain between 1 and 50 characters,inclusive.

-

S will consist only of characters 'R', 'G'and 'B'.

Examples

0)

 

    

"RGB"

Returns: 3

Any strategy is optimal here. Each strategyscores 0+1+2 = 3 points.

1)

 

    

"RGGRBBB"

Returns: 21

 

2)

 

    

"RRRGBRR"

Returns: 16

 

3)

 

    

"RRRR"

Returns: 5

 

4)

 

    

"GGRRRGR"

Returns: 18

 

5)

 

    

"G"

Returns: 0

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

159.31

#include<iostream>

#include<vector>

#include<string>

#include<set>

 

using namespace std;

 

class LittleElephantAndBalls

{

public: int getNumber(string s)

                   {

                            set<char>left,right;

                            intans=0;

                            for(charc:s)

                            {

                                     ans+=left.size()+right.size();

                                     (left.count(c)?right:left).insert(c);

                            }

                            returnans;

                   }

};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值