原题地址ios
题目大意:有一个\(n\)个数的序列,这个序列里的数为互不相等,如今能够从序列的最左\(/\)右边选取一个数,并删去这个数,将其加入另外一个序列中,要保证这另外一个序列是递增的,问你这另外一个序列的长度最长是多少,要怎样操做(选择最左边的数输出\(L\),最右边的数输出\(R\))。c++
这题主要考验贪心思想。spa
这题的贪心十分容易想出:选出序列两端最小的数,与另外一个序列的最后一个数判断,若是大于另外一个序列的最后一个数,便加入另外一个序列,不然选择另外一端的数判断,一样,若是大于另外一个序列的最后一个数,便加入另外一个序列,不然表明已经没法加入另外一个序列,直接跳出判断。c++11
须要注意的是,若是序列只剩下一个数,若是能加入,应输出\(L\)!code
有了上面的贪心,代码也就显而易见了吧。get
\(Code:\)string
#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; }