Codeforces Round #323 (Div. 2) D LIS 最長上升子序列
連結:戳這裡
D. Once Again...
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array.Input
The first line contains two space-separated integers: n, T (1 ≤ n ≤ 100, 1 ≤ T ≤ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 300).
Output
Print a single number — the length of a sought sequence.
Examples
input
4 3
3 1 4 2
output
5
Note
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
題意:
給你長度為n的序列,然後再複製加長T倍,問最長上升子序列
思路:
給出的長度為n的子序列求LIS肯定很好算,那麼我們分析一下,什麼情況下會答案很大呢
如果給你的n個數是唯一的,(n=100)那麼我算最長的也就是n*n了,為什麼?
你能拿到的最長的數也就到第n*n,因為假設你每個n塊裡面取一個出來(至少有一個滿足答案)
也就是n*n個,所以我們求到n*n也才10000,那麼多出來的(T-n)怎麼辦
直接拿出現次數最多的那個數就行了
程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int s[1000100];
int LIS(int *a,int n){
int len=0;
s[++len]=a[1];
for(int i=2;i<=n;i++){
if(a[i]>=s[len]) s[++len]=a[i];
else {
int x=upper_bound(s+1,s+len+1,a[i])-s;
/// s中>a[i]的第一個元素。
s[x]=a[i];
}
}
return len;
}
int n,T;
int a[1000100],b[1000100],vis[1000100];
int main(){
scanf("%d%d",&n,&T);
int num=0;
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
vis[a[i]]++;
num=max(vis[a[i]],num);
}
int cnt=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
b[++cnt]=a[j];
}
}
ll ans=0;
if(T<=n){
ans=LIS(b,n*T);
} else {
ans=LIS(b,n*n)+(ll)num*(T-n);
}
cout<<ans<<endl;
return 0;
}
相關文章
- 動態規劃7:最長上升子序列LIS動態規劃
- 最長上升子序列(LIS)的兩種演算法演算法
- 最長上升子序列LIS 詳解+變形+擴充
- Codeforces Round #323 (Div. 2) C gcdGC
- 最長上升子序列
- Codeforces Round #323 (Div. 2) B 模擬
- Codeforces Round #323 (Div. 2) (ABCD題解)
- Codeforces Round #452 (Div. 2) D
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- 【部分轉載】:【lower_bound、upperbound講解、二分查詢、最長上升子序列(LIS)、最長下降子序列模版】
- DP筆記最長上升子序列(LIS)以及零件分組問題筆記
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- 線性dp:最長上升子序列
- NlogN 求最長不下降子序列(LIS)
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- 動態規劃:最長上升子序列動態規劃
- 最長上升子序列動態規劃動態規劃
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- 動態規劃-最長上升子序列模型動態規劃模型
- 【數論】Codeforces Round #404 (Div. 2)(D)Anton and School - 2