【水贪心】#11 A. Increasing Sequence
2014-04-25 01:31
357 查看
A. Increasing Sequence
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
A sequence a0, a1, ..., at - 1 is
called increasing if ai - 1 < ai for
each i: 0 < i < t.
You are given a sequence b0, b1, ..., bn - 1 and
a positive integer d. In each move you may choose one element of the given sequence and add d to
it. What is the least number of moves required to make the given sequence increasing?
Input
The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106).
The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≤ bi ≤ 106).
Output
Output the minimal number of moves needed to make the sequence increasing.
Sample test(s)
input
output
一道模拟题,其实直接模拟就出了,不过用一下除法来提升效率还是养成习惯的好。
贪心:遍历一次,每个当前数比自己前一个数大即可
C++:
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
A sequence a0, a1, ..., at - 1 is
called increasing if ai - 1 < ai for
each i: 0 < i < t.
You are given a sequence b0, b1, ..., bn - 1 and
a positive integer d. In each move you may choose one element of the given sequence and add d to
it. What is the least number of moves required to make the given sequence increasing?
Input
The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106).
The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≤ bi ≤ 106).
Output
Output the minimal number of moves needed to make the sequence increasing.
Sample test(s)
input
4 2 1 3 3 2
output
3
一道模拟题,其实直接模拟就出了,不过用一下除法来提升效率还是养成习惯的好。
贪心:遍历一次,每个当前数比自己前一个数大即可
C++:
#include <algorithm> #include <iostream> #include <cstdio> using namespace std; int main() { int n,d,cnt=0; scanf("%d%d",&n,&d); int val ; for(int i=0;i<n;i++) scanf("%d",&val[i]); for(int i=1;i<n;i++) { if(val[i]>val[i-1])continue; else { int temp=val[i-1]-val[i]; val[i]+=(temp/d+1)*d; cnt+=(temp/d+1); } } printf("%d",cnt); return 0; }
相关文章推荐
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
- 二叉树--Sequence--利用优先队列贪心求解最优匹配
- [LIS]Longest Increasing Sequence:nlogn算法和两种普通DP的实现
- 【CodeForces 5C】【贪心】【dp】Longest Regular Bracket Sequence
- 【HDU】4915 Parenthese sequence 贪心
- HDU4915:Parenthese sequence(贪心)
- hdu 4915 Parenthese sequence(贪心,模拟)
- poj 2442 Sequence(贪心,堆)
- codeforce 3D. Least Cost Bracket Sequence(贪心)
- longestIncreasingSequence最长上升子序列问题
- HDU-5783 Divide the Sequence(贪心水题)
- 【LintCode】最长上升序列(Longest Increasing Sequence)
- Sequence(贪心+优先队列)
- [HDOJ5783]Divide the Sequence(贪心)
- Codeforces Beta Round #11 A. Increasing Sequence
- Find longest increasing sequence (LIS)
- AtCoder Beginner Contest 059 C - Sequence(贪心)
- CF 601B(Lipshitz Sequence-贪心)
- 【codeforces 13 C】【DP + 离散化 + 贪心+滚动数组 】C. Sequence【用最小代价把序列变成非严格递增序列】