cf 605A Sorting Railway Cars 贪心 简单题
其实就是求总长度 - 一个最长“连续”自序列的长度
最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6...
遍历一遍,贪心就是了
遍历到第i个时,此时值为a[i],如果a[i]-1在前面已经出现过了,则len[a[i]] = len[a[i-1]]+1
否则len[a[i]] = 1
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <iostream>
- using namespace std;
- const int MAXN = +;
- bool pos[MAXN];
- int len[MAXN];
- void solve()
- {
- int n;
- scanf("%d",&n);
- memset(pos,false,sizeof pos);
- for(int i=;i<=n;i++){
- int a;
- scanf("%d",&a);
- if(pos[a - ]){
- len[a] = len[a-] + ;
- }
- else{
- len[a] = ;
- }
- pos[a] = true;
- }
- int ma = -;
- for(int i=;i<=n;i++){
- if(len[i] > ma)
- ma = len[i];
- }
- printf("%d\n",n - ma);
- return ;
- }
- int main()
- {
- solve();
- return ;
- }
cf 605A Sorting Railway Cars 贪心 简单题的更多相关文章
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 605A Sorting Railway Cars 思维
早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...
- CodeForces 605A Sorting Railway Cars
求一下最长数字连续上升的子序列长度,n-长度就是答案 O(n)可以出解,dp[i]=dp[i-1]+1,然后找到dp数组最大的值. #include<cstdio> #include< ...
- CodeForces 606C--Sorting Railway Cars,思路题~~~
C - Sorting Railway Cars Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划
C. Sorting Railway Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/conte ...
- A. Sorting Railway Cars
A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 606-C:Sorting Railway Cars(LIS)
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- P188 实战练习(父类和子类)
1.创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将它向上转型到基类并调用这个方法. 创建Computer父类: package org.hanqi.practise ...
- 黑马程序员——JAVA基础之多态与Object
------- android培训.java培训.期待与您交流! ---------- 多态 : 多态定义: 某一类事物的多种存在形态. 多态的体现: 父类的引用指向了自己的子类对象. ...
- 一些实用的JS
1.str.split(/\s+/) 这句是表示以和/\s+/匹配的字符串作为分界,分割字符串str 比如一个空格或者多个或者空格以及回车等 其中+表示一个或者多个 var a = "b- ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- javascript闭包实例
实例一 //每次执行一次c()i加1.关键在于var c=a():c容器将i装载记住了. function a(){ var i=0; function b(){ alert(++i); } retu ...
- centos6.4 网络适配器设置仅主机模式
网络适配器设置仅主机模式时: 1.vmnet1网卡必须开启
- linux -redhat rpm 和zabbix和各种rpm包下载地址
redhat ftp://ftp.redhat.com/pub/redhat/linux/enterprise/6Client/en/os/SRPMS/ zabbix https://sourcefo ...
- 【转载】关于Python中的yield
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...
- redis模块
http://www.cnblogs.com/melonjiang/p/5342505.html http://www.django-china.cn/topic/1054/ 1.连接方式 redis ...
- javascript this 代表的上下文,JavaScript 函数的四种调用形式
JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.其中JavaScript 中的 this 关键字,就是一个比较容易混乱的概念,在不同的场景下,this会化身不同的 ...