[AIZU1361] Deadlock Detection [2015 ACM-ICPC Asia Tsukuba Regional Contest F]
2016-07-30 14:58
651 查看
题意
给出p个进程和r种资源,每一种资源有ri个资源,每个进程对每一种资源需要need[p][r]个。给出时刻表,每个时刻会有一个进程请求某一种资源中的一个,当一个进程将所有需要的资源请求完毕后会释放掉所有资源。由于资源分配顺序不当可能出现“锁死现象”,某两个进程等待对方施放资源。问什么时刻会“不可避免地”将要出现“锁死现象”。
题解
二分时间t,t及之前的请求按照给出的分配顺序进行分配,之后不断枚举所有的进程,如果该进程满足所有的资源数目,则释放它占用的资源,直到不能结束进程。当这个过程结束后还存在未结束的进程,那么当前时刻就是个“不可避免地”发生锁死的时刻,反之,则为可能不会发生锁死的时刻。代码
/****************************************\ * Author : ztx * Title : * ALG : * CMT : * Time : \****************************************/ #include <cstdio> #define Rep(i,l,r) for(i=(l);i<=(r);i++) #define rep(i,l,r) for(i=(l);i< (r);i++) #define Rev(i,r,l) for(i=(r);i>=(l);i--) #define rev(i,r,l) for(i=(r);i> (l);i--) typedef long long ll ; typedef double lf ; int CH , NEG ; template <typename TP>inline void read(TP& ret) { ret = NEG = 0 ; while (CH=getchar() , CH<'!') ; if (CH == '-') NEG = true , CH = getchar() ; while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ; if (NEG) ret = -ret ; } template <typename TP>inline void readc(TP& ret) { while (ret=getchar() , ret<'!') ; while (CH=getchar() , CH>'!') ; } template <typename TP>inline void reads(TP *ret) { ret[0]=0;while (CH=getchar() , CH<'!') ; while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ; ret[ret[0]+1]=0; } #include <cstring> #define maxx 310LL #define maxt 200010LL int pro, res, tim; int orineed[maxx][maxx]; int p[maxt], r[maxt]; int t_need[maxx][maxx], t_avai[maxx]; bool t_term[maxx]; struct statu { int need[maxx][maxx]; int apply[maxx], avai[maxx]; bool term[maxx]; inline void init() { int i,j; Rep (i,1,res) read(avai[i]); Rep (i,1,pro) term[i] = false, apply[i] = 0; Rep (i,1,pro) Rep (j,1,res) read(orineed[i][j]), need[i][j] = orineed[i][j], apply[i] += need[i][j]; } inline void acqu(int p,int r) { need[p][r] -- , apply[p] -- , avai[r] -- ; if (!apply[p]) { term[p] = true; Rep (r,1,res) avai[r] += orineed[p][r]; } } inline void transcript() { memcpy(t_need, need, sizeof need); memcpy(t_avai, avai, sizeof avai); memcpy(t_term, term, sizeof term); } inline bool valid() { int i, j; bool RE = false, release; transcript(); #define need t_need #define avai t_avai #define term t_term while (!RE && (RE=true)) Rep (i,1,pro) if (!term[i]) { release = true; Rep (j,1,res) if (need[i][j] > avai[j]) { release = false; break; } if (release) { term[i] = true; Rep (j,1,res) avai[j] += orineed[i][j]-need[i][j]; RE = false; } } Rep (i,1,pro) if (!term[i]) return false; return true; #undef need #undef avai #undef term } }last, now; int main() { int i, L, R, M; // #define READ #ifdef READ freopen(".in" ,"r",stdin ) ; freopen(".out","w",stdout) ; #endif while (scanf("%d%d%d", &pro, &res, &tim) != EOF) { last.init(); Rep (i,1,tim) read(p[i]), read(r[i]); last.acqu(p[1],r[1]); L = 1, R = tim+1; while (M=L+R>>1 , L+1 < R) { now = last; Rep (i,L+1,M) now.acqu(p[i],r[i]); if (now.valid()) last = now, L = M; else R = M; } printf("%d\n", (R>tim ? -1 : R)); } #ifdef READ fclose(stdin) ; fclose(stdout) ; #else getchar() ; getchar() ; #endif return 0 ; }
相关文章推荐
- HDOJ 5773 (2016多校联合训练 Training Contest 4) The All-purpose Zero
- Baidu地图在自己的项目中使用(二)
- poj 1681 Painter's Problem 【高斯消元 枚举自由变元】
- HDOJ 5775 (2016多校联合训练 Training Contest 4) Bubble Sort
- LeetCode--373. Find Pairs with Smallest Sums
- HDOJ 5763 (2016多校联合训练 Training Contest 4) Another Meaning
- Composer:Failed to execute git clone --mirror ...Permission denied (publickey). fatal: Could not
- HDU5724 Chess 阶梯博弈变形 2016 Multi-University Training Contest 1
- Assign directions to edges so that the directed graph remains acyclic
- HDU 2476 String painter(动态规划)★
- Rails 中 View 层 haml 初步
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv
- POJ 1363 Rails(栈,模拟地铁出站)
- HDU 1082 Matrix Chain Multiplication(栈和map的使用)
- wait、notify、notifyAll和Condition
- [iPhone]Xcode process launch failed: Security
- CURL命令报错:dyld: lazy symbol binding failed: Symbol not found: _SSL_load_error_strings解决办法
- foxmail导入outlook部分邮件
- POJ 1218 THE DRUNK JAILER
- Aizu 2677 Breadth-First Search by Foxpower