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 鲁东大学
随机推荐
- easyui及读取xml
本地测试地址例如http://localhost:6541/TreeExam/AuthorityTree TreeExam 是TreeExamController AuthorityTree是Tree ...
- PL/SQL语言语法
一.前言 SQL全称是"结构化查询语言(Structured Query Language)",而PL/SQL是过程语言(Procedure Language),是对SQL的扩展. ...
- HTML使用正则验证
制作HTML前台用户验证等,需要对用户名或者密码进行验证,这时使用正则表达式能够精确地对text进行限制. 具体在HTML中的运用代码如下: 转自 https://blog.csdn.net/weix ...
- Node.js NPM Tutorial
Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...
- SpringBoot2.1电商通用(微信+支付宝)支付系统实战
『课程目录』: ├─第10章 全模块电商系统之商品模块 │ 10-1_商品列表-上.mp4 │ 10-2_商品列表-中.mp4 │ 10-3_商品列表-下.mp4 │ ...
- (上)python3 selenium3 从框架实现学习selenium让你事半功倍
本文感谢以下文档或说明提供的参考. Selenium-Python中文文档 Selenium Documentation Webdriver 参考 如有错误欢迎在评论区指出,作者将即时更改.文章从我的 ...
- fastclick从接触到丢弃
fastclick简介 fastclick是一款为了解决移动端300ms点击延迟而诞生的插件. 在移动端,如果对页面没有做任何处理,点击一个元素,触发的事件流程可简单理解为:touch -> 经 ...
- Jmeter(三) - 从入门到精通 - 测试计划(Test Plan)的元件(详解教程)
1.简介 上一篇中宏哥已经教你如何通过JMeter来创建一个测试计划(Test Plan),那么这一篇我们就将JMeter启动起来,创建一个测试计划(Test plan),然后宏哥给大家介绍一下测试计 ...
- jpype2
# -*- coding: utf-8 -*-# @Time : 2020/5/21 0:04# 从环境变量获取jvm虚拟机安装路径,若为None则获取默认路径import os import jpy ...
- 【Ubuntu】利用sudo修改/etc/sudoers翻车
翻车背景:利用命令行创建新用户,这里不得不提该翻车博客[1],当然这里并没有怪罪的意思,贴出来只是为了让后来者使用正确命令修改sudoers文件.系统:Ubuntu18.04 利用[1]中的“新用 ...