zoukankan      html  css  js  c++  java
  • hihocoder [Offer收割]编程练习赛8 矩形计数

    考虑k比较小,想到容斥原理,枚举,容斥求和。

    //http://www.cnblogs.com/IMGavin/
    #include <iostream>
    #include <stdio.h>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <stack>
    #include <set>
    #include <bitset>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    #define gets(A) fgets(A, 1e8, stdin)
    const int INF = 0x3F3F3F3F, N = 1008, MOD = 1003;
    
    const double EPS = 1e-6;
    int x[N], y[N];
    int tot;
    int r[N], c[N], n, m, k;
    LL ans;
    
    void dfs(int d, int cnt, int x1, int y1){
    	if(d == tot){
    		if(cnt & 1){
    			ans -= x1 * y1;
    		}else{
    			ans += x1 * y1;
    		}
    		return ;
    	}
    	dfs(d + 1, cnt, x1, y1);
    	dfs(d + 1, cnt + 1, min(x1, x[d]), min(y1, y[d]));
    }
    
    int main(){
    
    	while(cin >> n >> m >> k){
    		for(int i = 0; i < k; i++){
    			scanf("%d %d", &r[i], &c[i]);
    		}
    		ans = 0;
    		for(int i = 1; i <= n; i++){
    			for(int j = 1; j <= m; j++){
    				tot = 0;
    				bool ok = 1;
    				for(int l = 0; l < k; l++){
    					if(i == r[l] && j == c[l]){
    						ok = 0;
    						break;
    					}
    					if(i >= r[l] && j >= c[l]){
    						x[tot] = r[l];
    						y[tot] = c[l];
    						tot++;
    					}
    
    				}
    				if(ok){
    					dfs(0, 0, i, j);
    					
    				}
    
    			}
    		}
    		cout<<ans<<endl;
    
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    IOI2000 回文字串
    洛谷 P2758 编辑距离
    NOIP2012普及第三题 摆花
    [SCOI2005]骑士精神
    [USACO1.5]八皇后 Checker Challenge
    Codeforces Round #637 (Div. 2) 题解
    核电站---两种DP解法
    POJ1077 八数码问题
    洛谷 P1162 填涂颜色
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/IMGavin/p/6505755.html
Copyright © 2011-2022 走看看