Stones

题目链接(传送门) 来源:upc12899

题目描述

There are N stones arranged in a row. Every stone is painted white or black. A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone. Find the minimum number of stones that needs to be recolored.

Constraints

1≤N≤2×105

S is a string of length 

N consisting of . and #.

输入

Input is given from Standard Input in the following format:

N

S

输出

Print the minimum number of stones that needs to be recolored.

样例输入

3
#.#

样例输出

1

提示

It is enough to change the color of the first stone to white.

题目描述:

输入字符串只包含 ‘ . ’ 和 ‘ # ’,可以改变任意数目使 ‘ . ’ 变为 ‘ # ’ 或使‘ # ’ 变为 ‘ . ’,最终要求 ‘ # ’的右边不可以紧挨着 ‘ . ’,输出最小变化数目。

思路:

比赛的时候想的是每次碰到 ‘#.’ 就判断 ‘ # ’(cnt1)和‘ . ’(cnt2)的个数,令cnt+=min(cnt1,cnt2)但一直错,也找不出错误的样例,后来同学给了组样例:

##.#...# #       我的计算结果是 2  明显不对

同学让我考虑末状态(最终状态):

1、......

2、####

3、.....#### (‘ . ’全部出现在‘ # ’的左边)

也只有上面几种情况

于是:

对于1、2很好判断,直接前缀和就能求出

对于3 枚举....####交叉的位置(这个样例交叉位置是3和4)

整体取最小值就能解决啦

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5;
struct node{
int cnt1;
int cnt2;
}num[MAX+5];
int sum[MAX+5];
char a[MAX+5];
int main()
{
int la;
scanf("%d%s",&la,a);
int cnt1=0,cnt2=0;
for(int i=0;i<la;i++){
if(a[i]=='#') cnt1++;
else cnt2++;
num[i].cnt1=cnt1;
num[i].cnt2=cnt2;
}
int cnt=0;
for(int i=0;i<la;i++){
sum[cnt++]=(num[i].cnt1+(num[la-1].cnt2-num[i+1].cnt2));
}
sum[cnt++]=num[la-1].cnt2;
sum[cnt++]=num[la-1].cnt1;
sort(sum,sum+cnt);
printf("%d\n",sum[0]);
return 0;
}

Stones【中石油个人赛第十七场I】的更多相关文章

  1. 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法

    6690: Transit Tree Path 时间限制: 1 Sec  内存限制: 128 MB提交: 472  解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...

  2. iOS系统中导航栏的转场解决方案与最佳实践

    背景 目前,开源社区和业界内已经存在一些 iOS 导航栏转场的解决方案,但对于历史包袱沉重的美团 App 而言,这些解决方案并不完美.有的方案不能满足复杂的页面跳转场景,有的方案迁移成本较大,为此我们 ...

  3. 2106 Problem F Shuffling Along 中石油-未提交-->已提交

    题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...

  4. 中石油—2的幂次方(power)

    问题 E: 2的幂次方(power) 时间限制: 1 Sec  内存限制: 64 MB提交: 38  解决: 19[提交][状态][讨论版] 题目描述 任何一个正整数都可以用2的幂次方表示.例如:13 ...

  5. 中石油大学统考(大学英语B)押题笔记

    二. 词汇与结构 1. I will.意为“我会的”,固定搭配. 2. get tired of 是词组“对…厌烦了”的意思. 3. — ________ is your girl friend li ...

  6. 2078 Problem H Secret Message 中石油-未提交-->已提交

    题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...

  7. 中石油-高精度阶乘-java

    问题 F: [高精度]高精度阶乘 时间限制: 1 Sec  内存限制: 64 MB提交: 49  解决: 13[提交][状态][讨论版] 题目描述 <魔法宝典>对于修罗王是如此重要,是因为 ...

  8. 中石油-高精度除法-java版

    问题 G: [高精度]高精度数除以低精度数I 时间限制: 1 Sec  内存限制: 512 MB提交: 19  解决: 15[提交][状态][讨论版] 题目描述 修罗王聚集了庞大的暗元素以施展隐匿魔法 ...

  9. 备战省赛组队训练赛第十七场(UPC)

    upc:传送门 A: 题解[1] G: 题解[1] D,G,H,J,L 题解 by 鲁东大学

随机推荐

  1. .Net数据集导出到Excel样式细节---------------摘自别人的

    .Net数据集导出到Excel样式细节 本文的目的是总结一些在做Excel导出功能时需要注意的样式细节.使用环境是Asp.Net,数据集的形式是Html Table,Excel还是识别一些CSS代码的 ...

  2. 用了这么多年的 Java 泛型,你对它到底有多了解?

    作为一个 Java 程序员,日常编程早就离不开泛型.泛型自从 JDK1.5 引进之后,真的非常提高生产力.一个简单的泛型 T,寥寥几行代码, 就可以让我们在使用过程中动态替换成任何想要的类型,再也不用 ...

  3. Django中的事务与ajax

    一 事务与锁 1.行级锁 行级锁是由存储引擎实现的.如mysql里默认指定的InnoDB存储引擎,由它实现行级锁.InnoDB的行级锁定同样分为两种类型,共享锁(X)和排他锁(S). 对于UPDATE ...

  4. Android设置按钮透明

    <Button android:id="@+id/bt3" android:layout_width="163dp" android:layout_hei ...

  5. Python之Flask框架一

    flask是一个使用 Python 编写的轻量级 Web 应用框架.轻巧页就意味着他比较简洁,不过见到的MTV框架还是有的,(MVC)但是最重要的还是他的可扩展性很强,对比与Django框架呢,他的灵 ...

  6. 项目工程化之git提交规范以及 CHANGELOG生成

    事先声明,本文是参考了其他大神的博客之后自己尝试的记录,具体可以参考如下 链接 先说说git 提交规范把,这里基本都是这个工具 cz-customizable 1,安装 npm install cz- ...

  7. [安卓自动化测试] 001.UIAutomator初探

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. [优文翻译]001.真正程序员该是什么样的(How To Be A Real Programmer)

    01.Real Programmers don't write specs -- users should consider themselves lucky to get any programs ...

  9. java第十三周课后作业 0529

    1.把多个企鹅的信息添加到集合中查看企鹅的数量及所有企鹅的信息删除集合中部分企鹅的元素判断集合中是否包含指定企鹅 package homework; import java.util.ArrayLis ...

  10. Java实现 LeetCode 792 自定义字符串排序(暴力)

    792. 匹配子序列的单词数 给定字符串 S 和单词字典 words, 求 words[i] 中是 S 的子序列的单词个数. 示例: 输入: S = "abcde" words = ...