2019-07-19  2024-09-15    537 字  2 分钟

Codeforces-C1157 C1. Increasing Subsequence (easy version)

  1. 初始化上次的数为 0,先判断上次的数是否小于数组最左边的数 并且 (数列左边的数小于右边 或 上次的数大于数列右边的数)就选择左边的数字
  2. 若不满足上面的条件,判断上次的数是否小于数组最右边的数 并且 (数列右边的数小于左边 或 上次的数大于数列左边的数)就选择右边的数字
Code
  1/**
  2 *    author: Akvicor
  3 *    created: 2019-07-19 15-57-08
  4**/
  5
  6#include <bits/stdc++.h>
  7
  8using namespace std;
  9
 10#ifdef DEBUG
 11string to_string(string s) {
 12	return '"' + s + '"';
 13}
 14
 15string to_string(const char* s) {
 16	return to_string((string) s);
 17}
 18
 19string to_string(bool b) {
 20return (b ? "true" : "false");
 21}
 22
 23template <typename A, typename B>
 24string to_string(pair<A, B> p) {
 25	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
 26}
 27
 28template <typename A>
 29string to_string(A v) {
 30	bool first = true;
 31	string res = "{";
 32	for (const auto &x : v) {
 33		if (!first) {
 34			res += ", ";
 35		}
 36		first = false;
 37		res += to_string(x);
 38	}
 39	res += "}";
 40	return res;
 41}
 42
 43void debug_out() { cerr << endl; }
 44
 45template <typename Head, typename... Tail>
 46void debug_out(Head H, Tail... T) {
 47	cerr << " " << to_string(H);
 48	debug_out(T...);
 49}
 50#endif
 51
 52#ifdef DEBUG
 53#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
 54#else
 55#define debug(...) 17
 56#endif
 57
 58#ifdef DEBUG
 59#define FAST_IO 17
 60#else
 61#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 62#define endl '\n'
 63#endif
 64
 65#define LL long long
 66#define ULL unsigned long long
 67#define rep(i, n) for(int i = 0; i < (n); ++i)
 68#define reep(i, n) for(int i = 0; i <= (n); ++i)
 69#define lop(i, a, n) for(int i = a; i < (n); ++i)
 70#define loop(i, a, n) for(int i = a; i <= (n); ++i)
 71#define ALL(v) (v).begin(), (v).end()
 72#define PB push_back
 73#define VI vector<int>
 74#define PII pair<int,int>
 75#define FI first
 76#define SE second
 77#define SZ(x) ((int)(x).size())
 78
 79const double EPS = 1e-6;
 80const double PI = acos(-1.0);
 81const int INF = 0x3f3f3f3f;
 82const LL LINF = 0x7f7f7f7f7f7f7f7f;
 83const int MAXN = (int)1e6 + 10;
 84const int MOD = (int)1e9 + 7;
 85
 86int a[MAXN];
 87
 88int main(){
 89	FAST_IO;
 90	int n;
 91	while(cin >> n){
 92		string s;
 93		rep(i, n) cin >> a[i];
 94		int i = 0, j = n-1, la = 0;
 95		while(i <= j){
 96			if(la < a[i] && (a[i]<=a[j] || la>a[j])){
 97				s.push_back('L');
 98				la = a[i];
 99				++i;
100			}else if(la < a[j] && (a[j]<a[i] || la>a[i])){
101				s.push_back('R');
102				la = a[j];
103				--j;
104			}else break;
105		}
106		cout << s.size() << endl << s << endl;
107	}
108
109	return 0;
110}

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处