D. Once Again...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
Copy
4 3 3 1 4 2
output
Copy
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.
链接:http://codeforces.com/contest/583/problem/D
思路: 如果仅仅考虑头尾两个再 + 中间的 肯定是错的了,如果我给出一个 6 3 6 5 3 4 1 2 或者 8 4 7 8 5 6 3 4 1 2类似的数据是这样想就能看出来明显是错的,但是注意到n 特别小,只有100 并且最坏的情况就是上边类似的情况,那么对于上边的情况我只需要暴力他的n/2 倍长度的序列,然后加上剩下的T-n/2 次乘以 出现最多的次数。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N =105;
int a[N*N];
int n,T;
int num[N];
int c[305];
int cnt[305];
int lowbit(int x)
{
return x&(-x);
}
int query(int x)
{
int mx=0;
for(;x>0;x-=lowbit(x))
{
mx=max(mx,c[x]);
}
return mx;
}
void update(int x,int mx)
{
for(;x<=302;x+=lowbit(x))
{
c[x]=max(c[x],mx);
}
}
int main()
{
scanf("%d %d",&n,&T);
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
int ci=min((n+1)/2,T);
for(int i=1;i<=ci;i++)
{
int j=(i-1)*n+1;
int p=1;
while(p<=n)
{
a[j++]=num[p++];
}
}
ll ans=0;
for(int i=1;i<=ci*n;i++)
{
ll mx=1ll*query(a[i]);
ans=max(ans,mx+1);
update(a[i],mx+1);
}
ll mx=0;
for(int i=1;i<=n;i++)
{
cnt[num[i]]++;
mx=max(mx,1ll*cnt[num[i]]);
}
if(T>ci) ans+=(T-ci)*mx;
printf("%lld\n",ans);
return 0;
}
/*
8 5
7 8 5 6 3 4 1 2
*/