Stones【中石油个人赛第十七场I】
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】的更多相关文章
- 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法
6690: Transit Tree Path 时间限制: 1 Sec 内存限制: 128 MB提交: 472 解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...
- iOS系统中导航栏的转场解决方案与最佳实践
背景 目前,开源社区和业界内已经存在一些 iOS 导航栏转场的解决方案,但对于历史包袱沉重的美团 App 而言,这些解决方案并不完美.有的方案不能满足复杂的页面跳转场景,有的方案迁移成本较大,为此我们 ...
- 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 ...
- 中石油—2的幂次方(power)
问题 E: 2的幂次方(power) 时间限制: 1 Sec 内存限制: 64 MB提交: 38 解决: 19[提交][状态][讨论版] 题目描述 任何一个正整数都可以用2的幂次方表示.例如:13 ...
- 中石油大学统考(大学英语B)押题笔记
二. 词汇与结构 1. I will.意为“我会的”,固定搭配. 2. get tired of 是词组“对…厌烦了”的意思. 3. — ________ is your girl friend li ...
- 2078 Problem H Secret Message 中石油-未提交-->已提交
题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...
- 中石油-高精度阶乘-java
问题 F: [高精度]高精度阶乘 时间限制: 1 Sec 内存限制: 64 MB提交: 49 解决: 13[提交][状态][讨论版] 题目描述 <魔法宝典>对于修罗王是如此重要,是因为 ...
- 中石油-高精度除法-java版
问题 G: [高精度]高精度数除以低精度数I 时间限制: 1 Sec 内存限制: 512 MB提交: 19 解决: 15[提交][状态][讨论版] 题目描述 修罗王聚集了庞大的暗元素以施展隐匿魔法 ...
- 备战省赛组队训练赛第十七场(UPC)
upc:传送门 A: 题解[1] G: 题解[1] D,G,H,J,L 题解 by 鲁东大学
随机推荐
- zz 关于插入意向间隔锁( insert intention gap lock)产生的死锁问题
出处: http://www.cnblogs.com/sunss/p/3166550.html 昨天看到一个很有意思的死锁,拿来记录下: 环境:deadlock on 事务隔离级别: read com ...
- 二、第一个C程序:Hello World!
如何在Dev C++中编辑.编译和运行程序? 一.打开Dev C++ 二.在上面编辑窗口中输入以下代码 #include<stdio.h> int main() { printf(&quo ...
- celery异步消息队列的使用
1.准备工作 1.1 流程图 2.环境安装 2.1.在Ubuntu中需要安装redis 安装redis $sudo apt-get update $sudo apt-get install redis ...
- Java——倒序输出Map集合
package com.java.test.a; import java.util.ArrayList; import java.util.LinkedHashMap; import java.uti ...
- MyBatis——com.mysql.cj.exceptions.InvalidConnectionAttributeException
报错信息 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.c ...
- 面向对象案例 - 学生信息管理系统V1.0
学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...
- 【书签】连续型特征的归一化和离散特征的one-hot编码
1. 连续型特征的常用的归一化方法.离散型特征one-hot编码的意义 2. 度量特征之间的相关性:余弦相似度和皮尔逊相关系数
- 1. vue文件结构
1)这是文件规则 <template> <div > </div> </template> <script> Name:"当前的名 ...
- CPU亲和度
CPU亲和度(CPU Affinity),就是将一个进程或者线程强制绑定在CPU的某一个core上运行. 参考:https://www.cnblogs.com/zhangxuan/p/6427533. ...
- Java实现 蓝桥杯 基础练习 查找整数
基础练习 查找整数 时间限制:1.0s 内存限制:256.0MB 提交此题 锦囊1 锦囊2 问题描述 给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个. 输入格式 第一行包含一个整数n ...