CF-920C-Swap Adjacent Elements 贪心
题意
给你一个1~n的排列。
并给你一个字符串——其中用0和1表示对应数列中的位置上的值可不可以和后面相邻的数交换。
判断该数列能否在限制中交换为不降序数列。
思路
由于刚学了树状数组,一开始以为是用这样的数据结构去找有没有逆序。
事实上题目中的1~n并且每个数并不相同应该引起注意。关键就是这了。
下面是两种不同的思路;
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
//这个思路是只要当前的最大比位子i大,说明要向后移动,要是str=0,则NO;
using namespace std;
int n,a[+];
string str;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
cin>>str;
int maxn=,flag=;
for(int i=;i<=n;i++)
{
if(maxn < a[i])maxn = a[i];
if(str[i-]=='')
{
if(maxn>i)
{
flag=;
break;
}
}
}
if(flag)puts("YES");
else puts("NO");
return ;
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
//队长是先跑字符串,判断每个i位子的最大可能值;
using namespace std;
int a[],dp[],n;
string str;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
cin>>str;
dp[n-]=n;
for(int i=n-;i>=;i--)
{
if(str[i]=='')
{
dp[i]=dp[i+];
}
else dp[i]=i+;
}
int flag=;
for(int i=;i<=n;i++)
{
if(a[i]>dp[i-])
{
flag=;
break;
}
}
if(flag)puts("YES");
else puts("NO"); return ;
}
CF-920C-Swap Adjacent Elements 贪心的更多相关文章
- CodeForces - 920C Swap Adjacent Elements
传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...
- CF920C Swap Adjacent Elements 贪心
我也不知道该说啥,水就是了~ code: #include <bits/stdc++.h> #define N 300004 #define setIO(s) freopen(s" ...
- Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...
- Swap Adjacent Elements
You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this ...
- 【Educational Codeforces Round 37 C】 Swap Adjacent Elements
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然l..r这一段连续的1可以把l..r+1变成有序的. 那么就把所有的连续1段变成有序的就好. 看看最后是不是升序即可. [代码] ...
- CF 435B Pasha Maximizes(贪心)
题目链接: [传送门][1] Pasha Maximizes time limit per test:1 second memory limit per test:256 megabytes ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- [CF #288-C] Anya and Ghosts (贪心)
题目链接:http://codeforces.com/contest/508/problem/C 题目大意:给你三个数,m,t,r,代表晚上有m个幽灵,我有无限支蜡烛,每支蜡烛能够亮t秒,房间需要r支 ...
随机推荐
- nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may ...
- Linux零拷贝技术,看完这篇文章就懂了
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫. 本文讲解 ...
- Java编程思想之十七 容器深入研究
17.1 完整的容器分类方法 17.2 填充容器 import java.util.*; class StringAddress { private String s; public StringAd ...
- MOCTF-Crypt-writeup
MOctf Crypt Writeup记录 都不难,就随便记录记录下. MOCTF平台地址:http://www.moctf.com 0x01 数据库密码 hint:20岁的小刚,自幼热爱信息安全,一 ...
- RocketMQ中Broker的启动源码分析(一)
在RocketMQ中,使用BrokerStartup作为启动类,相较于NameServer的启动,Broker作为RocketMQ的核心可复杂得多 [RocketMQ中NameServer的启动源码分 ...
- kafka同步异步消费和消息的偏移量(四)
1. 消费者位置(consumer position) 因为kafka服务端不保存消息的状态,所以消费端需要自己去做很多事情.我们每次调用poll()方法他总是返回已经保存在生产者队列中还未被消费者消 ...
- 深入研究BufferedInputStream内幕
目录 1 概述 2 BufferedInputStream源码分析 3 BufferedInputStream在实际场景中,没有太多用处 4 BufferedInputStream唯一使用场景 1 概 ...
- python匿名函数的介绍及用途
匿名函数 用lambda能够创建一个匿名函数,这中函数得名于省略了用def声明函数的标准步骤. 语法 lambda [arg1 [,arg2,.....argn]]:expression 如何使用 我 ...
- 关于c++中的复合类型
目录 数组 字符串 结构体 共用体 枚举 指针 数和指针的关系 常见的存储方式 数组替代品 一.数组 存储在每个元素中值的类型 数组名 数组中的元素数 通用格式:typename arrayname ...
- 行车记+翻车记:.NET Core 新车改造,C# 节能降耗,docker swarm 重回赛道
非常抱歉,10:00~10:30 左右博客站点出现故障,给您带来麻烦了,请您谅解. 故障原因与博文中谈到的部署变更有关,但背后的问题变得非常复杂,复杂到我们都在怀疑与阿里云服务器 CPU 特性有关. ...