模拟暴力 uva12108
来源:互联网 发布:java web简单小项目 编辑:程序博客网 时间:2024/06/04 19:37
这道题是紫书第四章的习题,拖了好久了。其实我这里我用了取巧的方法:我假设如果过去了1e7秒,还没有出现所有人认真听课的情况的话,那么就永远不会出现所有人认真听课的情况了,然后AC了。
关键在于一些细节的处理,比如编号从0开始,初始状态的处理等等。
#include<bits/stdc++.h>using namespace std;int n,num_awake,num_sleep;int status[11],a[11],b[11];bool check(int i)//if the student No.1 is asleep return false,else return true{ status[i]++; //printf("a[%d]=%d,b[%d]=%d,status[%d]=%d\n",i,a[i],i,b[i],i,status[i]); status[i]%=(a[i]+b[i]); if(status[i]<a[i])//continue to be awaken { return true; } if(status[i]==a[i])//the check point { if(num_sleep>num_awake)//choose to sleep { return false; } else { status[i]=0;//choose not to sleep,hang on for another a seconds return true; } } if(status[i]>a[i]) return false;}int main(void){ int cas=0,ans,temp_awake,temp_sleep; //freopen("in","r",stdin); while(cin>>n) { if(n==0) break; cas++; ans=-1; num_awake=num_sleep=0; for(int i=1;i<=n;i++) { scanf("%d%d%d",&a[i],&b[i],&status[i]); status[i]--;//easy to take mod,now status[i] is from 0 to a+b(exclusive) if(status[i]<a[i])//this student is awake num_awake++; else num_sleep++; } if(num_sleep==0) { printf("Case %d: %d\n",cas,1); continue; } for(int t=2;t<=1000000;t++) { temp_awake=temp_sleep=0; for(int i=1;i<=n;i++) { if(check(i))//he will be awaken temp_awake++; else temp_sleep++; } if(temp_sleep==0) { ans=t; break; } num_awake=temp_awake; num_sleep=temp_sleep; } printf("Case %d: %d\n",cas,ans); } return 0;}
0 0
- 模拟暴力 uva12108
- UVA12108 Extraordinarily Tired Students 【模拟】
- uva12108
- UVa12108
- uva12108
- UVA12108 Extraordinarily Tired Students(超级大模拟)
- HDU **** 暴力模拟
- HLJUOJ1117(暴力模拟)
- uva10273(暴力模拟)
- HDU2555(模拟暴力)
- uva1504(模拟+暴力)
- 【模拟】【暴力】[COCI]排序
- HDU6168(暴力模拟)
- POJ 1013 小水题 暴力模拟
- 1035 Spell checker 暴力模拟
- UVA 140 Bandwidth 暴力模拟
- Uva10288(概率期望+暴力模拟)
- HDU 5071 Chat 暴力模拟
- Makefile 使用总结
- Linux常用命令学习(一)
- freemark 判断并截取字符长度
- 欧几里得算法及其拓展
- c# WinForm 同一个窗体只创建一次实例
- 模拟暴力 uva12108
- c++实现单链表创建,删除,遍历,插入,修改操作
- C++: static
- 使用threejs点云秀出酷炫的图片效果(二)
- IO模型 阻塞 非阻塞 同步 异步概念对比区分
- Chapter12——编写一个程序,求面积和
- Array Partition I问题及解法
- 利用rsyslog对非系统日志进行远程传输
- ubantu 删除掉的文件回收站里没有