Codeforce 588A - Duff and Meat (貪心)
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
InputThe first line of input contains integer n
In the next n lines, i-th line contains two integers ai and pi (1?≤?ai,?pi?≤?100), the amount of meat Duff needs and the cost of meat in that day.
OutputPrint the minimum money needed to keep Duff happy for n days, in one line.
Examples input3output
1 3
2 2
3 1
10
3output
1 3
2 1
3 2
8Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
題解:不斷更新肉價的最小值
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 using namespace std; 13 #define lowbit(x) (x&(-x)) 14 #define max(x,y) (x>y?x:y) 15 #define min(x,y) (x<y?x:y) 16 #define MAX 100000000000000000 17 #define MOD 1000000007 18 #define pi acos(-1.0) 19 #define ei exp(1) 20 #define PI 3.141592653589793238462 21 #define INF 0x3f3f3f3f3f 22 #define mem(a) (memset(a,0,sizeof(a))) 23 typedef long long ll; 24 const int N=100005; 25 const int mod=1e9+7; 26 int a[N],b[N]; 27 int main() 28 { 29 int n; 30 scanf("%d",&n); 31 for(int i=0;i<n;i++){ 32 scanf("%d %d",&a[i],&b[i]); 33 } 34 ll s=0; 35 s+=a[0]*b[0]; 36 int t=b[0]; 37 for(int i=1;i<n;i++){ 38 if(b[i]<t) t=b[i]; 39 s+=a[i]*t; 40 } 41 printf("%lld\n",s); 42 return 0; 43 }
Codeforce 588A - Duff and Meat (貪心)