原题地址
题目大意:有一个\(n\)个数的序列,这个序列里的数为互不相等,现在可以从序列的最左\(/\)右边选取一个数,并删去这个数,将其加入另一个序列中,要保证这另一个序列是递增的,问你这另一个序列的长度最长是多少,要怎样操作(选择最左边的数输出\(L\),最右边的数输出\(R\))。
这题主要考验贪心思想。
这题的贪心十分容易想出:选出序列两端最小的数,与另一个序列的最后一个数判断,如果大于另一个序列的最后一个数,便加入另一个序列,否则选择另一端的数判断,同样,如果大于另一个序列的最后一个数,便加入另一个序列,否则代表已经无法加入另一个序列,直接跳出判断。
需要注意的是,如果序列只剩下一个数,如果能加入,应输出\(L\)!
有了上面的贪心,代码也就显而易见了吧。
\(Code:\)
#pragma GCC diagnostic error "-std=c++11" #include <iostream> #include <cstdio> using namespace std; template<class T>void r(T &a) { T s=0,w=1;a=0;char ch=getc(stdin); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getc(stdin);} while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getc(stdin);} a=w*s; } template<class T,class... Y>void r(T& t,Y&... a){r(t);r(a...);} int a[200010]; string answer; int main() { int n,le=1,ri,last=-300000,ans=0; r(n); ri=n; for(int i=1;i<=n;i++) r(a[i]); while(le<=ri) { if(a[le]<a[ri]) { if(a[le]>last) { ans++; last=a[le]; answer+='L'; le++; } else if(a[ri]>last) { ans++; last=a[ri]; answer+='R'; ri--; } else break; } else if(a[ri]<a[le]) { if(a[ri]>last) { ans++; last=a[ri]; answer+='R'; ri--; } else if(a[le]>last) { ans++; last=a[le]; answer+='L'; le++; } else break; } else if(a[le]>last) { ans++; last=a[le]; answer+='L'; le++; } else break; } printf("%d\n",ans); cout<<answer; return 0; }转载于:https://www.cnblogs.com/Naive-Cat/p/10789727.html
相关资源:JAVA上百实例源码以及开源项目