思路: 暴力求解
分析:
1 題目要求沒有吃掉的奶牛的個數已經最後一次吃掉奶牛的天數
2 沒有其它的方法只能暴力,對於n頭牛的n個周期求最小公倍數,然後在2個公倍數之內暴力求解
代碼:
#include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 1010; int n , lcm; vector<int>v[MAXN]; bool isEat[MAXN]; int gcd(int a , int b){ return b == 0 ? a : gcd(b , a%b); } void init(){ memset(isEat , false , sizeof(isEat)); for(int i = 0 ; i < MAXN ; i++) v[i].clear(); } void solve(){ int index = 0; int notEat = n; int numDay = 0; while(index < 2*lcm){ int min = 1<<30; int minIndex = -1; for(int i = 0 ; i < n ; i++){ if(!isEat[i]){ int size = v[i].size(); int tmp = v[i][index%size]; if(min > tmp){ min = tmp; minIndex = i; } else{ if(min == tmp) minIndex = -1; } } } index++; if(minIndex != -1){ notEat--; numDay = index; isEat[minIndex] = true; } } printf("%d %d\n" , notEat , numDay); } int main(){ int Case , m , x; scanf("%d" , &Case); while(Case--){ scanf("%d" , &n); init(); bool isFirst = true; for(int i = 0 ; i < n ; i++){ scanf("%d" , &m); if(isFirst){ lcm = m; isFirst = false; } lcm = lcm/gcd(lcm , m)*m; while(m--){ scanf("%d" , &x); v[i].push_back(x); } } solve(); } }