当前位置:网站首页>P6772 [NOI2020] 美食家(矩阵快速幂)
P6772 [NOI2020] 美食家(矩阵快速幂)
2022-06-29 07:13:00 【wind__whisper】
前言
无能狂怒。
见过甚至写过博客的trick,但就是想不起来了。
解析
做法1
设 f t , x f_{t,x} ft,x 表示 t 时刻在 x 的最大价值。
直接转移即可,时间复杂度 O ( T ( n + m ) ) O(T(n+m)) O(T(n+m)),期望得分 40 分。
结合无脑转圈的 A 性质,可得 50 分。
做法2
数据范围把矩阵快速幂写脸上了。
每条边建出对应时间个点转移,时间复杂度 O ( ( 5 m ) 3 k log T ) O((5m)^3k\log T) O((5m)3klogT),期望得分 65-75 分。
做法3
发现点比较少,边比较多,每条边都分裂节点很蠢。
于是改成把每个点分裂成5个,时间复杂度 O ( ( 5 n ) 3 k log T ) O((5n)^3k\log T) O((5n)3klogT)。
期望得分没变,乐。
然后?然后就不会了!
。。。
回家种地吧
正解
预处理出转移矩阵 2 k 2^k 2k 次,节日之间只需要用行向量乘转移矩阵即可。
时间复杂度 O ( ( 5 n ) 3 log T + ( 5 n ) 2 k log T ) O((5n)^3\log T+(5n)^2k\log T) O((5n)3logT+(5n)2klogT),期望得分 100 分。
这个优化这篇博客写过了,然而就是没有想到,乐。
这甚至根本不能叫优化,就是个写法。
乐。
跳出刻板思维!
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ok debug("OK\n")
inline ll read() {
ll x(0),f(1);char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;c=getchar();}
while(isdigit(c)) {
x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
const int N=255;
const int mod=998244353;
const ll inf=-1e18;
bool mem1;
bool Flag=0;
inline ll ksm(ll x,ll k){
ll res(1);
x%=mod;
while(k){
if(k&1) res=res*x%mod;
x=x*x%mod;
k>>=1;
}
return res;
}
int n,m,T,k;
int tot;
int id[N][6];
struct matrix{
int x,y;
ll a[N][N];
matrix(int X=0,int Y=0):x(X),y(Y){
memset(a,-0x3f,sizeof(a));}
}tr,mi[40],res;
matrix operator * (const matrix &u,const matrix &v){
matrix res(u.x,v.y);
assert(u.y==v.x);
for(int k=1;k<=u.y;k++){
for(int i=1;i<=u.x;i++){
ll tmp=u.a[i][k];
for(int j=1;j<=v.y;j++){
res.a[i][j]=max(res.a[i][j],tmp+v.a[k][j]);
}
}
}
return res;
}
int c[N];
struct node{
int t,x,w;
bool operator < (const node &oth)const{
return t<oth.t;}
}o[N];
int num;
bool mem2;
signed main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();m=read();T=read();k=read();
memset(tr.a,-0x3f,sizeof(tr.a));
tr.x=tr.y=n*5;
for(int i=1;i<=n;i++){
for(int j=1;j<=5;j++) id[i][j]=++tot;
for(int j=1;j<=4;j++) tr.a[id[i][j]][id[i][j+1]]=0;
}
for(int i=1;i<=n;i++) c[i]=read();
for(int i=1;i<=m;i++){
int x=read(),y=read(),w=read();
tr.a[id[x][w]][id[y][1]]=c[y];
}
for(int i=1;i<=k;i++){
int t=read(),x=read(),w=read();
o[++num]=(node){
t,x,w};
}
o[++num]=(node){
T,1,0};
sort(o+1,o+1+num);
mi[0]=tr;
for(int i=1;i<=30;i++) mi[i]=mi[i-1]*mi[i-1];
res.x=1;res.y=tot;
memset(res.a,-0x3f,sizeof(res.a));
res.a[1][id[1][1]]=c[1];
int pre(0);
for(int i=1;i<=num;i++){
int d=o[i].t-pre;
for(int k=30;k>=0;k--){
if(d&(1<<k)) res=res*mi[k];
}
res.a[1][id[o[i].x][1]]+=o[i].w;
pre=o[i].t;
}
if(res.a[1][id[1][1]]<0) puts("-1");
else printf("%lld\n",res.a[1][id[1][1]]);
return 0;
}
/* */
边栏推荐
- Reflection - project management thinking of small and medium-sized companies - make the products first and the functions first
- PaddleNLP通用信息抽取模型:UIE【信息抽取{实体关系抽取、中文分词、精准实体标。情感分析等}、文本纠错、问答系统、闲聊机器人、定制训练】
- Segment tree and use
- 马赛克笔记
- SQL Server 2008 publish and subscribe to SQL Server 2017 pit avoidance Guide
- SizeBalanceTree
- C#Mqtt订阅消息
- [eye of depth wuenda machine learning homework class phase IV] regularization regularization summary
- Code:: blocks code formatting shortcuts
- 常见的七大排序
猜你喜欢

【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据

solidity部署和验证代理合约

图文详解JVM中的垃圾回收机制(GC)

自注意力机制超级详解(Self-attention)

在colaboratory上云端使用GPU训练(以YOLOv5举例)

【LoRaWAN节点应用】安信可Ra-08/Ra-08H模组入网LoRaWAN网络的应用及功耗情况

关于SqlSugar的多对多的级联插入的问题(无法获取集合属性的id,导致无法维护中间表)

AC自动机

PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation

What are the constraints in MySQL? (instance verification)
随机推荐
[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables
自注意力机制超级详解(Self-attention)
laravel 中 distinct() 的使用方法与去重
AC自动机
【6G】算力网络技术白皮书整理
Using method and de duplication of distinct() in laravel
SVM, problems encountered in face recognition and Solutions
PHP 7.1.13 版本,在使用过程中发现 浮点类型 数据经过 json_encode 之后会出现精度问题
关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
阿里的211是指什么?
php 清除多维数组里面的空值
C compiler - implicit function declaration
Verilog初体验
Behaviortree in ros2
AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
苹果开发者容易招致调查的若干行为
[Kerberos] analysis of Kerberos authentication
sql语句concat搜索不出结果
MySQL系统关键字汇总(官网)
[repair collection function, update login interface] knowledge payment applet, blog applet, full version open source code, resource realization applet, with 299 whole station resource data