1000. Cutting Sausages
|
||
比较苦逼、爆0了、、总结一下吧、
1000. Cutting Sausages
|
||
Description
Mirko has given up on the difficult coach job and switched to food tasting instead. Having skipped breakfast like a professional connoisseur, he is visiting a Croatian cured meat festival. The most renowned cook at the festival, Marijan Bajs, has prepared N equal sausages which need to be distributed to M tasters such that each taster gets a precisely equal amount. He will use his trusted knife to cut them into pieces.
In order to elegantly divide the sausages, the number of cuts splitting individual sausages must be as small as possible. For instance, if there are two sausages and six tasters (the first test case below), it is sufficient to split each sausage into three equal parts, making a total of four cuts. On the other hand, if there are three sausages and four tasters (the second test case below), one possibility is cutting off three quarters of each sausage. Those larger parts will each go to one of the tasrers, while the fourth taster will get the three smaller pieces (quarters) left over.
Mirko wants to try the famous sausages, so he volunteered to help Bajs. Help them calculate the minimum total number of cuts needed to carry out the desired division.
Input
The first and only line of input contains two positive integers, N and M (1 ≤ N, M ≤ 100), the number of sausages and tasters, respectively.
Output
The first and only line of output must contain the required minimum number of cuts.
Sample Input
样例1: 2 6 样例2: 3 4 样例3: 6 2
Sample Output
样例1: 4 样例2: 3 样例3: 0 题意就是给你n个香肠,让你平均的分给m个人。问你最少要切几刀? 题目很水!最近做数学题目做的多了。以为推一个公式呢。其实不是,就是用一个循环无限分的情况!
首先n%m==0 ans=0;
n>m时 每人得到一个完整的香肠,剩下的n=n%m;的进行平均分配!下一种情况。
n<m时,如果m%n==0,则每个香肠切m/n-1,否则为m/n;
然后将剩下的香肠用上面的方法分给剩下的m=m%n个人,直到m==0结束,即所有人都分完为止。
代码:
|
#include<stdio.h> int main() { int N, M; while(~scanf("%d%d",&N,&M)) { int ans = 0; while(N % M != 0) { if(N > M) N = N % M; int s = M%N == 0 ? M / N -1 : M / N; M = M % N; ans = ans + s * N; if(M == 0) break; } printf("%d\n",ans); } }
#include <cstdio> int main() { int n,m; while(~scanf("%d%d",&n,&m)) { if(n==0||m==0) { printf("0\n");continue; } int ans=0; if(n<m) { if(m%n==0) ans=(m/n-1)*n; else { ans=n*(m/n)+n%(m%n); } } else { if(n%m==0) ans=0; else { n=n%m; if(m%n==0) ans=(m/n-1)*n; else { ans=n*(m/n)+n%(m%n); } } } printf("%d\n",ans); } return 0; }
Time Limit: 2 secs, Memory Limit: 256 MB
The difficult economic situation in the country and reductions in government agricultural subsidy funding
have caused Mirko to change his career again, this time to a thief. His first professional endeavour is a
jewellery store heist.
The store contains N pieces of jewellery, and each piece has some mass Mi and value Vi . Mirko has K bags
to store his loot, and each bag can hold some maximum mass Ci . He plans to store all his loot in these bags,
but at most one jewellery piece in each bag, in order to reduce the likelihood of damage during the escape.
Find the maximum total jewellery value that Mirko can “liberate”.
The first line of input contains two numbers, N and K (1 ≤ N, K ≤ 300 000).
Each of the following N lines contains a pair of numbers, Mi and Vi (1 ≤ Mi , Vi ≤ 1 000 000).
Each of the following K lines contains a number, Ci (1 ≤ Ci ≤ 100 000 000).
All numbers in the input are positive integers.
The first and only line of output must contain the maximum possible total jewellery value.
样例1: 2 1 5 10 100 100 11 样例2: 3 2 1 65 5 23 2 99 10 2
样例1: 10 样例2: 164
Clarification of the second example: Mirko stores the first piece of jewellery into the second bag and
the third piece into the first bag.
题意很好懂,给出n个珠宝的价格和重量。和k个能装t重量的袋子。条件是一个袋子只能装一个珠宝。问怎样装使得价值最大。想到的就是按价值从大到小排序,价值相同的按重量从大到小排序,然后袋子能装的重量从小到大排序,然后依次选能够装进去的,标记。最后算出ans。
但是超时了。求大神指点。
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; struct Node { int wi,vi; int ok; }; Node a[303000]; int bag[303000]; int comp(Node x,Node y) { if(x.vi!=y.vi) return x.vi>y.vi; if(x.wi!=y.wi) return x.wi>y.wi; } int main() { int n,k; while(~scanf("%d%d",&n,&k)) { for(int i=0;i<n;i++) { scanf("%d%d",&a[i].wi,&a[i].vi); a[i].ok=0; } for(int i=0;i<k;i++) scanf("%d",&bag[i]); sort(bag,bag+k); sort(a,a+n,comp); int ans=0; for(int i=0;i<k;i++) { for(int j=0;j<n;j++) { if(bag[i]>=a[j].wi&&a[j].ok==0) { ans+=a[j].vi;a[j].ok=1; break; } } } printf("%d\n",ans); } return 0; }