답안 #497898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497898 2021-12-24T04:33:32 Z AQ0212 Chessboard (IZhO18_chessboard) C++17
100 / 100
322 ms 5816 KB
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
 
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(x) x.begin(), x.end()
 
using namespace std;
 
ll inf2 = 3e18;
 
ll x1[ 100111 ], x2[ 100111 ];
ll y11[ 100111 ], y2[ 100111 ], cnt_move[ 100111 ], ans = inf2;
vector < ll > divv;
map < ll , ll > mp;
 
int main(){
	ll n, m;
 
	scanf("%lld%lld", &n, &m);
 
	for(int i = 1; i <= m; i ++){
		scanf("%lld%lld%lld%lld", &x1[ i ], &y11[ i ], &x2[ i ], &y2[ i ]);
	}
 
	divv.pb(1);
	for(int i = 2; i <= ((ll)ceil(sqrt(n))); i ++){
		if(n % i == 0 && !mp[ i ] && i != n){
			divv.pb(i);
			mp[ i ] ++;
			if(!mp[ n / i ]){
				divv.pb(n / i);
				mp[ n / i ] ++;
			}
		}
	}
 
	for(int i = 0; i < divv.size(); i ++){
		ll sqrs = n / divv[ i ];
		cnt_move[ i ] = (sqrs & 1) ? ((((sqrs >> 1) * ((sqrs >> 1) + 1)) << 1) * (divv[ i ] * divv[ i ])) : (sqrs >> 1) * (sqrs) * (divv[ i ] * divv[ i ]);
		cnt_move[ i ] = (n * n) - cnt_move[ i ];
	}
 
	for(int i = 1; i <= m; i ++){
		for(int j = 0; j < divv.size(); j ++){
			ll xstart = (x1[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
			ll ystart = (y11[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
			ll xend = x2[ i ] / (divv[ j ] * 2) * divv[ j ];
			ll yend = y2[ i ] / (divv[ j ] * 2) * divv[ j ];

			xstart += min((x1[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
			ystart += min((y11[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
			xend += min(x2[ i ] % (divv[ j ] * 2), divv[ j ]);
			yend += min(y2[ i ] % (divv[ j ] * 2), divv[ j ]);
			ll xtot = xend - xstart;
			ll ytot = yend - ystart;
			
			ll xadd = (x2[ i ] - x1[ i ] + 1) - xtot;
			ll yadd = (y2[ i ] - y11[ i ] + 1) - ytot;

			cnt_move[ j ] -= (xtot * ytot) + (xadd * yadd);
			cnt_move[ j ] += (xtot * yadd) + (xadd * ytot);
		}
	}
	
	for(int i = 0; i < divv.size(); i ++){
		ans = min(ans, min(cnt_move[ i ], (n * n) - cnt_move[ i ]));
	}
	printf("%lld", ans);
}

/*

6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6

6 5
1 2 1 2
5 5 5 5
3 3 4 4
2 1 2 1
3 6 3 6

*/

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(int i = 0; i < divv.size(); i ++){
      |                 ~~^~~~~~~~~~~~~
chessboard.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int j = 0; j < divv.size(); j ++){
      |                  ~~^~~~~~~~~~~~~
chessboard.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |  for(int i = 0; i < divv.size(); i ++){
      |                 ~~^~~~~~~~~~~~~
chessboard.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |  scanf("%lld%lld", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
chessboard.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |   scanf("%lld%lld%lld%lld", &x1[ i ], &y11[ i ], &x2[ i ], &y2[ i ]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 3740 KB Output is correct
2 Correct 6 ms 1136 KB Output is correct
3 Correct 14 ms 2380 KB Output is correct
4 Correct 16 ms 2520 KB Output is correct
5 Correct 18 ms 3244 KB Output is correct
6 Correct 13 ms 2124 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 11 ms 2196 KB Output is correct
9 Correct 31 ms 5152 KB Output is correct
10 Correct 16 ms 2936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 10 ms 1516 KB Output is correct
17 Correct 25 ms 4276 KB Output is correct
18 Correct 41 ms 4960 KB Output is correct
19 Correct 71 ms 4388 KB Output is correct
20 Correct 81 ms 4900 KB Output is correct
21 Correct 23 ms 4008 KB Output is correct
22 Correct 1 ms 308 KB Output is correct
23 Correct 21 ms 2244 KB Output is correct
24 Correct 31 ms 4516 KB Output is correct
25 Correct 4 ms 588 KB Output is correct
26 Correct 19 ms 2904 KB Output is correct
27 Correct 24 ms 3408 KB Output is correct
28 Correct 31 ms 4724 KB Output is correct
29 Correct 10 ms 1792 KB Output is correct
30 Correct 2 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 3740 KB Output is correct
2 Correct 6 ms 1136 KB Output is correct
3 Correct 14 ms 2380 KB Output is correct
4 Correct 16 ms 2520 KB Output is correct
5 Correct 18 ms 3244 KB Output is correct
6 Correct 13 ms 2124 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 11 ms 2196 KB Output is correct
9 Correct 31 ms 5152 KB Output is correct
10 Correct 16 ms 2936 KB Output is correct
11 Correct 0 ms 288 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 280 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 296 KB Output is correct
21 Correct 1 ms 292 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 296 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 10 ms 1516 KB Output is correct
27 Correct 25 ms 4276 KB Output is correct
28 Correct 41 ms 4960 KB Output is correct
29 Correct 71 ms 4388 KB Output is correct
30 Correct 81 ms 4900 KB Output is correct
31 Correct 23 ms 4008 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 21 ms 2244 KB Output is correct
34 Correct 31 ms 4516 KB Output is correct
35 Correct 4 ms 588 KB Output is correct
36 Correct 19 ms 2904 KB Output is correct
37 Correct 24 ms 3408 KB Output is correct
38 Correct 31 ms 4724 KB Output is correct
39 Correct 10 ms 1792 KB Output is correct
40 Correct 2 ms 428 KB Output is correct
41 Correct 66 ms 4788 KB Output is correct
42 Correct 39 ms 5440 KB Output is correct
43 Correct 46 ms 4792 KB Output is correct
44 Correct 37 ms 5204 KB Output is correct
45 Correct 31 ms 5680 KB Output is correct
46 Correct 77 ms 5408 KB Output is correct
47 Correct 31 ms 5012 KB Output is correct
48 Correct 45 ms 5028 KB Output is correct
49 Correct 33 ms 4844 KB Output is correct
50 Correct 296 ms 5292 KB Output is correct
51 Correct 313 ms 5668 KB Output is correct
52 Correct 285 ms 5264 KB Output is correct
53 Correct 305 ms 5512 KB Output is correct
54 Correct 306 ms 5192 KB Output is correct
55 Correct 313 ms 5716 KB Output is correct
56 Correct 288 ms 4928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 21 ms 3740 KB Output is correct
10 Correct 6 ms 1136 KB Output is correct
11 Correct 14 ms 2380 KB Output is correct
12 Correct 16 ms 2520 KB Output is correct
13 Correct 18 ms 3244 KB Output is correct
14 Correct 13 ms 2124 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 11 ms 2196 KB Output is correct
17 Correct 31 ms 5152 KB Output is correct
18 Correct 16 ms 2936 KB Output is correct
19 Correct 0 ms 288 KB Output is correct
20 Correct 0 ms 208 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 280 KB Output is correct
24 Correct 1 ms 296 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 296 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 296 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 10 ms 1516 KB Output is correct
35 Correct 25 ms 4276 KB Output is correct
36 Correct 41 ms 4960 KB Output is correct
37 Correct 71 ms 4388 KB Output is correct
38 Correct 81 ms 4900 KB Output is correct
39 Correct 23 ms 4008 KB Output is correct
40 Correct 1 ms 308 KB Output is correct
41 Correct 21 ms 2244 KB Output is correct
42 Correct 31 ms 4516 KB Output is correct
43 Correct 4 ms 588 KB Output is correct
44 Correct 19 ms 2904 KB Output is correct
45 Correct 24 ms 3408 KB Output is correct
46 Correct 31 ms 4724 KB Output is correct
47 Correct 10 ms 1792 KB Output is correct
48 Correct 2 ms 428 KB Output is correct
49 Correct 66 ms 4788 KB Output is correct
50 Correct 39 ms 5440 KB Output is correct
51 Correct 46 ms 4792 KB Output is correct
52 Correct 37 ms 5204 KB Output is correct
53 Correct 31 ms 5680 KB Output is correct
54 Correct 77 ms 5408 KB Output is correct
55 Correct 31 ms 5012 KB Output is correct
56 Correct 45 ms 5028 KB Output is correct
57 Correct 33 ms 4844 KB Output is correct
58 Correct 296 ms 5292 KB Output is correct
59 Correct 313 ms 5668 KB Output is correct
60 Correct 285 ms 5264 KB Output is correct
61 Correct 305 ms 5512 KB Output is correct
62 Correct 306 ms 5192 KB Output is correct
63 Correct 313 ms 5716 KB Output is correct
64 Correct 288 ms 4928 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 307 ms 5452 KB Output is correct
68 Correct 302 ms 5432 KB Output is correct
69 Correct 272 ms 4784 KB Output is correct
70 Correct 287 ms 5180 KB Output is correct
71 Correct 302 ms 5136 KB Output is correct
72 Correct 283 ms 5104 KB Output is correct
73 Correct 280 ms 4988 KB Output is correct
74 Correct 311 ms 5376 KB Output is correct
75 Correct 298 ms 5156 KB Output is correct
76 Correct 309 ms 5484 KB Output is correct
77 Correct 68 ms 5712 KB Output is correct
78 Correct 41 ms 5188 KB Output is correct
79 Correct 53 ms 4848 KB Output is correct
80 Correct 50 ms 5188 KB Output is correct
81 Correct 45 ms 4808 KB Output is correct
82 Correct 56 ms 5408 KB Output is correct
83 Correct 40 ms 5040 KB Output is correct
84 Correct 190 ms 5532 KB Output is correct
85 Correct 315 ms 5628 KB Output is correct
86 Correct 1 ms 296 KB Output is correct
87 Correct 1 ms 272 KB Output is correct
88 Correct 322 ms 5816 KB Output is correct
89 Correct 61 ms 1316 KB Output is correct
90 Correct 0 ms 284 KB Output is correct