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 ...
随机推荐
- C# Excel添加超链接
操作当前单元格(关键代码就两行) Range range = (Range)ExSheet.Cells[i + 2, j + 1]; ...
- leetcode238
public class Solution { public int[] ProductExceptSelf(int[] nums) { int[] result = new int[nums.Len ...
- Citrix XenApp登录服务器过程详解
详细流程: 1. 客户端上的receiver负责解析ICA文件,并根据ICA文件的内容发起连接请求.若是外网访问,则ICA文件中记录的是NetScaler的AG FQDN信息,连接请求发至NetSca ...
- 剑指offer例题——二进制中1的个数
题目:输入一个整数,输出该二进制表示中1的个数.其中负数用补码表示. 首先明确补码的定义: 原码 反码 补码 将最高位作为符号位(0表示正,1表示负), 其它数字位表达数值本身的绝对值的数字表示方式 ...
- Linux:sudo,没有找到有效的 sudoers 资源。
首先,这是因为用户的权限不够导致的. 使用 ls -l /etc/passwd 查看所有用户及权限.只有可读权限(r),说明用户的权限不够. 因此,我们可以用以下方法修改用户权限: 1. su roo ...
- Spring再接触 注入类型
共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...
- (vue.js)element ui 表单重置
el-form需要接收一个model,并且需要配合el-form-item一起使用,并且在el-form-item上绑定prop属性,resetField方法才能好使. <el-form :mo ...
- 算法练习LeetCode初级算法之排序和搜索
合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ...
- Springboot学习05-自定义错误页面完整分析
Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1 ...
- 利用travis自动化构建与部署(文档项目)
背景 保持网站上文档的最新性有比较重要的意义, travis ci 提供了免费的解决方案,本文基于 latex 构建+ aliyun oss 部署对此作了尝试. 项目链接为 https://travi ...