日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

[POJ3040] Allowance

發(fā)布時(shí)間:2023/12/13 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 [POJ3040] Allowance 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute the maximum number of weeks he can pay Bessie.

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Each line corresponds to a denomination of coin and
contains two integers: the value V (1 <= V <= 100,000,000) of the
denomination, and the number of coins B (1 <= B <= 1,000,000) of
this denomation in Farmer John's possession.

Output

* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance

題解:

貪心

貪心策略(分三步):

1、顯然先把大于c的全部出完

2、從大到小只要不超過c,能出就出

3、從小到大能出就出,直到剛好超過c

證明:顯然每次出錢需要浪費(fèi)的盡量少,這樣每次出只會(huì)使得出的錢剛好等于c或大于c一點(diǎn)點(diǎn),所以每次是最優(yōu)的

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

const int N =25;

struct Node {
  int a,b;
  bool operator < (const Node &x) const {
    return a<x.a;
  }
}p[N];

int gi() {
  int x=0,o=1; char ch=getchar();
  while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
  if(ch=='-') o=-1,ch=getchar();
  while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return o*x;
}

int main() {
  int n=gi(),c=gi(),i,ans=0;
  for(i=1; i<=n; i++) p[i].a=gi(),p[i].b=gi();
  sort(p+1,p+n+1);
  i=n;
  while(p[i].a>c) ans+=p[i].b,i--;
  n=i;
  while(1) {
    int now=0;
    for(i=n; i>=1; i--) {
      while(p[i].b && now+p[i].a<=c) {
	now+=p[i].a;
	p[i].b--;
      }
    }
    for(i=1; i<=n; i++) {
      while(p[i].b && now<c) {
	now+=p[i].a;
	p[i].b--;
      }
    }
    if(now<c) break;
    ans++;
  }
  printf("%d", ans);
  return 0;
}

總結(jié)

以上是生活随笔為你收集整理的[POJ3040] Allowance的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。