zoukankan      html  css  js  c++  java
  • uva 10273 Eat or Not to Eat?

    点击打开链接uva 10273

    思路: 暴力求解
    分析:
    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
    " , 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();
        }
    }
    
    



  • 相关阅读:
    shell 命令
    unzip解压失败 添加tar 解压
    tomcat
    Linux常用命令
    压缩归档与解压
    Linux的任务计划管理
    A01. openstack架构实战-openstack基本环境准备
    ubuntu16.04 server版破解密码
    Ubuntu Server 18.04 网络设置不生效的解决
    带宽单位 Mbps 及换算方式
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3231011.html
Copyright © 2011-2022 走看看