uva10273(暴力模拟)

来源:互联网 发布:网络教研总结 编辑:程序博客网 时间:2024/05/16 05:57

不知道为什么这题会在图伦里,纯暴力过了..

题目的意思就是好几只羊,他们每天产奶,

产奶是有周期的,例如4 1 2 3 4,就是周期为 4 ,第一天产1 ,第二天产2 ,第三天产3 ,第四天产4 ,第五天又是1.

然后每一天,这个人都会选产奶最少的干掉.但是如果最小的同时存在两个,这天就不杀.

求最后剩下几只不会被杀, 还有最后杀的那只是在第几天.

直接模拟..

有一点就是要求出天数的最大公倍数.

如果最大公倍数的天都一只都没杀,那就不用继续了.就永远死不了了..


AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N = 1000;const int INF = 0x3f3f3f3f;int day[N];int vis[N];int amount[N][250];int n;int cul(int x , int y) {int temp;if(x > y) {temp = x ;x = y;y = temp;}temp = 1;while(y != 0) {temp = x % y;x = y;y = temp;}return x;}int cycle() {//sort(day , day + n);int res = day[0];for (int i = 1 ; i < n ;i++) {res = res * day[i] / cul(res , day[i]);}return res ;}int main () {int t;scanf("%d",&t);while(t--) {memset(vis,0,sizeof(vis));scanf("%d",&n);for (int i = 0 ; i < n ;i++) {scanf("%d",&day[i]);for (int j = 0 ; j < day[i] ; j++) {scanf("%d",&amount[i][j]);}}int res = 0;int ans = 0;int k = 0 ;int kill = 0;int c = cycle();bool eat;while(1) {int minn = INF;for (int i = 0 ; i < n ;i++) {if(vis[i])continue;if(amount[i][res % day[i]] < minn) {eat = true;minn = amount[i][res % day[i]];kill = i;}else if(amount[i][res % day[i]] == minn) {eat = false;}}if (eat) {vis[kill] = 1;ans++;k = 0;}else {k++;if(k == c) {res -= c ;break;}}res++;}printf("%d %d\n",n - ans , res + 1);}}



0 0