当前位置:网站首页>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;
}
/* */
边栏推荐
- 苹果开发者容易招致调查的若干行为
- Protobuf binary file learning and parsing
- What are the organizational structure and job responsibilities of product managers in Internet companies?
- STM32基于HAL库的USART+DMA使用
- 服装产业发展趋势|供应链|智能制造
- 1284_ Implementation analysis of FreeRTOS task priority acquisition
- 友元,静态关键字,静态方法以及对象间的关系
- MySQL statistics by day / week / month / quarter / half year / year
- Behaviortree in ros2
- NLP标注工具:Label Studio实现多用户协作打标
猜你喜欢
![[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables](/img/51/581be1bdfe7cc97193ff68d3ec5d22.png)
[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables

SizeBalanceTree

关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知

Automatic operation and maintenance management platform - construction and daily use of SPuG

Mongodb- connect to the database using the mongo/mongosh command line

语音合成:概述【不等长序列关系建模的生成任务】

手撕二叉搜索树(Binary Search Tree)

Binary search tree

Taro 介绍

【kerberos】kerberos 认证浅析
随机推荐
语音标注工具:Praat
SizeBalanceTree
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
C mqtt subscription message
关于#sql#的问题:创建一个名为View_XB视图,功能是①如果有重名的视图先删除后创建②显示XSB表中本班级的男女生各有多少人,并添加检查约束
Seven common sorts
产品经理应该学习墨刀还是Axure?
阿里的211是指什么?
Un voyage profond d'IA dans Huawei Cloud
php 清除多维数组里面的空值
MySQL system keyword summary (official website)
Binary search tree
Introduction to taro
Reflection perfectionism
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
Improvement direction of programming ability
特征选择:最大信息系数(MIC;Maximal Information Coefficient)【用于衡量两个变量X和Y之间的关联程度,线性或非线性的强度,常用于机器学习的特征选择】
目标跟踪【单目标跟踪(VOT/SOT)、目标检测(detection)、行人重识别(Re-ID)】
Robotframework learning notes: introduction to robot framework and browserlibrary (playwright)
Thread pool operations in cartographer