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. shell脚本定时执行|关闭jar文件

    编写shell脚本,用于启动.关闭jar程序: #!/bin/bash #description: 启动重启server服务 #需要配置环境变量后才能直接使用java这些变量 export JAVA_ ...

  2. js读取json

    Json字符串是: [{"n":"aaa","un":"aaa"},{"n":"yang& ...

  3. 非常实用的织梦dede所有标签调用方法大全

    关键描述调用标签: <meta name="keywords" content="{dede:field name='keywords'/}">&l ...

  4. jpype2

    # -*- coding: utf-8 -*-# @Time : 2020/5/21 0:04# 从环境变量获取jvm虚拟机安装路径,若为None则获取默认路径import os import jpy ...

  5. [Office#PPT]0001.实例剖析-如何制作一个牛B的融资PPT

    一些好公司明明在模式上富有创新性,但却在 pitch 时很保守,而且他们又不缺设计和开发能力.如何设计你的融资演讲稿才会吸引投资人对你投资?投资人 Daniel Eckler给大家贡献了下面这篇技术贴 ...

  6. [注]一将功成万骨枯!App的七种死法

    一将功成万骨枯,这种事在有泡沫的行业总是会发生的.移动互联网尤甚.从<愤怒的小鸟>到<植物大战僵尸>.<捕鱼达人>.<唱吧>.<陌陌>……一 ...

  7. [Python基础]005.语法(4)

    语法(4) 类 创建 self 方法 变量 综合例子 继承 类 创建 与其他大部分语言一样,Python也用 class 来创建类. class Person: # Person类 pass # 空语 ...

  8. Docker 笔记一相关命令

    Centos 7 : Service network restart 重启网络 Ip addr 查看ip地址 Uname -r 查看内核版本 Yum install docker 安装docker 命 ...

  9. Binlog2sql+CentOS7 离线安装

    Binlog2sql+CentOS7 离线安装 目录 Binlog2sql+CentOS7 离线安装 1. 环境 2. 下载 3.1 Pip 安装 3.2 PyMySQL/mysql-replicat ...

  10. python字母串查找基本操作

    字符串查找基本操作主要分为三个关键词:find().index().count(). 这三个用法相同,格式都是为:自定义字符串名.关键词(‘子串’,开始位置,结束位置),开始和结束范围可不写. 1.f ...