• 刷题记录:牛客NC16850[NOI1998]免费馅饼


    传送门:牛客

    题目描述:

    游戏开始后,从舞台天幕顶端的格子中不断出现馅饼并垂直下落。游戏者左右移动去接馅饼。游戏者每秒
    可以向左或向右移动一格或两格,也可以站在原地不动。
    馅饼有很多种,游戏者事先根据自己的口味,对各种馅饼依次打了分。同时,在8-308电脑的遥控下,各种
    馅饼下落的速度也是不一样的,下落速度以格/秒为单位。
    当馅饼在某一秒末恰好到达游戏者所在的格子中,游戏者就收集到了这块馅饼。
    写一个程序,帮助我们的游戏者收集馅饼,使得所收集馅饼的分数之和最大
    输入:
    3 3
    0 1 2 5 
    0 2 1 3
    1 2 1 3
    1 3 1 4
    输出:
    12
    -1
    1
    1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    emmm,其实这一道并不是NOI的那道题,这道题是那道题的简化版感兴趣的可以去这里,之后我应该也会做一做那道题并且上线我的博客

    主要思路:

    1. 首先对于这道题我们应该有一个显然的dp思想,我们可以使用f[i][j]来表示前i分钟我们在j位置所获得的最大值.
    2. 但是当我们产生这个想法之后我们会发现这种比较难以实现,因为最后的最优位置我们是不知道的,这就意味着我们不好记录我们的最优路径.但是我们发现我们的起点其实是固定的,这样的话我们是不是可以倒推的解决这道题??,我们可以从最大的落下时间开始,然后转移前一段时间的状态即可,并且这一段区间是有[-2,2]这一段区间的.emmm,似乎讲不清楚

    dp[ i ][ j ]=max(dp[ i ][ j ],dp[ i+1 ][ j+k ]+a[ i ][ j ]) k ∈ \in [-2,2]

    不懂上面我的解释的话,可以直接看这个转移方程,感觉还是挺好懂的.不断的转移即可,就是将之前有数据的位置向左右进行扩展即可,当然在扩展的同时我们还需要记录,每一次改变就记录一下 K K K即可.

    对于我们最后的路径我们可以直接输出每一段时间的值即可,然后对于我们的位置(我们刚开始显然是中间点),后来的话就可以使用我们之前的那个 K K K进行移动即可

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    typedef long long ll;
    #define inf 0x3f3f3f3f
    #define root 1,n,1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    inline ll read() {
    	ll x=0,w=1;char ch=getchar();
    	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
    	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    	return x*w;
    }
    #define maxn 1000000
    #define ll_maxn 0x3f3f3f3f3f3f3f3f
    const double eps=1e-8;
    int w,h;
    int a[2000][2000];
    int dp[2000][2000];
    int path[2000][2000];
    int main() {
    	w=read();h=read();
    	h-=1;int max_t=-inf;
    	int xialuo_t,pos,xialuo_v,ww;                     
    	while(scanf("%d%d%d%d",&xialuo_t,&pos,&xialuo_v,&ww)!=EOF) {
    		if(h%xialuo_v!=0) continue;
    		a[xialuo_t+h/xialuo_v][pos]+=ww;
    		max_t=max(max_t,xialuo_t+h/xialuo_v);
    	}
    	for(int i=max_t;i>=0;i--) {
    		for(int j=1;j<=w;j++) {
    			for(int k=-2;k<=2;k++) {
    				if(j+k<1||j+k>w) continue;
    				if(dp[i][j]<dp[i+1][j+k]+a[i][j]) {
    					dp[i][j]=dp[i+1][j+k]+a[i][j];
    					path[i][j]=k;
    				}
    			}
    		}
    	}
    	printf("%d\n",dp[0][(w>>1)+1]);
    	int state=(w>>1)+1;
    	for(int i=0;i<max_t;i++) {
    		printf("%d\n",path[i][state]);
    		state+=path[i][state];
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
  • 相关阅读:
    Linux多线程C++版 线程基础 进程和线程关系 线程分类 Linux线程实现 线程表示
    Linux指令——crontab
    [洛谷月赛]终于结束的起点
    Netty 介绍
    【技术积累】Mysql中的SQL语言【技术篇】【二】
    记录--CSS 滚动驱动动画 scroll()
    LeetCode 0828. 统计子串中的唯一字符
    支持语音与视频即时通讯项目杂记(一)
    【python】OpenCV GUI——Trackbar(14.2)
    视频编码-码率控制CQP/CRF/ABR/CBR/VBV
  • 原文地址:https://blog.csdn.net/yingjiayu12/article/details/127579966