2019-07-19  2024-09-15    483 字  1 分钟

Codeforces-C1157 C1. Increasing Subsequence (easy version)

  1. 初始化上次的数为 0,先判断上次的数是否小于数组最左边的数 并且 (数列左边的数小于右边 或 上次的数大于数列右边的数)就选择左边的数字
  2. 若不满足上面的条件,判断上次的数是否小于数组最右边的数 并且 (数列右边的数小于左边 或 上次的数大于数列左边的数)就选择右边的数字
Code
/**
 *    author: Akvicor
 *    created: 2019-07-19 15-57-08
**/

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG
string to_string(string s) {
	return '"' + s + '"';
}

string to_string(const char* s) {
	return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
	bool first = true;
	string res = "{";
	for (const auto &x : v) {
		if (!first) {
			res += ", ";
		}
		first = false;
		res += to_string(x);
	}
	res += "}";
	return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}
#endif

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 17
#endif

#ifdef DEBUG
#define FAST_IO 17
#else
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#endif

#define LL long long
#define ULL unsigned long long
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define reep(i, n) for(int i = 0; i <= (n); ++i)
#define lop(i, a, n) for(int i = a; i < (n); ++i)
#define loop(i, a, n) for(int i = a; i <= (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define VI vector<int>
#define PII pair<int,int>
#define FI first
#define SE second
#define SZ(x) ((int)(x).size())

const double EPS = 1e-6;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const LL LINF = 0x7f7f7f7f7f7f7f7f;
const int MAXN = (int)1e6 + 10;
const int MOD = (int)1e9 + 7;

int a[MAXN];

int main(){
	FAST_IO;
	int n;
	while(cin >> n){
		string s;
		rep(i, n) cin >> a[i];
		int i = 0, j = n-1, la = 0;
		while(i <= j){
			if(la < a[i] && (a[i]<=a[j] || la>a[j])){
				s.push_back('L');
				la = a[i];
				++i;
			}else if(la < a[j] && (a[j]<a[i] || la>a[i])){
				s.push_back('R');
				la = a[j];
				--j;
			}else break;
		}
		cout << s.size() << endl << s << endl;
	}

	return 0;
}