文章目录
A - Increasing Sequence
递增序列指的是,在序列中,对于任何两个相邻的数b[i]和bi+1,总有b[i]<b[i+1]。现在,给你一个序列b[1],b[2],…b[n],和一个正整数d,每次,你可以选择序列中的某一个数,然后给他加上d。那么,最少需要多少次操作才能将序列变成一个递增序列呢?
Input
输入第一行是两个正整数n和d,2<=n<=2000,1<=d<=10^ 6。
接下来一行有n个数,分别表示b[1],b[2]…b[n]这些数,1<=b[i]<=10^ 6。
Output
输出最少需要操作的次数
Sample Input
4 2
1 3 3 2
Sample Output
3
比较水的一道贪心。
依次比较相邻两个数的大小,如果前一个数比后一个数大,就让后一个数加上最小倍数的d,使得后一个数大于前一个数。
#include <cstdio>
using namespace std;
typedef long long ll;
ll arr[2005];
int main(void)
{
ll n, d, ans = 0, t;
scanf("%lld%lld", &n, &d);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &arr[i]);
if (arr[i - 1] >= arr[i])
{
t = (arr[i - 1] - arr[i]) / d + 1;
ans += t;
arr[i] += t * d;
}
}
printf("%lld\n", ans);
return 0;
}
B - Jumping Jack
Jack is working on his jumping skills recently. Currently he’s located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he’ll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
Input
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output
Output the minimal number of jumps that Jack requires to reach x.
Examples
Input
2
Output
3
Input
6
Output
3
Input
0
Output
0
下面的代码是用一个结论做的:记录下每次都向前走的总步数,再遍历每次的步数,如果步数减去x是偶数,表示可以到达。
不太懂这个结论。
题解:https://www.cnblogs.com/jhz033/p/5528587.html
#include <cstdio>
int arr[50005];
int main(void)
{
int x;
for (int i = 0; i < 50000; i++)
{
if (i % 2 == 0)
arr[i] = i / 2 * (i + 1);
else
arr[i] = (i + 1) / 2 * i;
}
scanf("%d", &x);
if (x < 0)
x = -x;
for (int i = 0; ; i++)
{
if (arr[i] >= x && (arr[i] - x) % 2 == 0)
{
printf("%d\n", i);
break;
}
}
return 0;
}