zoukankan      html  css  js  c++  java
  • Deadlock Detection

    听说有老鸽读不懂题,。。。

    给出 (p) 个进程和 (r) 种资源,每一种资源有 (r_i) 个资源,每个进程对每一种资源需要 (need[p][r]) 个。

    给出时刻表,每个时刻会有一个进程请求某一种资源中的一个,当一个进程将所有需要的资源请求完毕后会释放掉所有资源。

    由于资源分配顺序不当可能出现“锁死现象”,某两个进程等待对方施放资源。问什么时刻会“不可避免地”将要出现“锁死现象”。

    二分时间 (t)(t) 及之前的请求按照给出的分配顺序进行分配。

    之后不断枚举所有的进程,如果该进程满足所有的资源数目,则释放它占用的资源,直到不能结束进程。

    当这个过程结束后还存在未结束的进程,那么当前时刻就是个“不可避免地”发生锁死的时刻,反之,则为可能不会发生锁死的时刻。

    // Hacheylight
    #include <map>
    #include <set>
    #include <ctime>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <vector>
    #include <bitset>
    #include <cstdio>
    #include <cctype>
    #include <string>
    #include <numeric>
    #include <cstring>
    #include <cassert>
    #include <climits>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std ;
    //#define int long long
    #define rep(i, a, b) for (int i = (a); i <= (b); i++)
    #define per(i, a, b) for (int i = (a); i >= (b); i--)
    #define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
    #define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
    #define clr(a) memset(a, 0, sizeof(a))
    #define ass(a, sum) memset(a, sum, sizeof(a))
    #define lowbit(x) (x & -x)
    #define all(x) x.begin(), x.end()
    #define SC(t, x) static_cast <t> (x)
    #define ub upper_bound
    #define lb lower_bound
    #define pqueue priority_queue
    #define mp make_pair
    #define pb push_back
    #define pof pop_front
    #define pob pop_back
    #define fi first
    #define se second
    #define y1 y1_
    #define Pi acos(-1.0)
    #define iv inline void
    #define enter cout << endl
    #define siz(x) ((int)x.size())
    #define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
    typedef double db ;
    typedef long long ll ;
    typedef unsigned long long ull ;
    typedef pair <int, int> pii ;
    typedef vector <int> vi ;
    typedef vector <pii> vii ;
    typedef queue <int> qi ;
    typedef queue <pii> qii ;
    typedef set <int> si ;
    typedef map <int, int> mii ;
    typedef map <string, int> msi ;
    const int N = 310 ;
    const int M = 200010 ;
    const int INF = 0x3f3f3f3f ;
    const int iinf = 1 << 30 ;
    const ll linf = 2e18 ;
    const int MOD = 1000000007 ;
    const double eps = 1e-7 ;
    void douout(double x){ printf("%lf
    ", x + 0.0000000001) ; }
    template <class T> void print(T a) { cout << a << endl ; exit(0) ; }
    template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
    template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
    template <class T> void add(T &a, T b) { a = (1ll * a + b) % MOD ; }
    template <class T> void sub(T &a, T b) { a = (a - b + MOD) % MOD ; }
    template <class T> void mul(T &a, T b) { a = (ll) a * b % MOD ; }
    template <class T> T read() {
        int f = 1 ; T x = 0 ;
        char ch = getchar() ;
        while (!isdigit(ch)) { if (ch == '-') f = -1 ; ch = getchar() ; }
        while (isdigit(ch)) { x = x * 10 + ch -'0' ; ch = getchar() ; }
        return x * f ;
    }
    
    int n, m, p ;
    int now[N], sum[N], has[N], rem[N], ax[M], ay[M] ;
    int need[N][N], Need[N][N] ;
    bool inq[N] ;
    
    bool check(int t) {
    	int a, b ;
    	rep(i, 1, m) rem[i] = has[i] ;
    	rep(i, 1, n)
    	rep(j, 1, m)
    	Need[i][j] = need[i][j] ;
    	rep(i, 1, n) inq[i] = 1, now[i] = 0 ;
    	rep(i, 1, t) {
    		a = ax[i], b = ay[i] ;
    		now[a]++, Need[a][b]--, rem[b]-- ;
    		if (now[a] == sum[a]) {
    			rep(j, 1, m) rem[j] += need[a][j] ;
    			inq[a] = 0 ;
    		}
    	}
    	bool flg = 1 ;
    	while (flg) {
    		flg = 0 ;
    		rep(i, 1, n)
    		if (inq[i]) {
    			int j ;
    			for (j = 1; j <= m; j++) if (Need[i][j] > rem[j]) break ;
    			if (j > m) {
    				rep(j, 1, m) rem[j] += need[i][j] - Need[i][j] ;
    				inq[i] = 0 ; flg = 1 ;
    			}
    		}
    	}
    	rep(i, 1, n) if (inq[i]) return 0 ;
    	return 1 ;
    }
    signed main() {
    	scanf("%d%d%d", &n, &m, &p) ;
    	rep(i, 1, m) scanf("%d", &has[i]) ;
    	rep(i, 1, n)
    	rep(j, 1 ,m)
    	scanf("%d", &need[i][j]), sum[i] += need[i][j] ;
    	rep(i, 1, p) scanf("%d%d", &ax[i], &ay[i]) ;
    	if (check(p)) print("-1") ; // 特判
        int l = 0, r = p ;
        while (l < r) {
    		int mid = (l + r) >> 1 ;
    		if (check(mid)) l = mid + 1 ;
    		else r = mid ;
    	}
    	printf("%d
    ", l) ;
    	return 0 ;
    }
    
    /*
    写代码时请注意:
    	1.ll?数组大小,边界?数据范围?
    	2.精度?
    	3.特判?
    	4.至少做一些
    思考提醒:
    	1.最大值最小->二分?
    	2.可以贪心么?不行dp可以么
    	3.可以优化么
    	4.维护区间用什么数据结构?
    	5.统计方案是用dp?模了么?
    	6.逆向思维?
    */
    
    
    
    
    
  • 相关阅读:
    2016年开源软件评选(截图备份)
    牛逼的思维方式都是倒逼出来的(摘)
    3-22 多态
    3 -20 类
    3 -19标准库
    3 -16 json序列化
    3 -16 内置方法
    迭代对象 和 迭代器
    3 -14 迭代 和列表 生成器
    3-13 装饰器
  • 原文地址:https://www.cnblogs.com/harryhqg/p/11141623.html
Copyright © 2011-2022 走看看