【链接】h在这里写链接
【题意】
在这里写题意
【题解】
枚举每层有多少个公寓就好。
要注意,每次都要从1到100判断,一下那个公寓该不该出现在那一层。
多个答案,如果答案是一样的。也算是唯一的。
(虽然这时候楼层不唯一)
【错的次数】
0
【反思】
在这了写反思
【代码】
#include <bits/stdc++.h> using namespace std; const int N = 200; int n, m, cur = 0, idx = 0;; int should[N + 10]; void ok(int per) { int now = 1, cnt = 0;//now 是当前的楼层,cnt是当前楼层的公寓数目 int temp = 1; for (int dd = 1; dd <= 100; dd++) {//枚举第dd间房子在哪里 cnt++;//当前楼层的公寓数目递增。 if (cnt > per) {//如果公寓数目大于每层的楼层数 cnt = 1;//进入下一层 now++;//楼层个数递增 } if (should[dd] != 0 && should[dd] != now) return; if (dd == n) temp = now; //如果dd公寓不应该在第now层.就结束 } //是一个合法的分配 if (cur == 0) {//如果之前没有找到过合法的。 idx = temp;//第n个房子,它就在第now层 cur = 1;//找到的解数目为1 } else {//数目大于0了 if (cur == 1) {//如果只有一个解的话 if (idx == temp) return;//如果第n间房子的层数和当前一样.退出 } cur++;//否则,答案递增。 } } int main() { //freopen("F:\rush.txt", "r", stdin); ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; should[x] = y;//x号公寓房子啊第y层 } for (int i = 1; i <= 102; i++)//枚举每层有i间公寓 ok(i); if (cur > 1 || cur == 0)//如果解的个数太多,或没解。 cout << -1 << endl; else cout << idx << endl; return 0; }