当前位置:编程学习 > C/C++ >>

uva 10273 Eat or Not to Eat?

 
思路: 暴力求解
分析:
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 易做图(int a , int b){
    return b == 0 ? a : 易做图(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/易做图(lcm , m)*m;
             while(m--){
                  scanf("%d" , &x);
                  v[i].push_back(x);
             }
         }
         solve();
    }
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,