CF1472F 填格子 贪心 + map

 1 #include<bits/stdc++.h>
 2 #define rg register
 3 using namespace std;
 4 const int N = 2e5 + 10;
 5 map<int, int> mp;
 6 int n, m;
 7 void solve()
 8 {
 9     for(rg int i = 1 ; i <= m ; i++){
10         int x, y; cin >> x >> y;
11         mp[y] += x;
12     }
13     int last = 0, tag = 0;//tag表示前一格对当前格子的影响
14     for(auto x : mp){
15         if(x.second == 3){//当前满的
16             if(tag != 0){//前面一格戳出来,无解
17                 cout << "NO" << endl;
18                 return ;
19             }
20         }else if(tag > 0){//当前不满,前一格戳出来
21             // 1010 1000
22             // 0000 0001 这两种不成立
23             if((x.second + tag == 3 && (x.first - last) % 2 > 0) || (x.second == tag && (x.first - last) % 2 == 0)){
24                 cout << "NO" << endl;
25                 return ;
26             }
27             tag = 0;
28             last = x.first;
29         }else{//当前不满,前一格对当前无影响,全空
30             tag = x.second;
31             last = x.first;
32         }
33     }
34     if(tag == 0){//空的
35         cout << "YES" << endl;
36     }else{
37         cout << "NO" << endl;
38     }
39 }
40 
41 int main(){
42     cin.tie(0);
43     ios_base::sync_with_stdio(0);
44     int T; cin >> T;
45     while(T--)
46     {
47         cin >> n >> m;
48         solve();
49         mp.clear();
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/ecustlegendn324/p/14248967.html