ACM_20190730_基础动态规划

A - 免费馅饼 HDU - 1176
都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:

为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)
Input
输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。
Output
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。

Sample Input

6
5 1
4 1
6 1
7 2
7 2
8 3
0

Sample Output
4
AC代码:


B.最少拦截系统
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 72717 Accepted Submission(s): 27997

Problem Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)

Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input
8 389 207 155 300 299 170 158 65

Sample Output
2

Source
浙江工业大学第四届大学生程序设计竞赛

思路:
遍历所有导弹,如果没有大于等于导弹高度的系统,就添加一个拦截系统。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n, a, b[300005], cnt;
	while(scanf("%d", &n) != EOF){
		memset(b, 0, sizeof(b));
		cnt = 0;
		for(int i = 0; i < n; i++){
			cin >> a;
			int j;
			for( j = 0; j <= cnt; j++){
				if(b[j] >= a){//如果能够拦截,那就拦截 
					b[j] = a;
					break;
				}
			}
			if(j > cnt){//如果不能拦截,那就从开一个 
				b[++cnt] = a;//
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
} 

D - Our Tanya is Crying Out Loud CodeForces - 940B
Right now she actually isn’t. But she will be, if you don’t solve this problem.

You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

Subtract 1 from x. This operation costs you A coins.
Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).

Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.

Examples
Input

9
2
3
1

Output

6

Input

5
5
2
20

Output
8
Input

19
3
4
2

Output
12
Note
In the first testcase, the optimal strategy is as follows:

Subtract 1 from x (9 → 8) paying 3 coins.
Divide x by 2 (8 → 4) paying 1 coin.
Divide x by 2 (4 → 2) paying 1 coin.
Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
思路:
题目大意就是给一个数n, 让n 变成k 有两种操作, 第一种减一,第二种除以k。
AC代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
	long long n, k, a, b;
	long long sum = 0;
	cin>> n >> k >> a >> b;
	if(n < k || k == 1){	cout<< (n-1) * a <<endl;	return 0;}
	//k == 1 只能-1 
	while(n > 1){
		if(n < k){
			sum += (n-1)*a;
			break;
		}
		if(n%k != 0) sum +=(n%k) * a; 
		// 
		int t= n/k;
		n = n-n%k;
		if((n-t)*a < b) sum += (n-t)*a;
		else sum += b;
		n /= k;
	}
	cout<< sum <<endl;
	 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值