Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

题目链接:http://codeforces.com/contest/712/problem/D

A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A比B大的情况有多少种。

dpA[i][j]表示第i轮获得j分的情况数。因为第i轮只和第i-1轮有关,所以这里i用滚动数组优化。

要是普通做法3个for就会超时,所以要用前缀和优化,dpA[i][j]可以由一段连续的dp[i - 1][x]转移过来,所以用sumA数组存取dp[i - 1][x]的前缀和。就可以省去一个for。

B同理。

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 2e5 + 205;
17 LL dp[2][N], mod = 1e9 + 7, sum[N], dpA[2][N], sumA[N];
18
19 int main()
20 {
21     int a, b, k, t;
22     scanf("%d %d %d %d", &a, &b, &k, &t);
23     a += 1e5, b += 1e5;
24     dp[0][b] = 1;
25     dpA[0][a] = 1;
26     sum[b] = 1, sumA[a] = 1;
27     for(int i = 1; i <= t; ++i) {
28         for(int j = 1; j < N; ++j) {
29             dp[i%2][j] = dpA[i%2][j] = 0;
30         }
31         for(int j = -k*i; j <= k*i; ++j) {
32             int l = max(-k*(i - 1), j - k), r = min(k*(i - 1), j + k);
33             dp[i % 2][b + j] = ((sum[r + b] - sum[l - 1 + b] + dp[i % 2][b + j]) % mod + mod) % mod;
34             dpA[i % 2][a + j] = ((sumA[r + a] - sumA[l - 1 + a] + dpA[i % 2][a + j]) % mod + mod) % mod;
35         }
36         for(int j = 1; j < N; ++j) { //在每轮中sum都会重新更新
37             sum[j] = (sum[j - 1] + dp[i % 2][j]) % mod;
38             sumA[j] = (sumA[j - 1] + dpA[i % 2][j]) % mod;
39         }
40     }
41     LL ans = 0;
42     for(int i = 1; i < N; ++i) {
43         ans = (ans + sum[i - 1]*dpA[t % 2][i] % mod) % mod;
44     }
45     printf("%lld\n", ans);
46     return 0;
47 }
时间: 2024-12-17 07:09:43

Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)的相关文章

P5241 序列(滚动数组+前缀和优化dp)

P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$i$条边 这显然是不够的.于是我们又想:强连通分量.....连通块....... 于是加个$j$表示还有$j$个强连通分量 于是dp数组为$f[i][j]$ 这是我们发现一个问题,状态$f[i][j]$不一定是合法的. 那dp不就GG了吗 再次撕烤,我们发现每次加上的边无非就3种情况: 1.把2个强

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

【最大M子段和】dp + 滚动数组

题目描述 给定 n 个数求这 n 个数划分成互不相交的 m 段的最大 m 子段和. 给出一段整数序列 A1,A2,A3,A4,...,Ax,...,An ,其中 1≤x≤n≤1,000,000, -32768≤Sx≤32767. 我们定义一种函数 sum(i,j)=Ai + ... + Aj (1≤i≤j≤n,且Ai-Aj 是连续的数). 现在,我们得到一个正整数 m(1≤x≤m≤30),你的工作是寻找 m 对 i 与 j.这 m 对 i 和 j 满足以下条件:  sum(i1,j1)+sum(

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma