矩阵快速幂 CodeForces - 582B Once Again...
2015-10-05 20:34
369 查看
建出转移矩阵...然后做矩阵快速幂就行了....
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 105; const int INF = 0x3f3f3f3f; int mat[maxn][maxn]; int res[maxn][maxn]; int mid[maxn][maxn]; int a[maxn]; int n, m; void calc(int b) { memset(res, 0, sizeof res); while(b) { if(b % 2) { for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { mid[i][j] = -INF; for(int k = 1; k <= n; k++) mid[i][j] = max(mid[i][j], res[i][k] + mat[k][j]); } memcpy(res, mid, sizeof mid); } for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { mid[i][j] = -INF; for(int k = 1; k <= n; k++) mid[i][j] = max(mid[i][j], mat[i][k] + mat[k][j]); } memcpy(mat, mid, sizeof mid); b /= 2; } } void work() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) mat[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { if(a[i] <= a[j]) { mat[i][j] = 1; for(int k = 1; k < j; k++) if(a[k] <= a[j]) mat[i][j] = max(mat[i][j], mat[i][k] + 1); } else mat[i][j] = -INF; } calc(m); int ans = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) ans = max(ans, res[i][j]); printf("%d\n", ans); } int main() { //freopen("data", "r", stdin); work(); return 0; }
相关文章推荐
- codeforces 583B B. Once Again...(dp)
- HDU 4810 Wall Painting(组合数学)
- LightOJ 1282 Leading and Trailing
- Codeforces Round #323 (Div. 1) B. Once Again... 最长非严格递增子序列
- 搭建hibernate时 'hibernate.dialect' must be set when no Connection available错误
- testlink和maints的集成
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
- maints使用
- codeforces 583 D. Once Again... (LIS + 贪心)
- [CareerCup] 10.3 Integer not Contain in the File 文件中不包含的数
- 1236 - Pairs Forming LCM (唯一分解定理加组合数学)
- 端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
- Codeforces Round #323 (Div. 1) B. Once Again...
- coreseek windows下服务 FATAL: Tokenizer initialization failure 解决办法
- 嵌套的 CONTAINING_RECORD 宏
- 详细分析CONTAINING_RECORD宏
- ListEntry 链表图解,及解析 InsertTailList & RemoveHeadList&RemoveEntryList函数
- Codeforces Round #323 (Div. 1) B. Once Again... (最长不下降序列_DP)
- leetcode笔记:Factorial Trailing Zeroes
- [Leetcode]Container With Most Water