UVA - 10273 Eat or Not to Eat?
Posted liguangsunls
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 10273 Eat or Not to Eat?相关的知识,希望对你有一定的参考价值。
Description
Problem E
Eat or not to Eat?
Input: Standard Input
Output: Standard Output
A young farmer has N cows, but they produced really really a very very small amount of milk. John cannot live on the milk they made, so he‘s planning to eat some of the ‘worst‘ cowsto get rid of hunger. Each day, John chooses the cow that produces the LEAST amount of milk on that dayand eat them. If there are more than one cow with minimal milk, John will be puzzled and will not eatany of them (Yeah! That‘s GREAT!!).
The i-th cow has a cycle of production Ti. That means, if it produces L unit milk on one day, itwill also produce L unit after Ti days -- If it will not be eaten during these day :-).Though John is not a clever man, he doubts whether the cows will be eventually eaten up,so he asks for your help. Don‘t forget that he will offer you some nice beef for that!
Input
The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50)Each test case begins with an integer N(1<=N<=1000), the number of cows. In the following N lines,each line contains an integer Ti(1<=Ti<=10), indicating the cycle of the i-th cow, then Ti integers Mj(0<=Mj<=250) follow, indicating the amount of milk it can produce on the j-th day.
Output
For each test case in the input, print a single line containing two integers C, D, indicating the number of cows that will NOT be eaten, and the number of days passed when the last cow is eaten. If no cow is eaten, the second number should be 0.
Sample Input
14
4 7 1 2 9
1 2
2 7 1
1 2
Sample Output
2 6__________________________________________________________________________________________
Rujia Liu
题意:已知有n头牛,每一个牛有自己的产奶周期,然后每天杀一头,找最低产奶量的杀,假设有两个一样少的话,今天就不杀。求最后剩下几头和最后杀的时间
思路:在2*lcm里面暴力枚举,lcm表示全部周期的最小公倍数。在2倍周期的天数里我们就能够匹配到全部的可能,就是a牛第几天的产奶量和b牛第几天的产奶比較
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 1010; const int INF = 0x3f3f3f3f; int n, lcm; vector<int> v[MAXN]; int vis[MAXN]; int gcd(int a, int b) { return b==0?a:gcd(b, a%b); } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); memset(vis, 0, sizeof(vis)); for (int i = 0; i <= n; i++) v[i].clear(); int m, d, flag = 1; for (int i = 0; i < n; i++) { scanf("%d", &m); if (flag) { flag = 0; lcm = m; } lcm = lcm/gcd(lcm, m)*m; for (int j = 0; j < m; j++) { scanf("%d", &d); v[i].push_back(d); } } int cur = 0, cnt = n, day = 0; while (day <= 2*lcm) { int Min = INF; int minIndex = -1; for (int i = 0; i < n; i++) { if (!vis[i]) { int size = v[i].size(); int tmp = v[i][day%size]; if (Min > tmp) { Min = tmp; minIndex = i; } else if (Min == tmp) minIndex = -1; } } day++; if (minIndex != -1) { cnt--; cur = day; vis[minIndex] = 1; } } printf("%d %d\n", cnt, cur); } return 0; }
以上是关于UVA - 10273 Eat or Not to Eat?的主要内容,如果未能解决你的问题,请参考以下文章
UVA 11768 - Lattice Point or Not(数论)
UVA - 11768 Lattice Point or Not (扩展欧几里得)