牛客练习赛13F m皇后

题目链接:https://ac.nowcoder.com/acm/contest/70/F

题目大意:

  略

分析:

  可以分成四步计算冲突:水平方向,垂直方向,左斜线方向,右斜线方向。只要会处理水平方向,其余同理。

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16 
 17 #define SORT(c, s, t) sort(c + s, c + t + 1)
 18  
 19 #define LOWBIT(x) ((x)&(-x))
 20  
 21 #define ALL(x) x.begin(),x.end()
 22 #define INS(x) inserter(x,x.begin())
 23  
 24 #define ms0(a) memset(a,0,sizeof(a))
 25 #define msI(a) memset(a,inf,sizeof(a))
 26 #define msM(a) memset(a,-1,sizeof(a))
 27  
 28 #define pii pair<int,int>
 29 #define piii pair<pair<int,int>,int>
 30 #define MP make_pair
 31 #define PB push_back
 32 #define ft first
 33 #define sd second
 34  
 35 template<typename T1, typename T2>
 36 istream &operator>>(istream &in, pair<T1, T2> &p) {
 37     in >> p.first >> p.second;
 38     return in;
 39 }
 40  
 41 template<typename T>
 42 istream &operator>>(istream &in, vector<T> &v) {
 43     for (auto &x: v)
 44         in >> x;
 45     return in;
 46 }
 47  
 48 template<typename T1, typename T2>
 49 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 50     out << "[" << p.first << ", " << p.second << "]" << "\n";
 51     return out;
 52 }
 53  
 54 typedef long long LL;
 55 typedef unsigned long long uLL;
 56 typedef pair< double, double > PDD;
 57 typedef set< int > SI;
 58 typedef vector< int > VI;
 59 const double EPS = 1e-10;
 60 const int inf = 1e9 + 9;
 61 const LL mod = 1e9 + 7;
 62 const int maxN = 1e5 + 7;
 63 const LL ONE = 1;
 64  
 65 struct Queen{
 66     int r, c, cnt = 0;
 67 }; 
 68 
 69 // 先按行排序,再按列排序 
 70 inline bool cmp1(const Queen &x, const Queen &y) {
 71     if(x.r == y.r) return x.c < y.c;
 72     return x.r < y.r;
 73 }
 74 
 75 // 先按列排序,再按行排序 
 76 inline bool cmp2(const Queen &x, const Queen &y) {
 77     if(x.c == y.c) return x.r < y.r;
 78     return x.c < y.c;
 79 }
 80 
 81 // 先按r - c排序,再按r + c排序 
 82 inline bool cmp3(const Queen &x, const Queen &y) {
 83     if(x.r - x.c == y.r - y.c) return x.r + x.c < y.r + y.c;
 84     return x.r - x.c < y.r - y.c;
 85 }
 86 
 87 // 先按r + c排序,再按r - c排序 
 88 inline bool cmp4(const Queen &x, const Queen &y) {
 89     if(x.r + x.c == y.r + y.c) return x.r - x.c < y.r - y.c;
 90     return x.r + x.c < y.r + y.c;
 91 }
 92 
 93 // 行方向计数辅助函数
 94 inline bool cnt1(const Queen &x, const Queen &y) {
 95     return x.r == y.r;
 96 }
 97 
 98 // 列方向计数辅助函数
 99 inline bool cnt2(const Queen &x, const Queen &y) {
100     return x.c == y.c;
101 }
102 
103 // 左斜线方向计数辅助函数
104 inline bool cnt3(const Queen &x, const Queen &y) {
105     return x.r - x.c == y.r - y.c;
106 }
107 
108 // 右斜线方向计数辅助函数
109 inline bool cnt4(const Queen &x, const Queen &y) {
110     return x.r + x.c == y.r + y.c;
111 }
112 
113 int n, m;
114 Queen Q[maxN];
115 int ans[9];
116 
117 // 方向计数函数 
118 void CountDir(bool (*fcnt)(const Queen&,const Queen&)) {
119     //cout << endl;
120     //For(i, 1, m) cout << MP(Q[i].r, Q[i].c);
121     int cnt = 1;
122     For(i, 2, m) {
123         if(fcnt(Q[i], Q[i - 1])) {
124             if(cnt == 1) Q[i - 1].cnt += 1;
125             else Q[i - 1].cnt += 2;
126             ++cnt;
127         }
128         else {
129             if(cnt > 1)Q[i - 1].cnt += 1;
130             cnt = 1;
131         } 
132     }
133     if(cnt > 1) Q[m].cnt += 1;
134     //For(i, 1, m) cout << Q[i].cnt << " ";
135     //cout << endl;
136 }
137 
138 int main(){
139     INIT();
140     cin >> n >> m;
141     For(i, 1, m) cin >> Q[i].r >> Q[i].c;
142     
143     // 处理左右 
144     sort(Q + 1, Q + m + 1, cmp1);
145     CountDir(cnt1);
146     // 处理上下 
147     sort(Q + 1, Q + m + 1, cmp2);
148     CountDir(cnt2);
149     // 处理左上,左下 
150     sort(Q + 1, Q + m + 1, cmp3);
151     CountDir(cnt3);
152     // 处理右上,右下 
153     sort(Q + 1, Q + m + 1, cmp4);
154     CountDir(cnt4);
155     
156     For(i, 1, m) ++ans[Q[i].cnt];
157     Rep(i, 9) cout << ans[i] << " ";
158     cout << endl;
159     return 0;
160 }
View Code

 

posted @ 2019-04-21 00:01  梦樱羽  阅读(171)  评论(0)    收藏  举报
Live2D