Codeforces 582 B Once Again... (LIS)
題目連結:傳送門
題意:
給定一個長度為n(1<=n<=100)的序列求將這個序列複製T次的LIS的長度。
分析:
方法一:
設f(i,j)表示在迴圈節下標i開頭j結尾的最長不減子序列,則f3(i,j)=max{f1(i,k)+f2(k,j)},因此可以用矩陣進行加速
程式碼如下:
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
int n;
struct matrix {
int a[maxn][maxn];
matrix() {
memset(a,0,sizeof(a));
}
matrix operator * (const matrix &tmp)const {
matrix c;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
c.a[i][j]=-inf;
for(int k=0; k<n; k++) {
c.a[i][j]=max(c.a[i][j],a[i][k]+tmp.a[k][j]);
}
}
}
return c;
}
matrix operator ^(int b) {
matrix ans;
matrix tmp = *this;
while(b) {
if(b&1)
ans=ans*tmp;
b>>=1;
tmp = tmp*tmp;
}
return ans;
}
};
void debug(matrix A) {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++)
cout<<A.a[i][j]<<" ";
cout<<endl;
}
}
int a[maxn];
int main() {
int t;
while(~scanf("%d%d",&n,&t)) {
for(int i=0; i<n; i++)
scanf("%d",a+i);
matrix A,ans;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(a[i]>a[j]) {
A.a[i][j]=-inf;
continue;
}
A.a[i][j]=1;
for(int k=0; k<j; k++) {
if(a[k]<=a[j])
A.a[i][j]=max(A.a[i][j],A.a[i][k]+1);
}
}
}
//debug(A);
ans = A^t;
//debug(ans);
int mm = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
mm = max(mm,ans.a[i][j]);
}
}
printf("%d\n",mm);
}
return 0;
}
方法二:
發現肯定有一段是連續的相同的,因此我們先將序列複製n次,求出它的LIS然後再找出原序列中出現的次數最多的數插入在相應的位置。
程式碼如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
int a[maxn];
int p[maxn];
int num[maxn];
int main() {
int n,t;
while(~scanf("%d%d",&n,&t)) {
memset(num,0,sizeof(num));
int tmp = 0;
for(int i=1; i<=n; i++) {
scanf("%d",a+i);
num[a[i]]++;
tmp = max(tmp,num[a[i]]);
}
int N;
if(t>=n)
N=n*n;
else
N=t*n;
for(int j=n+1; j<=N; j++) {
if(j%n)
a[j]=a[j%n];
else
a[j]=a[n];
}
int len=0;
for(int i=1;i<=N;i++){
int pos = upper_bound(p,p+len,a[i])-p;
if(pos==len) p[len++]=a[i];
else p[pos]=a[i];
}
int ans=len;
if(t>n) ans = ans + (t-n)*tmp;
printf("%d\n",ans);
}
return 0;
}
相關文章
- codeforces #442 B
- codeforces_B. Barnicle
- CodeForces - 1363B
- Codeforces Round #323 (Div. 2) D LIS 最長上升子序列
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Codeforces 194B.Square
- Codeforces Round #399 (A,B,C)
- codeforces 498B. Name That Tune
- CodeForces 1132B Discounts
- B/S版區域檢驗管理系統原始碼(雲LIS)原始碼
- Codeforces 264B. Good SequencesGo
- CodeForces 1417B Two Arrays
- Codeforces Round #448 (Div. 2)B
- Codeforces Round #450 (Div. 2) B
- codeforces 101 B. Buses
- Codeforces Round 840題解(A、B、C)
- codeforces round 961題解(A、B、C)
- Day 582:遊戲型組織遊戲
- Codeforces 549B. Looksery Party[構造]
- Codeforces 687B. Remainders Game[剩餘]REMAIGAM
- CodeForces - 282B Painting EggsAI
- CodeForces703B Mishka and trip 數學
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】CodeForces - 623B Array GCDGC
- Codeforces Testing Round #10 B. Balancer
- Codeforces Round #251 (Div. 2) A/B/D
- codeforces 260 div2 A,B,C
- Codeforces Round 949題解(A、B、C、D)
- CF741B:https://codeforces.com/problemset/problem/741/BHTTP
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
- require_once()UI
- HDD is Outdated Technology Codeforces 612B
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces 500B. New Year Permutation[連通性]
- Codeforces 854B Maxim Buys an Apartment:貪心
- Codeforces Round #362 (Div. 2) B 模擬
- Codeforces Round #336 (Div. 2) B 暴力
- Codeforces Round #325 (Div. 2) B 遞推