当前位置:网站首页>[code source] daily one question non decreasing 01 sequence
[code source] daily one question non decreasing 01 sequence
2022-07-25 09:37:00 【self_ disc】
Topic link : Non decreasing 01 Sequence - subject - Daimayuan Online Judge
Topic
huaji There is one 01 Sequence , You can negate one of them every time (0 change 1,1 change 0)
Finding at least a few bits can make the sequence become The decreasing Sequence
Input format
Enter an integer in the first line n (1≤n≤10^6)
On the second line, enter a length of n And only 0 and 1 String
Output format
Output an integer , Is the minimum number of operations that the sequence becomes a non decreasing sequence
sample input
6
010110sample output
2analysis :
Non decreasing sequence , Isn't it incremental , Consider that the last substring is 00……11…… This form , It is made up of continuous 0 And continuous 1 constitute , So let's consider enumeration 0 and 1 The dividing point of , The number of operations is in front of the dividing point 1 The number of (1 Must become 0) After the dividing point 0 The number of (0 It has to be 1).
See the code for details. :
#include <bits/stdc++.h>
using namespace std;
int ans = 1e9;
int presum[1000009], n;
int main()
{
cin >> n;
string s;
cin >> s;
s = "&" + s; // Subscript string from 1 Start
for (int i = 1; i <= n; i++) // Preprocessing prefixes and
{
int t = s[i] - '0';
presum[i] = presum[i - 1] + t;
}
for (int i = 1; i <= n; i++) // enumeration 0,1 Dividing point
{
int cur = 0;
cur += presum[i - 1]; // i front 1 The number of + i Back 0 The number of
cur += (n - i - (presum[n] - presum[i]));
ans = min(ans, cur);
}
cout << ans;
}
边栏推荐
- The difference between abstract classes and interfaces (the most detailed)
- What is cerebral fissure?
- Operation 7.19 sequence table
- 自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
- OC -- Foundation -- dictionary
- A brief introduction to the interest of convolutional neural networks
- Numpy - Construction of array
- UI——无限轮播图和分栏控制器
- Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
- Redis set 结构命令
猜你喜欢

【cf】Round 128 C. Binary String
![[gplt] 2022 popular lover (Floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[gplt] 2022 popular lover (Floyd)

UI——无限轮播图和分栏控制器

~4.1 剑指 Offer 05. 替换空格

那天帮妹纸装了个数据库。。。就又帮她整理了篇快捷键

Redis string structure command

作业7.15 shell脚本

Swagger2 shows that there is a problem with the get interface, which can be solved with annotations

OC -- first acquaintance

OC -- object replication
随机推荐
kotlin基础知识点
OC--类别 扩展 协议与委托
OC -- category extension agreement and delegation
OC -- first acquaintance
Swift简单实现待办事项
Object initialization
Numpy array attribute, shape changing function, basic operation
Redis string structure command
Student management system (summary)
Basic network knowledge
What is anemia model and congestion model?
学习新技术语言流程
Go foundation 4
OC--Foundation--字典
Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
Data preprocessing
What are stand-alone, cluster and distributed?
[code source] daily question - queue
App的生命周期和AppleDelegate,SceneDelegate
Database operation language (DML)