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 鲁东大学
随机推荐
- 永久关闭windows更新步骤
在搜索“web和windows”框中输入“服务” 在搜索结果中点击第一个,那个图标像齿轮的那个!如下图. 在打开的“服务”窗口中,我们找到windows update 找到”windows upd ...
- node进程间通信
作为一名合格的程序猿/媛,对于进程.线程还是有必要了解一点的,本文将从下面几个方向进行梳理,尽量做到知其然并知其所以然: 进程和线程的概念和关系 进程演进 进程间通信 理解底层基础,助力上层应用 进程 ...
- Typora Ubuntu 不显示 加粗
问题描述: Typora 在 Ubuntu18.04 上面不显示 Markdown 加粗语法 解决办法: 在 Typora's github.css 里面,将 body 修改为如下内容 body { ...
- Hello, CTF
0x01 拿到题目后查壳,发现什么也没有,32位vc++ 0x02 放到IDA里,F5反编译,得到下图 很容易我们就看到了比较的函数,以及出现wrong和success的字符串,所以接下来就是仔细分析 ...
- parrot os安装vmware pro
转载https://www.linuxtechi.com/install-vmware-workstation-14-debian-9/ Open the terminal and execute t ...
- Cypress系列(3)- Cypress 的初次体验
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...
- Rocket - diplomacy - LazyModule的实例化(补)
https://mp.weixin.qq.com/s/rgCRorjPYyyD6i7moIzbZg 介绍LazyModule和Node构造方法的执行过程,即实例化过程. 1. NullIn ...
- Java实现 LeetCode 796 旋转字符串 (水题)
796. 旋转字符串 给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结果就是'bcdea' .如果在若干次旋转 ...
- Java实现 LeetCode 763 划分字母区间(暴力)
763. 划分字母区间 字符串 S 由小写字母组成.我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段.返回一个表示每个字符串片段的长度的列表. 示例 1: 输入: S = & ...
- Java实现蓝桥杯历届试题格子刷油漆
问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形(如下图所示),现需要把这些格子刷上保护漆. 你可以从任意一个格子刷起,刷完一格,可以移动到和它相邻的格子(对角相邻也算数),但不能移动 ...