【倍增】T-shirt @2018acm徐州邀请赛 I
问题 I: T-shirt
时间限制: 1 Sec 内存限制: 64 MB
题目描述
His vacation has N days. Each day, he can choose a T-shirt to wear. Obviously, he doesn’t want to wear a singer color T-shirt since others will consider he has worn one T-shirt all the time.
To avoid this problem, he has M different T-shirt with different color. If he wears A color T-shirt this day and B color T-shirt the next day, then he will get the pleasure of f[A][B].(notice: He is able to wear one T-shirt in two continuous days but may get a low pleasure)
Please calculate the max pleasure he can get.
输入
- The first line of the input contains two integers N,M (2 ≤ N≤ 100000, 1 ≤ M≤ 100), giving the length of vacation and the T-shirts that JSZKC has.
- The next follows M lines with each line M integers. The jth integer in the ith line means f[i][j](1<=f[i][j]<=1000000).
There are no more than 10 test cases.
输出
样例输入
3 2
0 1
1 0
4 3
1 2 3
1 2 3
1 2 3
样例输出
2
9
meaning
给一个m×m的矩阵,data[i][j]表示第一天在i点第二天在j点的收益,问n天的最大收益。
solution
f[k][i][j] 表示第一天在i点,2^k天后在j点的最大收益。
f[0][i][j] = data[i][j];
f[k][i][j] = max(f[k][i][j],f[k-1][i][p]+f[k-1][p][j]);
d[i][j] 表示从i点到j点的最大收益。
code
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxm = 105;
ll f[20][maxm][maxm];
ll d[2][maxm][maxm];
int main() {
// IN_LB();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF) {
memset(f,0,sizeof f);
memset(d,0,sizeof d);
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
scanf("%lld",&f[0][i][j]);
}
}
for(int k=1; k<20; k++)
for(int l=0; l<m; l++)
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
f[k][i][j] = max(f[k][i][j],f[k-1][i][l]+f[k-1][l][j]);
n--;
int cnt = 0;
for(int k=19; k>=0; k--) {
if(n>(1<<k)) {
n-=(1<<k);
for(int l = 0; l<m; l++) {
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
d[cnt][i][j]=max(d[cnt][i][j],d[cnt^1][i][l]+f[k][l][j]);
}
}
}
cnt^=1;
}
}
if(n==1) {
for(int l = 0; l<m; l++) {
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
d[cnt][i][j]=max(d[cnt][i][j],d[cnt^1][i][l]+f[0][l][j]);
}
}
}
}
ll ans = 0;
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
ans = max(ans,d[cnt][i][j]);
}
}
printf("%lld\n",ans);
}
return 0;
}
【倍增】T-shirt @2018acm徐州邀请赛 I的更多相关文章
- 【容斥+组合数】Massage @2018acm徐州邀请赛 E
问题 E: Massage 时间限制: 1 Sec 内存限制: 64 MB 题目描述 JSZKC feels so bored in the classroom that he w ...
- 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G
问题 G: JS Window 时间限制: 2 Sec 内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...
- 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题
题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...
- 树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题
题目描述 有一棵树,每个结点有一个灯(初始均是关着的).每个灯能对该位置和相邻结点贡献1的亮度.现有两种操作: (1)将一条链上的灯状态翻转,开变关.关变开: (2)查询一个结点的亮度. 数据规模:\ ...
- 2018 ICPC 徐州邀请赛 总结
Day 0 上午在高铁里面,很困但是睡不着…… 中午到矿大报道. 食堂饭菜不错,有西瓜,就是有点辣. 下午热身赛,D题队友想了个假算法……GG. 评测机摸底考试正常进行. 热身赛之后精疲力尽,赶到宾馆 ...
- 徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂
题目 题目描述 JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-s ...
- 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板
题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...
- 台州学院maximum cow训练记录
前队名太过晦气,故启用最大牛 我们的组队大概就是18年初,组队阵容是17级生詹志龙.陶源和16级的黄睿博. 三人大学前均无接触过此类竞赛,队伍十分年轻.我可能是我们队最菜的,我只是知道的内容最多,靠我 ...
- acm一路走来的体验和想法
2017年参加天梯赛 作为大一萌新去参加人生第一场正规的比赛,学校拿钱砸的.我呢,是十分激动的,是时候检验下自己的编程水平了,也有一丝慌张,怕给团队抹黑. 然后呢,晒一下自己成绩. 我本来觉得我和他们 ...
随机推荐
- 【BZOJ2298】[HAOI2011]problem a
题解: 虽然也是个可以过得做法...但又没有挖掘到最简单的做法... 正解是发现这个东西等价于求不相交区间个数 直接按照右端点排序,然后贪心就可以O(n)过了 而我的做法是按照a排序(其实我是在模拟这 ...
- 基于nopcommerce b2c开源项目的精简版开发框架Nop.Framework
http://www.17ky.net/soft/70612.html?v=1#0-sqq-1-39009-9737f6f9e09dfaf5d3fd14d775bfee85 项目详细介绍 该开源项目是 ...
- Js计算时间差,天数,小时数,余数
var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); //begintime 为开始时间 v ...
- JMeter中BeanShell的实际应用
使用Jmeter的BeanShell断言,把响应数据中的JSON跟数据库中的记录对比 很多时候我们需要把Response Data取到的 Json 字符串跟数据库里的对比,来验证接口的正确性,使用Be ...
- Centos7创建CA和申请证书
转载:http://rackie386.blog.51cto.com/11279229/1947999 Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/op ...
- JVM及class文件加载问题-学习使人快乐4
今天看了些粗浅的JVM原理的知识 1.class文件编译过程: 词法分析 语法分析 源码 ---------Token流-------------语法树----------字节码 2.classloa ...
- BZOJ3262/洛谷P3810 陌上花开 分治 三维偏序 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8672131.html 题目传送门 - BZOJ3262 题目传送门 - 洛谷P3810 题意 有$n$个元素,第 ...
- P2279 [HNOI2003]消防局的设立 贪心or树形dp
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- Oracle中start with...connect by子句的用法
http://www.blogjava.net/xzclog/archive/2010/03/05/314642.html
- hive提前过滤重要性
hive提前过滤 create table sospdm.tmp_yinfei_test_01 ( id string ) partitioned by (statis_date string) ; ...