题目链接:https://codeforces.com/contest/1157/problem/C2

首先讲一下题目大意:给你n个数,然后从最左边(L)或者最右边(R)取一个数生成出一个新的序列,对于这个序列的要求是递增的(注意是递增的,不能存在等于的情况)问这个序列有多长。并打印此操作。

这题就是忘了,这个序列不能存在相同的情况,导致wa了几发。

思路:就是采取贪心的策略,贪心的策略是比较这个序列的最左端或最右端,谁小就取谁,当两个相等的情况就看谁的序列更长,如果出现两个序列都一样长,就要比较最后的字母谁大谁小了。用两个下标来控制这个要取得序列位置。当然别忘了,比较的时候你要看生成的新序列中最后一个元素和你要取的数比较,是不是满足加进来的数要比新序列中的数大。所以这题就是情况讨论比较多,思想很简单。下面是我写的代码,不喜勿喷。时间复杂度是O(n)

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
#include <stack>
#include <numeric>
#include <time.h>
#include<iomanip>
#include<sstream>
#pragma disable:4996)
using namespace std;
const long long inf = 0x7f7f7f7f;
long long GCD(long long a, long long b) { return == b ? a : GCD(b, a%b); }
const long long mod = 1e9 + ;
const double pi = acos(-);
int str[];
int main()
{
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = ; i < n; i++)
cin >> str[i];
vector<int>q;
int k = n - ;
int a = -;
for (int i = ; i < n; )
{
if (str[i] < str[k] && a < str[i])
q.push_back(), a = str[i], i++;
else if (str[i] == str[k] && a < str[k])
{
int ans1 = , ans2 = ;
for (int j = i + ; j <= k; j++)
{
if (str[j - ] < str[j])
ans1++;
else
break;
}
for (int j = k - ; j >= i; j--)
if (str[j + ] < str[j])
ans2++;
else
break;
if (ans1 < ans2)
{
a = str[k - ans2];
for (int j = ; j <= ans2; j++)
q.push_back(), k--;
}
else if (ans1 == ans2)
{
if (str[i + ans1] > str[k - ans2])
{
a = str[k - ans2];
for (int j = ; j <= ans2; j++)
q.push_back(), k--;
}
else
{
a = str[i + ans1];
for (int j = ; j <= ans1; j++)
q.push_back(), i++;
}
}
else
{
a = str[i + ans1];
for (int j = ; j <= ans1; j++)
q.push_back(), i++;
}
}
else if (str[i] > str[k] && a < str[k])
q.push_back(), k--, a = str[k + ];
else
{
if (a < str[i])
q.push_back(), a=str[i],i++;
else if (a < str[k])
q.push_back(),a=str[k], k--;
else
break;
}
if (i > k)
break;
}
cout << q.size() << endl;
for (int i = ; i < q.size(); i++)
if (q[i] == )
cout << "R";
else
cout << "L";
}

Increasing Subsequence (hard version)的更多相关文章

  1. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  2. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version) (贪心)

    题意:给你一组数,每次可以选队首或队尾的数放入栈中,栈中元素必须保持严格单增,问栈中最多能有多少元素,并输出选择情况. 题解:首先考虑队首和队尾元素不相等的情况,如果两个数都大于栈顶元素,那么我们选小 ...

  3. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  6. LintCode-Longest Increasing Subsequence

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  9. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

随机推荐

  1. 什么是python的全局解释锁(GIL)

    GIL解决了Python中的什么问题? 为什么选取GIL作为解决方案? 对多线程Python程序的影响 为什么GIL还没有被删除? 为什么在Python 3 中GIL没有被移除? 如何处理Python ...

  2. 深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)

    1. rnn.BasicLSTMCell(num_hidden) #  构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, ...

  3. ajax单删

    通过点击事件获取相应数据的id,将id传到的控制器,根据id执行删除的sql语句,执行官删除:同时ajax的有点就是快捷方便,无刷新,增强用户的体验. <?php namespace app\i ...

  4. 面试小结之Elasticsearch篇(转)

    最近面试一些公司,被问到的关于Elasticsearch和搜索引擎相关的问题,以及自己总结的回答. Elasticsearch是如何实现Master选举的? Elasticsearch的选主是ZenD ...

  5. alert()、confirm()、prompt()的区别

    使用消息框 使用警告.提示和确认 可以使用警告.确认和提示消息框来获得用户的输入.这些消息框是 window 对象的接口方法.由于 window 对象位于对象层次的顶层,因此实际应用中不必使用这些消息 ...

  6. jquery.validate和jquery.form配合实现验证表单后AJAX提交

    基础代码其实很简单,之后一点一点扩充.最终代码写在最后. 表单: <form action="@Url.Action("AddColumns","Cont ...

  7. JVM系列2:垃圾收集器与内存分配策略

    垃圾收集是一个很大话题,本文也只是看了深入理解Java虚拟机总结了下垃圾收集的知识. 首先按照惯例,先上思维导图: 垃圾收集简而言之就是JVM帮我们清理掉内存区域不需要的数据.它主要负责清理堆中实例对 ...

  8. Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况

    chkconfig命令是用于RedHat/Fedora发行版的,而对于像Ubuntu之类的Debian发行版,应该使用这个命令: sudo update-rc.d mysql defaults 验证一 ...

  9. python获取文件夹的大小(即取出所有文件计算大小)

    import os path = r'/Users/authurchen/PycharmProjects/Demo' # print(os.listdir(path)) ls = os.listdir ...

  10. Linux进程的原理及与信号的联系

    第1节 程序.进程.守护进程.僵尸进程的区别 程序.进程.守护进程.僵尸进程: 程序:c/php/java,代码文件,静态的,放在磁盘里的数据. 进程:正在内存中运行的程序,进程是动态的,会申请和使用 ...