What is the difference between the snapshot 10273 Eat and Not to Eat?

Source: Internet
Author: User

 
Idea: brute-force Solution
Analysis:
1. The number of cows that have not been eaten must be the number of days that have been last eaten.
2. No other method can only be violent. For n cycles of native cattle, obtain the minimum public multiple, And then solve the problem by brute force within 2 Public multiples.

Code:


 

#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();    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.