hdu3698 Let the light guide us dp+線段樹最佳化,hdu3698dp
http://acm.hdu.edu.cn/showproblem.php?pid=3698
Let the light guide us
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 821 Accepted Submission(s): 285
Problem DescriptionPlain of despair was once an ancient battlefield where those brave spirits had rested in peace for thousands of years. Actually no one dare step into this sacred land until the rumor that “there is a huge gold mine underneath the plain” started to spread.
Recently an accident destroyed the eternal tranquility. Some greedy fools tried using powerful bombs to find the hidden treasure. Of course they failed and such behavior enraged those spirits--the consequence is that all the human villages nearby are haunted by ghosts.
In order to stop those ghosts as soon as possible, Panda the Archmage and Facer the great architect figure out a nice plan. Since the plain can be represented as grids of N rows and M columns, the plan is that we choose ONLY ONE cell in EACH ROW to build a magic tower so that each tower can use holy light to protect the entire ROW, and finally the whole plain can be covered and all spirits can rest in peace again. It will cost different time to build up a magic tower in different cells. The target is to minimize the total time of building all N towers, one in each row.
“Ah, we might have some difficulties.” said Panda, “In order to control the towers correctly, we must guarantee that every two towers in two consecutive rows share a common magic area.”
“What?”
“Specifically, if we build a tower in cell (i,j) and another tower in cell (i+1,k), then we shall have |j-k|≤f(i,j)+f(i+1,k). Here, f(i,j) means the scale of magic flow in cell (i,j).”
“How?”
“Ur, I forgot that you cannot sense the magic power. Here is a map which shows the scale of magic flows in each cell. And remember that the constraint holds for every two consecutive rows.”
“Understood.”
“Excellent! Let’s get started!”
Would you mind helping them?
InputThere are multiple test cases.
Each test case starts with a line containing 2 integers N and M (2<=N<=100,1<=M<=5000), representing that the plain consists N rows and M columns.
The following N lines contain M integers each, forming a matrix T of N×M. The j-th element in row i (Tij) represents the time cost of building a magic tower in cell (i, j). (0<=Tij<=100000)
The following N lines contain M integers each, forming a matrix F of N×M. The j-th element in row i (Fij) represents the scale of magic flows in cell (i, j). (0<=Fij<=100000)
For each test case, there is always a solution satisfying the constraints.
The input ends with a test case of N=0 and M=0.
OutputFor each test case, output a line with a single integer, which is the minimum time cost to finish all magic towers.
Sample Input
3 59 5 3 8 78 2 6 8 91 9 7 8 60 1 0 1 21 0 2 1 10 2 1 0 20 0
Sample Output
10
Source2010 Asia Fuzhou Regional Contest
題意:就是每行選一個,上下兩行需滿足|j-k|≤f(i,j)+f(i+1,k).,問最小的cell和值。
分析:明顯的dp,dp[i][j]表示到第i行選第j個的值,但是這樣轉移複雜度需要n*m*m,肯定會逾時。
我們注意到|j-k|<=f(i,j)+f(i-1,k),那麼對於i-1行的第k個我們更新[k-f(i-1,k),k+f(i-1,k)],對於第i行查詢[j-f(i,j),j+f(i,j)],這樣剛好滿足的是要求的條件。
所以就用線段樹維護一下查詢區間最小值更新區間值就好。這樣複雜度就是n*m*log(m)。。
/** * @author neko01 *///#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <set>#include <map>using namespace std;typedef long long LL;#define min3(a,b,c) min(a,min(b,c))#define max3(a,b,c) max(a,max(b,c))#define pb push_back#define mp(a,b) make_pair(a,b)#define clr(a) memset(a,0,sizeof a)#define clr1(a) memset(a,-1,sizeof a)#define dbg(a) printf("%d\n",a)typedef pair<int,int> pp;const double eps=1e-8;const double pi=acos(-1.0);const int INF=0x7fffffff;const LL inf=(((LL)1)<<61)+5;const int N=105;const int M=5005;int a[N][M];int f[N][M];int dp[N][M];struct node{ int l,r; int Min; int col;}tree[M*4];void build(int x,int l,int r){ tree[x].l=l,tree[x].r=r; tree[x].Min=INF; tree[x].col=INF; if(l==r) return; int mid=(l+r)>>1; build(x<<1,l,mid); build(x<<1|1,mid+1,r);}inline void push_down(int x){ if(tree[x].col!=INF) { tree[x<<1].col=min(tree[x].col,tree[x<<1].col); tree[x<<1|1].col=min(tree[x<<1|1].col,tree[x].col); tree[x<<1].Min=min(tree[x].col,tree[x<<1].Min); tree[x<<1|1].Min=min(tree[x].col,tree[x<<1|1].Min); tree[x].col=INF; }}void update(int x,int l,int r,int val){ if(tree[x].l==l&&tree[x].r==r) { tree[x].Min=min(tree[x].Min,val); tree[x].col=min(tree[x].col,val); return; } push_down(x); int mid=(tree[x].l+tree[x].r)>>1; if(r<=mid) update(x<<1,l,r,val); else if(l>mid) update(x<<1|1,l,r,val); else { update(x<<1,l,mid,val); update(x<<1|1,mid+1,r,val); } tree[x].Min=min(tree[x<<1].Min,tree[x<<1|1].Min);}int query(int x,int l,int r){ if(tree[x].l==l&&tree[x].r==r) return tree[x].Min; push_down(x); int mid=(tree[x].l+tree[x].r)>>1; if(r<=mid) return query(x<<1,l,r); else if(l>mid) return query(x<<1|1,l,r); else return min(query(x<<1,l,mid),query(x<<1|1,mid+1,r));}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); if(i==1) dp[1][j]=a[i][j]; } } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&f[i][j]); for(int i=2;i<=n;i++) { build(1,1,m); for(int j=1;j<=m;j++) { int l=max(1,j-f[i-1][j]); int r=min(m,j+f[i-1][j]); update(1,l,r,dp[i-1][j]); } for(int j=1;j<=m;j++) { int l=max(1,j-f[i][j]); int r=min(m,j+f[i][j]); dp[i][j]=query(1,l,r)+a[i][j]; } } int ans=INF; for(int i=1;i<=m;i++) ans=min(ans,dp[n][i]); printf("%d\n",ans); } return 0;}
Let The Light (Shine On Me) 歌詞
歌曲名:Let The Light (Shine On Me)
歌手:triumph
專輯:Surveillance
I've walked beside you through a thousand dreams
Sometimes I wonder where the journey leads
We never seem to get to where we want to go...no...
I won't betray the trust you have in me
I'll never lose my faith in what can be
Don't ever be afraid to let your feelings show...no...
Triumph
Nothing to fear, no place to hide
We got to face the truth inside
CHORUS:
Let the light that shines in your eyes shine on me
Let it shine forever
Let the light shine on me
We can build a dream together now
One in a million that's a chance I'll take
Cause when the moment comes I'll take my chance with you
And when it's more than you can bear
You call my name and I'll be there
CHORUS
So hard to find your way out of the dark
Let the light shine from your heart...
CHORUS
music.baidu.com/song/14209057
八年級英語題1、___(one),you need to get ready2、Let us__(light)the candleLet'stoo dark
你好!
正確答案為:
1 First
首先,你需要做好準備
2 light
讓我們點亮蠟燭吧,太黑了。
3 heavier
空氣比我們想像得要重
4 doesn't rain
如果明天不下雨,明天我就去公園
5 interesting
科學非常有趣
6 surprised me
surprise sb 讓某人吃驚
7 Bottom up
botoom up 翻過來,固定短語搭配
8 making preparations
做準備
9 Don't do the experiment, please.
祈使句否定,在最前面加don't
10 What's that called?
11 My stomach is empty.
full的對應詞是empt,空的
12 Why was Jim late?
對原因提問用why
^___^
祝你進步!
另外千萬別忘記採納喲!