POJ 3040-allowance (greedy) __poj

Source: Internet
Author: User
L-allowance Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64D &%i64u Submit Status Practice POJ 3040 appoint Description:system Crawler (2016-05-01)

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 <=) different denominations, where each denomination of coin evenly divides th e Next-larger denomination (e.g., 1 cent coins, 5 cent coins, cent coins, and cent). Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,0 ) 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 <= 10 0,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 This is the number of weeks farmer John can pay Bessie at least C allowance

Sample Input

3 6
1
1
5 120

Sample Output

111

Hint INPUT DETAILS:
FJ would like to pay Bessie 6 cents per week. He has 1-cent coins,120 5-cent coins, and 1 10-cent coin.

OUTPUT DETAILS:
FJ can overpay Bessie with the one 10-cent coin to 1 week, then pay Bessie two 5-cent coins for ten weeks and then pay Bes Sie one 1-cent coin and one 5-cent coin for weeks.
Meaning

From big to small sort, as long as not excessive can put how much, and finally from small start find a put in can excess.

Proof of correctness, because the large is a small multiple, so large put in not excessive must be put in, because the small no matter how to take, and then more than C before will be together into this large denomination, then a large replacement must be better.

After the first step is done, then must now put in a coin, then choose the smallest and can be greater than C of also must be optimal.

AC Code:

* * This greedy comparison pit where I wrote the program is not satisfied to >c is all added a value, so WA. Examples: 4 7 9 1 6 1 1 0 3 2 ans:2/#include <iostream> #include <algorithm> #include <cstring> #include & lt;string> #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include
<queue> #include <vector> #include <set> using namespace std;
const int t=55000;

#define INF 0X3F3F3F3FL typedef long LL; struct node {int v,w;}

A[50];

BOOL CMP (const node& A,const node& b) {return a.v>b.v;}
	int main () {#ifdef ZSC freopen ("Input.txt", "R", stdin); #endif int n,m,i,j;
		while (~SCANF ("%d%d", &n,&m)) {int num=0,cnt=0;
		int k = 0;
		memset (A,0,sizeof (a));
			for (i=0;i<n;++i) {scanf ("%d%d", &AMP;A[I].V,&AMP;A[I].W);
			Excluding the direct can pay if (a[i].v>=m) k+=a[i].w,i--, n--;
		else cnt = A[I].W;
		
		Sort (a,a+n,cmp);
			while (cnt>0) {num = m;
				From large to small money for (i=0;i<n;++i) {if (!A[I].W) continue; if (num>0){int tmp = MIN (NUM/A[I].V,A[I].W);
					A[I].W-= tmp;
					CNT-= TMP;
				num-= TMP*A[I].V;
			} if (num<=0) break; ///From small to large money if (num>0) {for (i=n-1;i>=0;--i) {if (A[I].W) {while (NUM&GT;0&AMP;&AMP;A[I].W) n
						UM-=A[I].V,A[I].W--, cnt--;
					if (num<=0) break;
	}} if (num<=0) k++;//changed here on AC} printf ("%d\n", K);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.