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 ...
随机推荐
- 纯Java——简易高并发框架
转自:https://blog.csdn.net/MonkeyDCoding/article/details/81369610 0.源代码github-简易高并发框架 注:本篇博客知识来自于网课. 1 ...
- shell多个脚本怎么并发执行
#!bin/bash PRONUM=10 #进程个数 tmpfile="$$.fifo" #临时生成管道文件 mkfifo $tm ...
- Linux网络编程学习(六) ----- 管道(第四章)
1.管道的定义 管道就是将一个程序的输出和另外一个程序的输入连接起来的单向通道,比如命令: ls -l|more,就建立了一个管道,获取ls -l的输出作为more的输入,数据就沿着管道从管道的左边流 ...
- 35.Spring-jdbc支持.md
目录 1.JdbcTemplate类 1.1导入jar包 1.2创建Dao对象 1.3将上述例子封装后 2. 3. 1.JdbcTemplate类 传统的jdbc开始,需要对Connection.St ...
- html css col-md-offset
有的时候,我们不想让两个相邻的列挨在一起,这时候利用栅格系统的列偏移(offset)功能来实现,而不必再定义margin值.使用.col-md-offset-*形式的样式就可以将列偏移到右侧.例如,. ...
- CSS 图像居中对齐
CSS 图像居中对齐 我们在<CSS 内外边距>学过内容居中,它的原理是将外边左右设置为auto.图像居中也是这个原理. 示例 <!DOCTYPE html> <htm ...
- vue单页面处理SEO问题
设置vue 单页面meta info信息 vue-meta-info,(https://github.com/muwoo/vue-meta-info)如果需要单页面SEO,可以和 prerender- ...
- linux百万并发之 tcp_mem
百万并发之 tcp_mem 在服务端,连接达到一定数量,诸如50W时,有些隐藏很深的问题,就不断的抛出来. 通过查看dmesg命令查看,发现大量TCP: too many of orphaned ...
- df -i 100%时处理方法
晚上正在看文档的时候,运营突然打电话给我说游戏全炸了,都玩不了了: 我立马登上服务器,查看了下各服务是否正常运行,结果运行命令都报Unable to create lock file: No spa ...
- Zabbix告警脚本-邮件
[root@iot-svndata02 bin]# cat zbmail.sh #!/bin/bash to_email_address="$1" # 收件人Email地址,zab ...