【刷题】【dp】 Make The Fence Great Again
二维dp
%%%%大佬,最近没啥时间写博客了,以后补上
https://www.cnblogs.com/bianjunting/p/11556876.html
结果最后tle靠快读卡过去了
#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; #define ll long long int n; const int N=3e5+10; ll f[N][3]; inline ll read() { ll x=0;char c=getchar(); while(c< '0' || c> '9' ) c=getchar(); while(c>='0' && c<='9' ) x=(x<<3)+(x<<1)+c-'0',c=getchar(); return x; } int main() { int q;q=read(); while(q--) { n=read(); ll pre,now=read(),v=read(); f[1][1]=v,f[1][2]=(v<<1); for(int i=2;i<=n;i++) { pre=now; now=read(),v=read(); f[i][0]=f[i][1]=f[i][2]=-1; for(int j=0;j<3;j++) for(int k=0;k<3;k++) { if((pre+j) != (now+k) ) if(f[i][k]==-1 ) f[i][k]=f[i-1][j]+v*k; else f[i][k]=min(f[i][k],f[i-1][j]+v*k); } } cout<<min(f[n][0],min(f[n][1],f[n][2] ) )<<endl; } return 0; }