codeforces C. Sorting Railway Cars 贪心
来源:互联网 发布:linux下c语言编程ide 编辑:程序博客网 时间:2024/05/16 06:10
An infinitely long railway has a train consisting of n cars, numbered from1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.
The second line contains n integers pi (1 ≤ pi ≤ n,pi ≠ pj ifi ≠ j) — the sequence of the numbers of the cars in the train.
Print a single integer — the minimum number of actions needed to sort the railway cars.
54 1 2 5 3
2
44 1 3 2
2
In the first sample you need first to teleport the 4-th car, and then the5-th car to the end of the train.
题意: 有一个1-n的排列(排列是指全是1-n中的数字,并且各不相同), 每次可以任意从里面拿出来一个放在最前面或者最后面,求最小的次数把这个数列排好序.
分析: 开始想的是求这个排列中的最长递增子序列, 找到最长的一部分, 这一部分不用动, 只动别的部分就行, 然而是不对的, 比如1 2 4 5 3就不符合, 后来一想, 最长递增子序列的相邻两个必须是差值为一. 这样才能保持不动, 然后动别的数..
#include<bitset>#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}const int N=100100;int a[N],sum[N],ans;int main(){ int n=in(); for(int i=0;i<n;i++) { int x=in(); sum[x]=sum[x-1]+1; //这个数的差值为1的最长递增子序列等于前面一个数的最长递增子序列加1 ans=max(ans,sum[x]); } cout<<n-ans<<endl; return 0;}
类似题: hdu 5500
Reorder the Books
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 994 Accepted Submission(s): 554
dxy puts these books in a book stack with the order of their numbers increasing from top to bottom. dxy takes great care of these books and no one is allowed to touch them.
One day Evensgn visited dxy's home, because dxy was dating with his girlfriend, dxy let Evensgn stay at home himself. Evensgn was curious about this series of books.So he took a look at them. He found out there was a story about "Little E&Little Q". While losing himself in the story,he disrupted the order of the books.
Knowing that dxy would be back soon,Evensgn needed to get the books ordered again.But because the books were too heavy.The only thing Evensgn could do was to take out a book from the book stack and and put it at the stack top.
Give you the order of the disordered books.Could you calculate the minimum steps Evensgn would use to reorder the books? If you could solve the problem for him,he will give you a signed book "The Stories of SDOI 9: The Story of Little E" as a gift.
There is an positive integer
For each testcase, there is an positive integer
Followed
Hint:
For the first testcase:Moving in the order of
For the second testcase:It's already ordered so there is no operation needed.
244 1 2 351 2 3 4 5
30
题意: 1-n的排列, 每次从中选择一个数放在最前面, 求最小的步数使得数列排好序.
分析: 这次是只能放在最前面. ,想一下应该固定的最长的序列应该是什么样的, 这次就不是上面的差值为1的最长递增子序列了, 而是结尾为n的差值为1的最长递增子序列.因为如果结尾不是n的话,这段数列是不能固定的, 因为结尾不是n的话,肯定有比他们大的数在他们前面, 这段肯定会被移动.
拓展: 1-n的排列, 每次从中选择一个数放在最后面, 求最小的步数使得数列排好序.?
#include<bitset>#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}const int N=100100;int sum[N];int main(){ int T=in(); while(T--) { int n=in(); mem(sum,0); for(int i=0;i<n;i++) { int x=in(); sum[x]=sum[x-1]+1; } cout<<n-sum[n]<<endl; //n-结尾为n的差值为1的最长递增子序列 } return 0;}
官方题解: 首先发现最大的数n是不用操作的, 操作的话只会让步数增加, 操作别的数到他前面去, 他自然就在后面了,我们从后往前找到n的位置, 从n的位置往前找到n-1的位置,如果没找到, 说明n-1在n的后面, 那么原数列肯定最少需要n-1次操作排序 ,(把n-1移到最前面, 然后从大到小一个个移动就行了,); 如果n-1在n 的前面, 那么n-1就不用移动了, 原理同n的一样,同理, 我们接着从(n−1)(n-1的位置往前找(n−2)(n-2),再从(n−2)(n-2)的位置往前找(n−3)(n-3)...假如数kk找不到了,那么就至少需要kk次操作。这种做法的复杂度是O(n)O(n)的
#include<bitset>#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}const int N=100010;int a[22];int main(){ int T=in(); while(T--) { int n=in(); for(int i=0;i<n;i++) a[i]=in(); int ans=n; for(int i=n-1;i>=0;i--) { if(ans==a[i]) ans--; } cout<<ans<<endl; } return 0;}
- codeforces C. Sorting Railway Cars 贪心
- Codeforces 605A Sorting Railway Cars [贪心]
- 【Codeforces 605A】【贪心】Sorting Railway Cars
- 贪心--Sorting Railway Cars
- Codeforces 606 C Sorting Railway Cars【思维】
- Codeforces 606 C Sorting Railway Cars
- codeforces 335 C Sorting Railway Cars
- C - Sorting Railway Cars
- C. Sorting Railway Cars
- CodeForces 605 A.Sorting Railway Cars(贪心)
- Codeforces Round #335 (Div. 2) C.Sorting Railway Cars
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
- CodeForces 606C--Sorting Railway Cars,思路题~~~
- Codeforces Round #335 (Div. 2)C. Sorting Railway Cars
- CF 605A(Sorting Railway Cars-贪心)
- Sorting Railway Cars----CodeForces 605A
- codeforces 605 A Sorting Railway Cars
- Codeforces 605A.Sorting Railway Cars【技巧】
- wlan 相关 华为技术论坛
- MySql5.7.10初始化
- 部门管理中非递归搜索部门以及所管辖下部门
- wget
- Oracle-SQL Plus学习总结
- codeforces C. Sorting Railway Cars 贪心
- 文章标题
- 一个用Python编写的股票数据(沪深)爬虫和选股策略测试框架
- 机器视觉开源代码集合
- iOS多线程NSOperation的使用
- Eclipse 使用自己的签名包进行签名
- 易语言浏览网页
- jQuery学习--jQuery选择器
- LinkedBlockingQueue_demo