poj 1666

Description

There is a huge mountain outside Kicc's house, and Kicc has to climb the mountain every time he wants to go out. It costs him a lot of time and he wants to change the situation. Yes, he figures out a good idea - to move the mountain away! But there is a problem. There are some wild animals not too far away from the mountain. Kicc will make noises when digging or moving the stones. If the animals hear the sound made by Kicc, they will come closer. If an animal reaches the mountain and sees Kicc, the animal will eat Kicc immediately. But the animals are stupid - when they do not hear the sound, they will go back along the exact route they come to the mountain until they are in the place where they start. When they hear the sound again, they will come to the mountain again no matter where they are. In this situation, Kicc has to stop sometimes before the first animal arrives to avoid being eaten. You should notice that as soon as the animal arrive in the mountain, Kicc will be eaten, no matter he stops his work or not.

Kicc has t units of time per day and he can handle x units of stones in one unit of time (This action will make noises). There are m animals near the mountain. The i-th animal stays away the mountain with d i units of distance and can run s i units of distance in one of unit time. Kicc wants to know the most amount of stones he can handle every day (You can assume the amount of the stones in the mountain is infinite). To make it easier, you may assume that Kicc can work or rest for only integer units of time, for example, 1 unit of time, or 2 units of time, or 3 units of time, and so on.

Input

The first line contains an integer t (0 <= t < 1000000), and an integer x (0 < x < 10). The second line contains a single integer m (0 <= m < 1000). There are m lines following the second line, each has 2 integers d i (0 < d i < 1000000) and s i (0 < s i < 1000000).

Output

The output should contain a single integer which means the most amount of stones Kicc can move away in one day.
 
 
题目大意:一个人想移走门前的山,但离此山一段距离处有野生动物,此人一旦工作,动物便以一定速度向这山移动,一旦遇到此人,便将他吃掉。此人一旦停止工作,动物便以同样的速度离开,直到回到原点。给定最大工作时间单位,每一单位时间可移动的石头数量,以及动物个数,各动物离此人的距离,动物移动的速度,求此人一天最大的工作量。
 
 
题目思路很简单,要注意动物不动和人不能动的情况。动物不动,最大值为t*x,当动物离人的最近起始位置小于动物一次移动的距离时,最大值为0。distance和speed相除不一定为整数,区分开。求出动物移动到人的最短时间单位,此后只要一动一停即可。
 
代码:
 
#include<iostream> using namespace std ; int main(){ int t, x, m, d, s, sum, max ; cin >> t >> x >> m ; max = t ; for(int i=0; i<m; i++){ cin >> d >> s ; if(s==0)    continue ;         if(s>=d) max=0 ;         int f = d / s ;         if(d%s==0) f -- ;         sum = f + (t-f) / 2 ;         if(sum<max) max = sum ; } cout << (x*max) << endl ;     return 0 ; } 

你可能感兴趣的:(poj)