Ultraviolet A: 10670-Work ction (Greedy)

Source: Internet
Author: User

Ultraviolet A: 10670-Work ction (Greedy)

Question: 10670-Work ction


N, m, L. n represents the total number of paperworks given by the boss, m represents the remaining number, and L represents the minimum cost you need from so many companies.

Solution:

1. L branch companies. Each line is named by the company: A, B; A represents A copy of the money required by paperwork, B Represents helping you reduce the total number of paperworks to half the money required for the call. Note that you have paperworks on hand instead of the number of paperworks that the boss requires you to complete. It has been stuck here for a long time. Otherwise, the final quantity cannot be smaller than m. Because the boss requires m copies to be left.

2. Separate the input string from the name and Money A and B.

3. Calculate the sorting output of the amount.

Code:

# Include
 
  
# Include
  
   
Using namespace std; const int N = 105; int n, m, l; struct MONEY {char name [N]; int money; int a, B;} M [N]; char str [N]; int cmp (const MONEY & x, const MONEY & y) {if (x. money <y. money) return true; else if (x. money> y. money) return false; else return strcmp (x. name, y. name) <0? True: false;} // separated string names and charges void handle (int j) {int k = 0; for (int I = 0; I <strlen (str ); I ++) {if (str [I]! = ':') K ++; elsebreak;} memcpy (M [j]. name, str, sizeof (str); M [j]. name [k] = '\ 0'; int sum = 0; for (int I = k + 1; I <strlen (str); I ++) {if (str [I]! = ',') Sum = sum * 10 + str [I]-'0'; else {M [j]. a = sum; sum = 0 ;}} M [j]. B = sum;} // calculation fee void cal (int j) {int num = n; int sum = 0; while (1) {if (num/2> = m & (num-num/2) * M [j]. a> = M [j]. b) {sum + = M [j]. b; num/= 2;} else {sum + = (num-m) * M [j]. a; M [j]. money = sum; break ;}}int main () {int t; char ch; scanf ("% d", & t); for (int I = 1; I <= t; I ++) {printf ("Case % d \ n", I); scanf ("% d % c", & n, & m, & l, & ch); for (int j = 0; j <l; j ++) {gets (str); handle (j ); cal (j);} sort (M, M + l, cmp); for (int j = 0; j <l; j ++) printf ("% s % d \ n", M [j]. name, M [j]. money);} return 0 ;}
  
 


Related Article

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.