CodeForces 407B Long Path (DP)】的更多相关文章

题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[i]表示从1走到i需要多少步,那么走到房间i+1需要dp[i+1]=dp[i]+1+x+1,这里面第一个1表示走完这步退回到Pi,这个x表示退回到房间Pi再走回来的步数,第二个1表示走完这步到达i+1.我们发现:1.当走到房间Pi的时候,Pi+1到i都是没有走过的即为0次:2.当走到房间i的时候,P…
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门:①第一扇门通向i+1,如果当前房间标记有奇数个,则必须走第一扇门.②第二扇门通向pi(pi<=i),如果当前房间标记有偶数个,则必须走第二扇门.问从房间1走到房间n+1需要多少步,结果对1e9+7取模.解题思路:之前傻了...明明都推出规律了,结果被自己给否定了...设dp[i]表示从1~i需要走…
题目: One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one. The maze is organized as follows. Each…
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列上可以向上走,其他列不能向上走.可以重复经过同一个点.求从(1,1)出发,经过所有宝藏的最短路径长度 \(n,m,k,q \leq 2 \times 10^5\) 分析 贪心考虑,我们应该按照行一层一层的走.每一行应该从最左的宝藏走到最右的宝藏,或者从最右的宝藏走到最左的宝藏,然后找最近的一个可以向…
B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at th…
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define…
题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1557    Accepted Submission(s): 678 Problem Description Frog fell into a maze. This maze is a rectangle containing N rows and M …
Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 673 Problem Description Frog fell into a maze. This maze is a rectangle containing N rows and M column…
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl…
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city,…
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍),问要获得长度为n的串所需最少的时间. 思路:dp[i]表示获得长度为i的串所需要的最短时间,分i为奇数和偶数讨论. #include<bits/stdc++.h> using namespace std; const int N=1e7+3; typedef long long ll; ll…
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; int dp[105][3]; int main() { int n; scanf("%d",&n); memset(dp,INF,sizeof(dp)); d…
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+3; const ll INF=1e18; ll dp[N][2]; string a[N],b[N]; int c[N]; int main() { int n; scanf("%d",&n); for(…
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word in…
http://codeforces.com/contest/408/problem/D 题意:有一排房间每个房间有两扇门,一扇通往第i+1个房间,另一扇通往第p[i]个房间,(p[i]<=i)然后他每经过一个房间就做一个标记,只有偶数个标记时他才会走第一扇门.问你他走到第n+1个房间需要多少单位时间. 思路:dp,一个人进入一个房间之后,如果标记为奇数的话,他会进入p[i]房间,又重新进入这个房间,重复一次的过程,所以dp[i]=dp[i-1]+dp[i-1]-dp[p[i]-1]+2; #in…
http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year's Eve…
http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where…
题目链接:https://codeforces.com/problemset/problem/191/A 题意: 给出 $n$ 个小写字母组成的字符串,两个字符串如果前者的最后一个字母与后者的首字母相同,那么两者可以连接, 同时要求最后得到的一个长字符串的首尾字母也要相同,求最长的满足要求的字符串的长度是多少. 题解: 这个DP蛮有意思的. 记 $f[x][y]$ 为从第一个字符串到当前字符串,字母 $x$ 到字母 $y$ 的最长字符串的长度. 这样,对于当前的字符串 $s_i$,状态转移可以枚…
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符串是不好的. 现在你要删掉若干字母,使得字符串是好的,同时删除第 $i$ 个字母会使得歧义程度增加 $a[i]$,你需要让歧义程度最低,输出这个值. 题解: $dp[i][x=0,1,2,3]$ 的状态是前 $i$ 个字母,第二维 $x$ 代表:$0$——不包含任何有可能构成 “$hard$” 的子…
题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的方块进行重新涂色,使得任意两个方块的颜色不同. 题解: $dp[i][x]$ 表示前 $i$ 个方块,第 $i$ 个方块颜色是 $x$,最少重新涂色多少个方块使得满足要求. AC代码: #include<bits/stdc++.h> using namespace std; const int I…
POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须安装在山脊上(我们可以认为这是一条长度为L(1<=L<=1,000,000)的一维数列:L是偶数). 每个洒水器沿山脊向两个方向地面排水一段距离.每个喷雾半径是A.B范围内的整数(1<=A<=B<=1000).农夫约翰需要给整个山脊浇水,用一个喷头覆盖整个山脊上的每一个位置.此外…
题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue wit…
https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组成n颗宝石有多少种方法 题解 数据很大:找规律or矩阵快速幂 转移方程: dp[i]=dp[i-1]+dp[i-m] 因为n<=1e18可以用矩阵快速幂 构造矩阵如图: \[ \left[ \begin{matrix} f[i-1] & f[i-2] & \cdots & f[i…
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走向它的倍数一个一个往里加元素就好啦, 往那边走 这个可以用dp求出来, dp[ i ] 表示 gcd(x, m)从 i 开始最大元素一共有多少个, dp[ i ] = max( dp[ j ] ) + cnt[ i ]   且 i | j 然后用扩展欧几里德求出走到下一步需要乘多少. #include<…
534B - Covered Path 思路:贪心,每一秒取尽可能大并且可以达到的速度. 画张图吧,不解释了: 代码: #include<bits/stdc++.h> using namespace std; #define ll long long ],dp1[]; int main() { ios::sync_with_stdio(false); cin.tie(); int v1,v2,t,d; cin>>v1>>v2>>t>>d; dp[…
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be…
题目链接:http://codeforces.com/problemset/problem/264/B 题目大意:给出n个单调递增的数,让你找出最长的好序列,好序列是一种单调递增的并且相邻元素的最大公因数>1的子序列.解题思路:设dp[x]表示以x为结尾的好序列的最长长度.d[i]表示以i为因子的x中最大的dp[x]值.于是得到状态转移方程dp[x]=max(dp[x],d[i]+1) (i是x的因子)每次求出dp[x]还要顺便更新d[i],即d[i]=dp[x]. 代码: #include<…
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值和为$A(u_1, u_m) = V_{u_1} - V{u_2} + V{u_3} - V{u_4} + \cdots + (-1)^{m+1}V{u_m}$.求$\sum\limits_{u_i=1}^{N}\sum\limits_{u_j=1}^{N}A(u_i, u_j)\ \%\ (10^…
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可以获得分值x,求出能获得的最大分值为多少. 解题思路:从小到大排序,去重一下, 用cnt[i]记录一下数字i出现次数.那么可以得到状态转移方程:dp[i]=max(dp[i],dp[j]+cnt[i]*a[i])(j<i&&a[i]-a[j]>1),再用单调队列优化一下就行了O(∩…
题意: 一个人在起点0,有n个休息点,每个点有两个数值,分别表示距离起点的距离xi,以及所获得的愉悦值bi,这个人打算每天走L距离,但实际情况不允许他这么做.定义总体失望值val = sum(sqrt(Ri - L)) / sum(bi); 现在要使得val最小(这个人必须要到达最终的节点). 析:其实这个题并不难,看好这个式子,只要变形一下,sum(sqrt(Ri - L)) - val * sum(bi) = 0,要求val最小,假设val就是最后答案那肯定满足sum(sqrt(Ri - L…