【習題 4-8 UVA - 12108】Extraordinarily Tired Students
阿新 • • 發佈:2018-10-31
【連結】 我是連結,點我呀:)
【題意】
【題解】
一個單位時間、一個單位時間地模擬就好。
然後對於每個人。
記錄它所處的週期下標idx
每個單位時間都會讓每個人的idx++
注意從醒著到睡著的分界線的處理就好。
可以多迴圈幾次。。超過上限認為無解
(其他題解也提供了一種方法,就是如果狀態和初始的情況相同的話。就無解了即形成了一個環。
(可能如果無解一定會形成環?
【程式碼】
#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; }