[Exercise 4-8 UVA-12108] extraordinarily tired students

Source: Internet
Author: User

[LINK] I am a link. Click me :)
Question]

[Question]


It is good to simulate one unit time and one unit time.
Then for everyone.
Record its cycle subscript idx
Every unit of time will let everyone's idx ++
Pay attention to the processing of the line from waking to sleep.

It can be repeated several times .. No solution is considered if the limit is exceeded
(Other solutions also provide a method, that is, if the status is the same as the initial situation. A ring is formed without any solution.
(If there is no solution, it will definitely form a ring?

[Code]

#include <bits/stdc++.h>#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)using namespace std;const int N = 10;int n;int a[N+10][N+10];int cur[N+10],tot[N+10];int main(){    //freopen("D:\\rush.txt","r",stdin);    ios::sync_with_stdio(0),cin.tie(0);    int kase = 0;    while (cin >> n){        if (n==0) break;        rep1(i,1,n){            int x,y;            cin >> x >> y >> cur[i];cur[i]--;            rep1(j,1,x) a[i][j-1] = 1;            rep1(j,x+1,x+y) a[i][j-1] = 2;            tot[i] = x+y;        }        cout<<"Case "<<++kase<<": ";        rep1(kk,1,10000){            int sleep = 0,wake = 0;            rep1(i,1,n){                if (a[i][cur[i]]==1) wake++;else sleep++;            }            if (sleep==0){                cout<<kk<<endl;                break;            }            rep1(i,1,n){                if (a[i][cur[i]]==1 && a[i][(cur[i]+1)%tot[i]]==2){                    if (sleep>wake){                        cur[i] = (cur[i]+1)%tot[i];                    }else{                        cur[i] = 0;                    }                }else{                    cur[i] = (cur[i]+1)%tot[i];                }            }        }        int sleep = 0;        rep1(i,1,n)            if (a[i][cur[i]]==2)                sleep++;        if (sleep>0) cout<<-1<<endl;    }    return 0;}

[Exercise 4-8 UVA-12108] extraordinarily tired students

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.