Increasing Subsequence (hard version)
题目链接: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)的更多相关文章
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】
一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version) (贪心)
题意:给你一组数,每次可以选队首或队尾的数放入栈中,栈中元素必须保持严格单增,问栈中最多能有多少元素,并输出选择情况. 题解:首先考虑队首和队尾元素不相等的情况,如果两个数都大于栈顶元素,那么我们选小 ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LintCode-Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
随机推荐
- stm32 HAL库笔记(一)——普通IO口
今天介HAL库操作普通IO口,就是输入/输出. 如果用CubeMX配置io工程,打开以后可以看到如下代码: GPIO_InitTypeDef GPIO_Initure; __HAL_RCC_GPIOB ...
- leetcode1
public class Solution { public int[] TwoSum(int[] nums, int target) { ]; ; i < nums.Length; i++) ...
- Java Day26进程01天
Java开启多个线程有两种方法,一种继承Thread类,一种实现Runnable接口.具体示例如下: 01继承Thread类 02实现Runnable接口
- jQuery formValidator API
jQuery formValidator插件的API帮助 目前支持5种大的校验方式,分别是:inputValidator(针对input.textarea.select控件的字符长度.值范围.选择个数 ...
- Unity3D AssetBundle相关
Unity3D AssetBundle相关 首先,先看一下原理吧 Unity3D研究院之Assetbundle的原理(六十一) 其次,接着往下看:Unity3D研究院之Assetbundle的实战( ...
- ajax请求导致status为canceled(无任何回调数据)的原因
1.故障现象 一个普通的ajax请求,请求能够到达controller,也能正常处理业务,但是ajax的回调函数为空,即没有任何状态和数据返回,使用谷歌浏览器查看请求状态如下图: 出现该错误:简单来说 ...
- css之标签选择器
标签(空格分隔): 标签选择器 选择器定义: 在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式,某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素 ...
- Windows驱动开发调试工具
[开发工具] VS2012 [调试工具] Windbg:和VM配合实现双机联合调试,完成双机调试功能,可以结合<软件调试>这本书对Windbg有较为深入的认识. DebugView: 可以 ...
- 20162322 朱娅霖 作业011 Hash
20162322 2017-2018-1 <程序设计与数据结构>第十一周学习总结 教材学习内容总结 哈希方法 一.定义 哈希:次序--更具体来说是项在集合中的位置--由所保存元素值的某个函 ...
- 深入JVM之类的加载器
类加载器有两种: —java虚拟机的自带加载器 根类加载器(Bootstrap) 扩展类加载器(Extension) 系统类加载器(AppClassLoder) —自定义的类加载器 java.lang ...