Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <climits> #include <string> #include <map> #include <set> #include <list> #define ll long long #define fornum(A,B,C) for(A=B;A<C;A++) using namespace std; ///////////////////////////////////////////////////// #define MOD (ll)(1e9+7) #define Maxm(A, B) (A > B ? A : B) #define Minm(A, B) (A < B ? A : B) ll N,D,D2,x[101010],y[101010]; ll block[1010][1010], mksq[2020][2020]; bool mk[2020][2020],mkyl[2020],mkxl[2020]; ll maxx,cy,cx,minsq,addsq; ll ans; double aave; ll i,j,k,l; double abss(double a){ return a < 0 ? -a : a; } int main(){ scanf("%lld%lld", &N,&D); fornum(i,0,N){ scanf("%lld%lld", &x[i],&y[i]); block[y[i] % D][x[i] % D]++; } fornum(i,0,D){ fornum(j,0,D){ if(maxx<block[i][j]){ maxx=block[i][j]; } } } for (minsq = 0; minsq * minsq < maxx;minsq++) ; minsq--; //printf("%lld\n", minsq); D2 = D * 2; fornum(i,0,D2){ fornum(j,0,D2){ //printf("%lld ", block[i%D][j%D]); if(minsq*minsq<block[i%D][j%D]){ mk[i][j] = true; } if(minsq*minsq+minsq<block[i%D][j%D]){ mkyl[i] = true; mkxl[j] = true; } } //printf("\n"); } fornum(i,0,D2){ if(mkyl[i]){ continue; } fornum(j,0,D2){ if(mkxl[j]||mk[i][j]){ //printf("a"); continue; } mksq[i+1][j+1] = Minm(Minm(mksq[i][j+1],mksq[i+1][j]),mksq[i][j])+1; } } addsq = D; fornum(i,0,D2){ fornum(j,0,D2){ //printf("%lld ",mksq[i+1][j+1]); ll asq = D-mksq[i+1][j+1]; if(asq<addsq){ addsq = asq; } } //printf("\n"); } printf("%lld", (minsq)*D+addsq-1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<map> using namespace std; template<typename __T> inline void read(__T &x) { x=0; int f=1;char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)) {x=x*10+c-'0';c=getchar();} x*=f; } int n,k; int cn[1005][1005]; int ml=0; int ss1[2005][2005]; int ss2[2005][2005]; int cc1=0; int get1(int x1,int y1,int x2,int y2) { if(x2<x1 || y2<y1) return 0; int ans=ss1[x2][y2]; if(x1) ans-=ss1[x1-1][y2]; if(y1) ans-=ss1[x2][y1-1]; if(x1 && y1) ans+=ss1[x1-1][y1-1]; // cout<<"Q "<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<' '<<ans<<endl; return ans; } int get2(int x1,int y1,int x2,int y2) { if(x2<x1 || y2<y1) return 0; int ans=ss2[x2][y2]; if(x1) ans-=ss2[x1-1][y2]; if(y1) ans-=ss2[x2][y1-1]; if(x1 && y1) ans+=ss2[x1-1][y1-1]; return ans; } bool check(int x,int y,int d) { if(d<=0) return 0; // if(d==0) return get2(x,y,x+k-1,y+k-1)==0; if(get1(x,y,x+d-1,y+d-1)!=cc1) return 0; if(get2(x+d,y+d,x+k-1,y+k-1)!=0) return 0; return 1; } int main() { read(n); read(k); int a,b; for(int i=0;i<n;i++) { read(a); read(b); cn[a%k][b%k]++; ml=max(ml,cn[a%k][b%k]); } int gg=0; for(;gg*gg<ml;gg++); ml=gg; for(int i=0;i<k;i++) for(int j=0;j<k;j++) { if(cn[i][j]<=(ml-1)*(ml-1)) continue; if(cn[i][j]<=ml*(ml-1)) { ss2[i][j]=ss2[i+k][j]=ss2[i][j+k]=ss2[i+k][j+k]=1; // cout<<"HIT2 "<<i<<' '<<j<<endl; } else { ss1[i][j]=ss1[i+k][j]=ss1[i][j+k]=ss1[i+k][j+k]=1; // cout<<"HIT1 "<<i<<' '<<j<<endl; cc1++; } } for(int i=0;i<2*k;i++) for(int j=1;j<2*k;j++) ss1[i][j]+=ss1[i][j-1]; for(int j=0;j<2*k;j++) for(int i=1;i<2*k;i++) ss1[i][j]+=ss1[i-1][j]; for(int i=0;i<2*k;i++) for(int j=1;j<2*k;j++) ss2[i][j]+=ss2[i][j-1]; for(int j=0;j<2*k;j++) for(int i=1;i<2*k;i++) ss2[i][j]+=ss2[i-1][j]; int na=k; for(int i=0;i<k;i++) for(int j=0;j<k;j++) while(check(i,j,na-1)) na--; // cout<<"BS"<<ml<<endl; // cout<<"N"<<na<<endl; cout<<ml*k+na-k-1<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <random> using namespace std; typedef long long int ll; typedef pair<int, int> P; int main() { int n, d; cin>>n>>d; int ct[1000][1000]={}; for(int i=0; i<n; i++){ int x, y; cin>>x>>y; ct[x%d][y%d]++; } ll m1=1, m2=1e9; while(m1!=m2){ int m=(m1+m2)/2; bool ok=0; ll c[1000]; ll cm=m/d+1; int l=0; for(int k=0; k<d; k++){ if(k>m) c[k]=0; else c[k]=(m-k)/d+1; cm=min(c[k], cm); if(c[k]==m/d+1) l=k; } if(l==d-1){ ok=1; for(int i=0; i<d; i++){ for(int j=0; j<d; j++){ if(ct[i][j]>cm*cm){ ok=0; break; } } if(!ok) break; } if(ok) m2=m; else m1=m+1; continue; } bool nuee=0; int ctx[1001]={}, cty[1001]={}; int ct2[1001][1001]={}; for(int i=0; i<d; i++){ for(int j=0; j<d; j++){ if(ct[i][j]>(cm+1)*(cm+1)){ nuee=1; break; }else if(ct[i][j]>(cm+1)*cm){ ctx[i+1]++; cty[j+1]++; }else if(ct[i][j]>cm*cm){ ct2[i+1][j+1]++; } } if(nuee) break; } if(nuee){ m1=m+1; continue; } for(int i=1; i<=d; i++){ ctx[i]+=ctx[i-1]; cty[i]+=cty[i-1]; } for(int i=1; i<=d; i++){ for(int j=1; j<=d; j++){ ct2[i][j]=(ct2[i-1][j]+ct2[i][j-1]-ct2[i-1][j-1]+ct2[i][j]); } } for(int i=0; i<d; i++){ if(i+d-l-2<=d-1){ if(ctx[i]!=ctx[i+d-l-1]) continue; }else{ if(ctx[i]!=ctx[d] || ctx[(i+d-l-2)%d+1]!=0) continue; } for(int j=0; j<d; j++){ if(j+d-l-2<=d-1){ if(cty[j]!=cty[j+d-l-1]) continue; }else{ if(cty[j]!=cty[d] || cty[(j+d-l-2)%d+1]!=0) continue; } if(j+d-l-2<=d-1 && i+d-l-2<=d-1){ if(ct2[i+d-l-1][j+d-l-1]-ct2[i+d-l-1][j]-ct2[i][j+d-l-1]+ct2[i][j]>0) continue; }else if(i+d-l-2<=d-1){ if(ct2[i+d-l-1][d]-ct2[i+d-l-1][j]-ct2[i][d]+ct2[i][j]+ct2[i+d-l-1][(j+d-l-2)%d+1]-ct2[i][(j+d-l-2)%d+1]>0) continue; }else if(j+d-l-2<=d-1){ if(ct2[d][j+d-l-1]-ct2[d][j]-ct2[i][j+d-l-1]+ct2[i][j]+ct2[(i+d-l-2)%d+1][j+d-l-1]-ct2[(i+d-l-2)%d+1][j]>0) continue; }else{ if(ct2[(i+d-l-2)%d+1][(j+d-l-2)%d+1]+(ct2[d][(j+d-l-2)%d+1]-ct2[i][(j+d-l-2)%d+1])+(ct2[(i+d-l-2)%d+1][d]-ct2[(i+d-l-2)%d+1][j])+ct2[d][d]-ct2[i][d]-ct2[d][j]+ct2[i][j]>0) continue; } ok=1; break; } if(ok) break; } if(ok) m2=m; else m1=m+1; } cout<<m1<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "unordered_map" #include "unordered_set" #include "iomanip" #include "cmath" #include "random" #include "bitset" #include "cstdio" #include "numeric" #include "cassert" #include "ctime" using namespace std; constexpr long long int MOD = 1000000007; //constexpr int MOD = 1000000007; //constexpr int MOD = 998244353; //constexpr long long int MOD = 998244353; constexpr double EPS = 1e-8; int N, M, K, H, W, L, R; //long long int N, M, K, H, W, L, R; class Segment_Tree { vector<int>v; int num; int ret; int Update(int place) { if (place >= v.size() / 2) { return v[place]; } v[place] = max(Update(place * 2), Update(place * 2 + 1)); return v[place]; } public: Segment_Tree(int n) { n++; num = 1; while (num < n * 2) { num *= 2; } v.resize(num, -MOD); } void Insert(int place, long long int num, bool update) { place += v.size() / 2; v[place] = num; if (!update)return; place /= 2; while (place) { v[place] = max(v[place * 2], v[place * 2 + 1]); place /= 2; } } void TopDown() { Update(1); } int RMQ(int a, int b) { ret = INT_MIN; b++; for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) { if (a & 1)ret = max(ret, v[a++]); if (b & 1)ret = max(ret, v[--b]); } return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> K; vector<vector<int>>v(K, vector<int>(K)); for (int i = 0; i < N; i++) { cin >> W >> H; v[H%K][W%K]++; } vector<vector<int>>hbox(K); for (int i = 0; i < K; i++) { for (int j = 0; j < K; j++) { if (v[i][j])hbox[i].push_back(j); } } int mx = 0; for (auto i : v)for (auto j : i)mx = max(mx, j); L = (sqrt(mx) - 1)*K - 1; R = sqrt(mx)*K + K - 1; Segment_Tree r(K); vector<int>num(K); vector<int>rr(K); while (R - L > 1) { int mid = (R + L) / 2; bool flag = false; for (int i = 0; i < K; i++) { num[i] = (mid - i + K) / K; } for (int l = 0; l < K; l++) { int mi = MOD; for (int i = 0; i < K; i++) { rr[i] = 0; for (auto j : hbox[i]) { if (!num[i]) { rr[i] = MOD; break; } rr[i] = max(rr[i], (v[i][j] - 1) / num[i] * K + j + ((j < l) ? K : 0)); } } for (int i = 0; i < K; i++) { r.Insert(i, rr[i], false); } r.TopDown(); mi = min(mi, r.RMQ(0, K - 1)); if (mi - l <= mid) { flag = true; break; } if (0 == (mid + 1) % K)continue; for (int d = 0; d < K; d++) { num[d]--; int nx = (d + 1 + mid) % K; num[nx]++; if (d == K - 1)continue; int nr = 0; for (auto j : hbox[d]) { if (!num[d]) { nr = MOD; break; } nr = max(nr, (v[d][j] - 1) / num[d] * K + j + ((j < l) ? K : 0)); } r.Insert(d, nr, true); nr = 0; for (auto j : hbox[nx]) { if (!num[nx]) { nr = MOD; break; } nr = max(nr, (v[nx][j] - 1) / num[nx] * K + j + ((j < l) ? K : 0)); } r.Insert(nx, nr, true); mi = min(mi, r.RMQ(0, K - 1)); if (mi - l <= mid) { flag = true; break; } } if (flag)break; if (mi - l - 1 > mid) { l += mi - l - 1 - mid; } } if (flag) { R = mid; } else { L = mid; } } cout << R << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <algorithm> using namespace std; long long N, D, x[100009], y[100009], p[1009][1009], tabl[1000009], score[1009][1009], r[3][2009][2009], maxn = 0, ans = 1000000007; long long ranged(int ty, int px, int py, int qx, int qy) { return r[ty][px][py] + r[ty][qx][qy] - r[ty][px][qy] - r[ty][qx][py]; } bool solve(long long px, long long py, long long border) { border++; long long A1 = border / D, A2 = border % D; long long D1 = (maxn - 2); for (int i = 0; i < 3; i++) { long long p1 = ranged(i, px, py, px + A2, py + A2); if (p1 > 0) { D1 = maxn - i; break; } } long long D2 = (maxn - 2); for (int i = 0; i < 3; i++) { long long p1 = ranged(i, px + A2, py, px + D, py + A2); if (p1 > 0) { D2 = maxn - i; break; } } long long D3 = (maxn - 2); for (int i = 0; i < 3; i++) { long long p1 = ranged(i, px, py + A2, px + A2, py + D); if (p1 > 0) { D3 = maxn - i; break; } } long long D4 = (maxn - 2); for (int i = 0; i < 3; i++) { long long p1 = ranged(i, px + A2, py + A2, px + D, py + D); if (p1 > 0) { D4 = maxn - i; break; } } bool flag = true; if (D1 > 2LL * A1 + 2LL) flag = false; if (D2 > 2LL * A1 + 1LL) flag = false; if (D3 > 2LL * A1 + 1LL) flag = false; if (D4 > 2LL * A1 + 0LL) flag = false; return flag; } int main() { cin >> N >> D; for (int i = 1; i <= N; i++) { cin >> x[i] >> y[i]; p[x[i] % D][y[i] % D]++; } for (int i = 0; i <= 500; i++) { for (int j = i * i + 1; j <= i * (i + 1); j++) tabl[j] = i * 2 + 1; for (int j = i * (i + 1) + 1; j <= (i + 1)*(i + 1); j++) tabl[j] = i * 2 + 2; } for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { score[i][j] = tabl[p[i][j]]; maxn = max(maxn, score[i][j]); } } for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (maxn - 2 <= score[i][j]) { r[maxn - score[i][j]][i + 1][j + 1]++; r[maxn - score[i][j]][i + D + 1][j + 1]++; r[maxn - score[i][j]][i + 1][j + D + 1]++; r[maxn - score[i][j]][i + D + 1][j + D + 1]++; } } } for (int i = 0; i < 3; i++) { for (int j = 0; j <= 2 * D + 1; j++) { for (int k = 1; k <= 2 * D + 1; k++) r[i][j][k] += r[i][j][k - 1]; } for (int j = 1; j <= 2 * D + 1; j++) { for (int k = 0; k <= 2 * D + 1; k++) r[i][j][k] += r[i][j - 1][k]; } } // ------------------ 探索フェーズ ----------------------- for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { long long L = 0, R = 350000, M, minx = (1LL << 30); for (int k = 0; k < 22; k++) { M = (L + R) / 2; bool I = solve(i, j, M); if (I == true) { minx = min(minx, M); R = M; } else { L = M; } } ans = min(ans, minx); } } cout << ans << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; using int64 = long long; const int INF = 1 << 30; int N, D; int ex[2000][2000]; int chokudai[2][2001][2001]; inline int get_rect(int t, int a, int b, int c, int d) { return chokudai[t][c][d] + chokudai[t][a][b] - chokudai[t][a][d] - chokudai[t][c][b]; } bool check(int t) { int a = t / D; bool all = true; for(int i = 0; i < 2 * D; i++) { for(int j = 0; j < 2 * D; j++) { if(ex[i][j] > 1LL * (a + 1) * (a + 1)) return false; all &= ex[i][j] <= 1LL * a * a; chokudai[0][i + 1][j + 1] = ex[i][j] > 1LL * a * a; chokudai[1][i + 1][j + 1] = ex[i][j] > 1LL * a * (a + 1); } } if(all) return true; for(int k = 0; k < 2; k++) { for(int i = 1; i <= 2 * D; i++) { for(int j = 1; j <= 2 * D; j++) { chokudai[k][i][j] += chokudai[k][i - 1][j]; chokudai[k][i][j] += chokudai[k][i][j - 1]; chokudai[k][i][j] -= chokudai[k][i - 1][j - 1]; } } } int b = t % D + 1; for(int i = 0; i < D; i++) { for(int j = 0; j < D; j++) { int add = 0; add += get_rect(0, i + b, j + b, i + D, j + D); add += get_rect(1, i, j, i + D, j + D); add -= get_rect(1, i, j, i + b, j + b); add -= get_rect(1, i + b, j + b, i + D, j + D); if(add == 0) return true; } } return false; } int main() { cin >> N >> D; for(int i = 0; i < N; i++) { int x, y; cin >> x >> y; x %= D; y %= D; ++ex[x][y]; ++ex[x][y + D]; ++ex[x + D][y]; ++ex[x + D][y + D]; } int ok = (1 << 30) - 1; for(int k = 29; k >= 0; k--) { if(check(ok ^ (1 << k))) ok ^= 1 << k; } cout << ok << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> #include <iostream> #include <string> #include <vector> #include <list> #include <utility> #include <algorithm> #include <functional> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <iomanip> #include <sstream> #include <bitset> #include <limits> #include <numeric> #include <valarray> #include <fstream> using namespace std; typedef unsigned int uint; typedef long long LL; typedef unsigned long long ULL; typedef pair<LL, LL> PP; #define REP(i, a, n) for(LL i = (a), i##_max = (n); i < i##_max; ++i) #define REM(i, a, n) for(LL i = (LL)(n) - 1, i##min = (a); i >= i##min; --i) #define ALL(arr) (arr).begin(), (arr).end() #define FLOAT fixed << setprecision(16) #define SPEEDUP {cin.tie(NULL); ios::sync_with_stdio(false);} const int INF = 0x3FFFFFFF; const LL INFLL = 0x3FFFFFFF3FFFFFFF; const double INFD = 1.0e+308; const string INFSTR = "\x7f"; const double EPS = 1.0e-9; void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; } template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; } template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); } template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); } template<class T> T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); } istream& Ignore(istream& ist) { string s; ist >> s; return ist; } bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; } template <class T> T Next() { T buf; cin >> buf; return buf; } #ifdef ONLY_MY_ENVIR #include "IntMod.h" #include "Union_Find.h" #include "Graph.h" #include "Range.h" #include "Global.h" #include "Flow_Solver.h" #include "Tree.h" #include "Suffix_Array.h" #include "Geometry.h" #include "Matrix.h" #include "Segment_Tree.h" #include "BIT.h" #include "Rational.h" #include "Position.h" #include "Factorization.h" #endif #ifdef __GNUC__ typedef __int128 LLL; istream& operator>> (istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; } ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; } #endif #if 1234567891 #include <array> #include <random> #include <unordered_set> #include <unordered_map> template<typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >; // template <class T> // auto Is(const T& value) { return [value](const auto& comparand) -> bool { return comparand == value; }; } #endif int N, D; int C[1000][1000]; int A[2001][2001]; int B[2001][2001]; int is_cat(int P[2001][2001], int u, int d, int l, int r) { return P[u][l] - P[u][r] - P[d][l] + P[d][r] >= (d - u) * (r - l); } bool ok(int size) { int delta = D - size; REP(i, 0, D) { REP(j, 0, D) { if (!is_cat(A, i, i + delta, j, j + delta)) continue; if (!is_cat(B, i + delta, i + D, j, j + delta)) continue; if (!is_cat(B, i, i + delta, j + delta, j + D)) continue; return true; } } return false; } int main() { cin >> N >> D; REP(i, 0, N) { int x, y; scanf("%d %d", &x, &y); ++C[x % D][y % D]; } int cmx = 0; REP(i, 0, D) { REP(j, 0, D) { cmx = max(cmx, C[i][j]); } } int t = 0; while (t * t < cmx) { ++t; } --t; REP(i, 0, D) { REP(j, 0, D) { A[i + 1][j + 1] = A[i + 1][j + 1 + D] = A[i + 1 + D][j + 1] = A[i + 1 + D][j + 1 + D] = C[i][j] <= t * t; B[i + 1][j + 1] = B[i + 1][j + 1 + D] = B[i + 1 + D][j + 1] = B[i + 1 + D][j + 1 + D] = C[i][j] <= t * (t + 1); } } REP(i, 0, 2 * D + 1) { REP(j, 0, 2 * D) { A[i][j + 1] += A[i][j]; B[i][j + 1] += B[i][j]; } } REP(j, 0, 2 * D + 1) { REP(i, 0, 2 * D) { A[i + 1][j] += A[i][j]; B[i + 1][j] += B[i][j]; } } int lo = 0; int hi = D + 1; while (hi - lo > 1) { int mid = (lo + hi) / 2; if (ok(mid)) { hi = mid; } else { lo = mid; } } cout << t * D + hi - 1 << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define y1 __zzd001 using namespace std; const int N=2005; int n,d,v[N][N],s[3][N][N]; int S(int x1,int y1,int x2,int y2,int t){ return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x){ int r=x%d; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++){ int x1=i,y1=j,x2=x1+r,y2=y1+r,x3=x1+d-1,y3=y1+d-1; if (!S(x1,y2+1,x3,y3,1)&&!S(x2+1,y1,x3,y3,1)&&!S(x2+1,y2+1,x3,y3,0)) return 1; } return 0; } int main(){ scanf("%d%d",&n,&d); memset(v,0,sizeof v); while (n--){ int x,y; scanf("%d%d",&x,&y); v[x%d+1][y%d+1]++; } int Mx=0,t; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j],Mx=max(Mx,v[i][j]); for (t=0;(t+1)*(t+1)<Mx;t++); int l[3]={t*t+1,t*(t+1)+1,(int)1e9}; for (int i=1;i<=d*2;i++) for (int j=1;j<=d*2;j++) for (int t=0;t<2;t++){ s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if (l[t]<=v[i][j]&&v[i][j]<l[t+1]) s[t][i][j]++; } int L=t*d,R=t*d+d-1,ans=R,mid; while (L<=R) if (check(mid=(L+R)>>1)) R=mid-1,ans=mid; else L=mid+1; cout << ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define int long long #define INF 1000000000000000000 using namespace std; typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> Pii; #define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl; template<typename T> void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; } void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} } signed main(){ int N, D; cin >> N >> D; vec x(N), y(N); REP(i, N) cin >> x[i] >> y[i]; mat m(D, vec(D, 0)); REP(i, N) m[x[i] % D][y[i] % D]++; int M = -1; REP(i, D) REP(j, D) M = max(M, m[i][j]); int A = 0; while(A * A < M) A++; //cout << A << endl; int r1 = -1, r2 = D - 1; while(r2 - r1 > 1){ int r = (r1 + r2 + 1) / 2; mat cnt(2 * D, vec(2 * D, 0)); REP(i, D){ REP(j, D){ if(m[i][j] <= (A - 1) * (A - 1)){ cnt[i][j]++; cnt[i + D][j]--; cnt[i][j + D]--; cnt[i + D][j + D]++; }else if(m[i][j] <= A * (A - 1)){ //cnt[i][j]++; cnt[i + D][j]--; cnt[i][j + D]--; cnt[i + D][j + D]++; //cnt[i][j]--; cnt[i + D - 1 - r][j]++; cnt[i][j + D - 1 - r]++; cnt[i + D - 1 - r][j + D - 1 - r]--; }else{ cnt[i - r + D - 1][j - r + D - 1]++; cnt[i + D][j - r + D - 1]--; cnt[i - r + D - 1][j + D]--; cnt[i + D][j + D]++; } } } REP(i, 2 * D) FOR(j, 1, 2 * D) cnt[i][j] += cnt[i][j - 1]; REP(j, 2 * D) FOR(i, 1, 2 * D) cnt[i][j] += cnt[i - 1][j]; bool f = false; REP(i, D){ REP(j, D){ if(cnt[i][j] + cnt[i][j + D] + cnt[i + D][j] + cnt[i + D][j + D] == D * D) f = true; } } if(f) r2 = r; else r1 = r; } cout << D * (A - 1) + r2; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define mk make_pair using namespace std; typedef long long ll; const int N = 2e3 + 5; const int mod = 1e9 + 7; int n, d, cnt[N][N]; int x[N * 100], y[N * 100]; int type[N][N]; int sum[N][N][2]; inline int S(int x, int y, int z) { if (x < 0 || y < 0) return 0; return sum[x][y][z]; } bool check(int k) { ++k; int x = k / d; ll t1 = (x + 1) * (x + 1ll); ll t2 = (x + 1ll) * x; ll t3 = (ll)x * x; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { if (cnt[i][j] <= t3) { type[i][j] = 2; } else if (cnt[i][j] <= t2) { type[i][j] = 0; } else if (cnt[i][j] <= t1) { type[i][j] = 1; } else { return 0; } //cout << type[i][j] << ","; } //cout << endl; } for (int i = 0; i < 2 * d; ++i) { for (int j = 0; j < 2 * d; ++j) { for (int k = 0; k < 2; ++k) { sum[i][j][k] = 0; if (i) sum[i][j][k] += sum[i - 1][j][k]; if (j) sum[i][j][k] += sum[i][j - 1][k]; if (i && j) sum[i][j][k] -= sum[i - 1][j - 1][k]; if (k == type[i % d][j % d]) sum[i][j][k]++; } } } int tmp, tmp1, tmp2, p = k % d; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { tmp = S(i + p - 1, j + p - 1, 1) - S(i + p - 1, j - 1, 1) - S(i - 1, j + p - 1, 1) + S(i - 1, j - 1, 1); //cout << i << " " << j << " " << tmp << endl; if (sum[d - 1][d - 1][1] != tmp) continue; tmp1 = S(d - 1, j + p - 1, 0) - S(d - 1, j - 1, 0); tmp2 = S(i + p - 1, d - 1, 0) - S(i - 1, d - 1, 0); tmp = S(i + p - 1, j + p - 1, 0) - S(i + p - 1, j - 1, 0) - S(i - 1, j + p - 1, 0) + S(i - 1, j - 1, 0); if (tmp1 + tmp2 - tmp != sum[d - 1][d - 1][0]) continue; return 1; } } return 0; } int main() { scanf("%d %d", &n, &d); for (int i = 1; i <= n; ++i) { scanf("%d %d", x + i, y + i); cnt[x[i] % d][y[i] % d]++; } //check(4);return 0; int l = 1, r = 1e6 + 5; while (l < r) { int mid = l + r >> 1; //cout << mid << endl; if (l + 1 == r) { if (check(l)) --r; else ++l; break; } if (check(mid)) r = mid; else l = mid; } cout << l; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;} template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;} const int64 mod = 1e9+7; int64 minsq(int64 x){ // x個を正方形内に入れる最小の正方形9 -> 3, 10 -> 4 int64 a = sqrt(x); if(a*a >= x) return a; return a+1; } int main(void){ int32 N, D; cin >> N >> D; vector<int64> x(N), y(N); REP(i, N) cin >> x[i] >> y[i]; vector<vector<int32>> cnt(3030, vector<int32>(3030, 0)); vector<vector<int32>> ok1(3030, vector<int32>(3030, 0)); vector<vector<int32>> ok2(3030, vector<int32>(3030, 0)); int64 d = 0; REP(i, N){ cnt[x[i]%D][y[i]%D]++; chmax(d, cnt[x[i]%D][y[i]%D]); } using T = tuple<int64, int64, int64>; vector<T> v; sort(all(v), greater<T>()); d = minsq(d); REP(i, D){ REP(j, D){ REP(k, 3){ REP(l, 3){ if(cnt[i][j] > (d-1)*(d-1)) ok1[i+D*k][j+D*l]++; if(cnt[i][j] > d*(d-1)) ok2[i+D*k][j+D*l]++; } } } } REP(i, 3*D){ REP(j, 3*D){ ok1[i][j+1] += ok1[i][j]; ok2[i][j+1] += ok2[i][j]; } } REP(j, 3*D){ REP(i, 3*D){ ok1[i+1][j] += ok1[i][j]; ok2[i+1][j] += ok2[i][j]; } } auto sum1 = [&](int32 x1, int32 y1, int32 x2, int32 y2){ int32 res = ok1[x2][y2]; if(x1 > 0) res -= ok1[x1-1][y2]; if(y1 > 0) res -= ok1[x2][y1-1]; if(x1 > 0 && y1 > 0) res += ok1[x1-1][y1-1]; return res; }; auto sum2 = [&](int32 x1, int32 y1, int32 x2, int32 y2){ int32 res = ok2[x2][y2]; if(x1 > 0) res -= ok2[x1-1][y2]; if(y1 > 0) res -= ok2[x2][y1-1]; if(x1 > 0 && y1 > 0) res += ok2[x1-1][y1-1]; return res; }; int32 l = -1, r = D+1, m; auto ok = [&](int32 b){ REP(x, D){ REP(y, D){ bool ok = 1; // cout << x << " " << y << " " << b << endl; // cout << sum1(x, y+b+1, x+b, y+D-1) << " " << sum1(x+b+1, y, x+D-1, y+b) << " " << sum2(x+b+1, y+b+1, x+D-1, y+D-1) << endl; if(sum1(x+b+1, y+b+1, x+D-1, y+D-1) > 0) ok = 0; if(sum2(x+b+1, y, x+D-1, y+b) > 0) ok = 0; if(sum2(x, y+b+1, x+b, y+D-1) > 0) ok = 0; if(ok) return 1; } } return 0; }; while(r-l > 1){ m = (l+r)/2; if(ok(m)) r = m; else l = m; } cout << (d-1)*D+r << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(),v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL<<(n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; #define mdebug(m) cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; } template<typename T> void Out(T x) { cout << x << endl; } template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; //int dx[4] = {1,0,-1,0}; //int dy[4] = {0,1,0,-1}; //char d[4] = {'D','R','U','L'}; const int mod = 1000000007; //const int mod = 998244353; //#define Add(x, y) x = (x + (y)) % mod //#define Mult(x, y) x = (x * (y)) % mod int usqrt(int x){ FOR(i, 1, x + 1) if(i * i >= x) return i; return -1; } signed main(){ int N, D; cin >> N >> D; map<Pii, int> cnt; REP(i, N){ int x, y; cin >> x >> y; cnt[Pii(x % D, y % D)] += 1; } int S0 = -1, S1 = bit(20) - 1; mat a(2 * D, vec(2 * D, 0)); while(S1 - S0 > 1){ int S = (S0 + S1) / 2, T = S / D, r = S % D; REP(i, 2 * D) fill(all(a[i]), 0); for(PPi t: cnt){ int x = t.f.f, y = t.f.s, n = t.s; if(T * T >= n){ a[D - 1][D - 1]++; a[2 * D - 1][D - 1]--; a[D - 1][2 * D - 1]--; a[2 * D - 1][2 * D - 1]++; }else if(T * (T + 1) >= n){ a[x - r + D - 1][D - 1]++; a[x + D][D - 1]--; a[x - r + D - 1][2 * D - 1]--; a[x + D][2 * D - 1]++; a[D - 1][y - r + D - 1]++; a[D - 1][y + D]--; a[2 * D - 1][y - r + D - 1]--; a[2 * D - 1][y + D]++; a[x - r + D - 1][y - r + D - 1]--; a[x + D][y - r + D - 1]++; a[x - r + D - 1][y + D]++; a[x + D][y + D]--; }else if((T + 1) * (T + 1) >= n){ a[x - r + D - 1][y - r + D - 1]++; a[x + D][y - r + D - 1]--; a[x - r + D - 1][y + D]--; a[x + D][y + D]++; } } REP(i, 2 * D) REP(j, 2 * D - 1) a[i][j + 1] += a[i][j]; REP(j, 2 * D) REP(i, 2 * D - 1) a[i + 1][j] += a[i][j]; bool f = false; REP(i, D) REP(j, D) if(a[i][j] + a[i + D][j] + a[i][j + D] + a[i + D][j + D] >= SZ(cnt)) f = true; if(f) S1 = S; else S0 = S; } Out(S1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; template <typename T> class imos2 { int n, m; vector<vector<T>> v; public: imos2(int n_, int m_) : n(n_), m(m_), v(n + 1, vector<T>(m + 1)) {} T& val(int i, int j) { assert(0 <= i && i < n && 0 <= j && j < m); return v[i + 1][j + 1]; } void add(int li, int lj, int ri, int rj, T x) { assert(0 <= li && li <= ri && ri <= n); assert(0 <= lj && lj <= rj && rj <= m); val(li, lj) += x; if (ri < n) val(ri, lj) -= x; if (rj < m) val(li, rj) -= x; if (ri < n && rj < m) val(ri, rj) += x; } void build() { for (int i = 1; i <= n; i++) for (int j = 0; j <= m; j++) v[i][j] += v[i - 1][j]; for (int i = 0; i <= n; i++) for (int j = 1; j <= m; j++) v[i][j] += v[i][j - 1]; } T sum(int li, int lj, int ri, int rj) { assert(0 <= li && li <= ri && ri <= n); assert(0 <= lj && lj <= rj && rj <= m); return v[ri][rj] - v[ri][lj] - v[li][rj] + v[li][lj]; } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int N, D; cin >> N >> D; vector<vector<int>> cnt(D, vector<int>(D)); for (int i = 0; i < N; i++) { int x, y; cin >> x >> y; cnt[x % D][y % D]++; } int ma = 0; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { ma = max(ma, cnt[i][j]); } } int x = 0; while ((x + 1) * (x + 1) < ma) ++x; imos2<int> all1(D * 2, D * 2), all2(D * 2, D * 2); for (int i = 0; i < D * 2; i++) { for (int j = 0; j < D * 2; j++) { all1.val(i, j) = cnt[i % D][j % D] > x * x && cnt[i % D][j % D] <= x * (x + 1); all2.val(i, j) = cnt[i % D][j % D] > x * (x + 1); } } all1.build(); all2.build(); int sum2 = all2.sum(0, 0, D, D); int mi = D - 1; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { int lb = 0, ub = D; while (ub - lb > 1) { int c = (lb + ub) >> 1; if (all2.sum(i, j, i + c, j + c) == sum2 && all1.sum(i + c, j + c, i + D, j + D) == 0) { ub = c; } else { lb = c; } } mi = min(mi, lb); } } cout << x * D + mi << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define rep(i, a, b) for(ll i = ll(a); i < ll(b); i++) #define rer(i, a, b) for(ll i = ll(a) - 1; i >= ll(b); i--) #define sz(v) (int)(v).size() #define pb push_back #define sc second #define fr first #define sor(v) sort(v.begin(),v.end()) #define rev(s) reverse(s.begin(),s.end()) #define lb(vec,a) lower_bound(vec.begin(),vec.end(),a) #define ub(vec,a) upper_bound(vec.begin(),vec.end(),a) #define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end()) using namespace std; typedef long long int ll; typedef pair <int, int> P; const ll MOD=1000000007; ll N, D; ll x[100000], y[100000]; ll d[2000][2000]; ll s[2001][2001], ss[2001][2001], t[2001][2001], tt[2001][2001]; bool check(ll x, ll y, ll c){ bool t=true; if(ss[x+c][y+c]+ss[x][y]-ss[x+c][y]-ss[x][y+c]!=ss[D][D]+ss[0][0]-ss[0][D]-ss[D][0]) t=false; if(tt[x+D][y+D]+tt[x+c][y+c]-tt[x+D][y+c]-tt[x+c][y+D]!=(D-c)*(D-c)) t=false; return t; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>N>>D; rep(i,0,N){ cin>>x[i]>>y[i]; d[x[i]%D][y[i]%D]++; d[x[i]%D+D][y[i]%D]++; d[x[i]%D][y[i]%D+D]++; d[x[i]%D+D][y[i]%D+D]++; } ll T=0; rep(i,0,D){ T=max(T,*max_element(d[i], d[i]+D)); } T=ll(sqrt(T-1)); ll ans=D*T; rep(i,0,2*D){ rep(j,0,2*D){ if(d[i][j]>T*(T+1)){ s[i][j+1]=s[i][j]+1; } else{ s[i][j+1]=s[i][j]; } if(d[i][j]<=T*T){ t[i][j+1]=t[i][j]+1; } else{ t[i][j+1]=t[i][j]; } } } rep(i,0,2*D){ rep(j,1,2*D){ ss[i+1][j]=ss[i][j]+s[i][j]; tt[i+1][j]=tt[i][j]+t[i][j]; } } ll ng=-1, ok=D; while(ok-ng>1){ ll mid=(ok+ng)/2; bool t=false; rep(i,0,D){ rep(j,0,D){ if(check(i,j,mid)) t=true; } } if(t) ok=mid; else ng=mid; } cout <<ans+ok-1<<"\n"; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
//#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #define owo(i,a, b) for(int i=(a);i<(b); ++i) #define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i) #define senpai push_back #define ttgl pair<int, int> #define ayaya cout<<"ayaya~"<<endl using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; gpu_hash_table<int, int> mp; #define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update> */ using ll = long long; using ld = long double; const ll MOD = 924844033; const ll root = 62; int gcd(int a,int b){return b?gcd(b,a%b):a;} ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;} ll modInv(ll a){return binpow(a, MOD-2);} const double PI = acos(-1); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const ll NINFLL = 0xc0c0c0c0c0c0c0c0; const int mxN = 100001; const int mxD = 1001; int cnt[mxD][mxD]; int psum1[2*mxD][2*mxD]; int psum2[2*mxD][2*mxD]; //bigger to deal with negative shifts int n, d; int mxk = 0; int get1(int x1, int y1, int x2, int y2) { return psum1[x2][y2] + psum1[x1][y1] - psum1[x1][y2] - psum1[x2][y1]; } int get2(int x1, int y1, int x2, int y2) { return psum2[x2][y2] + psum2[x1][y1] - psum2[x1][y2] - psum2[x2][y1]; } bool check(int over) { int k = psum1[d][d]; owo(i, 0, d) { owo(j, 0, d) { //cout<<over<<" "<<i<<" "<<j<<" "<<get1(i, j, i+over, j+over)<<" "<<get2(i+over, j+over, i+d, j+d)<<"\n"; if(get1(i, j, i+over, j+over)==k&&get2(i+over, j+over, i+d, j+d)==0)return true; } } //cout<<over<<"\n"; return false; } int main() { //freopen("file.in", "r", stdin); //freopen("file.out", "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin.tie(0)->sync_with_stdio(0); cin>>n>>d; int a, b; owo(i, 0, n) { cin>>a>>b; cnt[b%d][a%d]++; } owo(i, 0, d) { owo(j, 0, d) { mxk = max(mxk, cnt[i][j]); } } mxk = (int)(sqrt(mxk)-eps); owo(i, 0, d) { owo(j, 0, d) { //cout<<cnt[i][j]<<"\n"; if(cnt[i][j]>(mxk+1)*(mxk))cnt[i][j] = 0; else if(cnt[i][j]>mxk*mxk)cnt[i][j] = 1; else cnt[i][j] = 2; } } owo(i, 0, 2*d) { owo(j, 0, 2*d) { psum1[i+1][j+1] = psum1[i+1][j]; psum2[i+1][j+1] = psum2[i+1][j]; if(cnt[i%d][j%d]==0) { psum1[i+1][j+1]++; } if(cnt[i%d][j%d]==1||cnt[i%d][j%d]==0) { psum2[i+1][j+1]++; //cout<<i<<" "<<j<<"\n"; } } } owo(i, 0, 2*d) { owo(j, 0, 2*d) { psum1[i+1][j+1] = psum1[i+1][j+1] + psum1[i][j+1]; psum2[i+1][j+1] = psum2[i+1][j+1] + psum2[i][j+1]; } } owo(i, 0, 2*d) owo(j, 0, 2*d) { //cout<<i<<" "<<j<<" "<<psum1[i][j]<<" "<<psum2[i][j]<<"\n"; } int l = 0; int r = d; while(l<r) { int mid = l+(r-l)/2; if(check(mid))r = mid; else l = mid+1; } cout<<(mxk*d+l-1)<<"\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(), D = ni(); int[][] f = new int[D][D]; for(int i = 0;i < n;i++){ int x = ni(), y = ni(); x %= D; y %= D; if(x < 0)x += D; if(y < 0)y += D; f[x][y]++; } int low = -1, high = D*1000; while(high - low > 1){ int h = high+low>>1; if(ok(h, f)){ high = h; }else{ low = h; } } out.println(high); } static boolean ok(int h, int[][] f) { int D = f.length; // f <= ((h-dx)/D+1)*((h-dy)/D+1) int[][] imos = new int[2*D+1][2*D+1]; int dx = h%D; // [0,dx],[0,dx] for(int i = 0;i < D;i++){ for(int j = 0;j < D;j++){ if((long)(h/D+1)*(h/D+1) >= f[i][j]){ imos[D+i-dx][D+j-dx]++; imos[D+i+1][D+j-dx]--; imos[D+i-dx][D+j+1]--; imos[D+i+1][D+j+1]++; } if((long)(h/D+1)*(h/D) >= f[i][j]){ imos[i+1][D+j-dx]++; imos[D+i-dx][D+j-dx]--; imos[i+1][D+j+1]--; imos[D+i-dx][D+j+1]++; imos[D+i-dx][j+1]++; imos[D+i+1][j+1]--; imos[D+i-dx][D+j-dx]--; imos[D+i+1][D+j-dx]++; } if((long)(h/D)*(h/D) >= f[i][j]){ imos[i+1][j+1]++; imos[D+i-dx][j+1]--; imos[i+1][D+j-dx]--; imos[D+i-dx][D+j-dx]++; } } } for(int i = 0;i <= 2*D;i++){ for(int j = 0;j <= 2*D;j++){ if(i > 0)imos[i][j] += imos[i-1][j]; if(j > 0)imos[i][j] += imos[i][j-1]; if(i > 0 && j > 0)imos[i][j] -= imos[i-1][j-1]; } } for(int i = 0;i < D;i++){ for(int j = 0;j < D;j++){ if(imos[i][j] + imos[i+D][j] + imos[i][j+D] + imos[i+D][j+D] == D*D){ return true; } } } return false; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
JAVA
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using lint = long long int; using ulint = unsigned long long int; template<class T = int> using V = vector<T>; template<class T = int> using VV = V< V<T> >; template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); } template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, d; cin >> n >> d; VV<> c; assign(c, d, d, 0); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; ++c[x % d][y % d]; } int c_max = 0; for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) c_max = max(c_max, c[i][j]); int k = sqrt(c_max) + 10; while (k * k >= c_max) --k; VV<> a1, a2; assign(a1, d, d, 0), assign(a2, d, d, 0); for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { if (c[i][j] > k * (k + 1)) ++a1[i][j]; if (c[i][j] > k * k) ++a2[i][j]; } VV<> c1, c2; assign(c1, d + 1, d + 1, 0), assign(c2, d + 1, d + 1, 0); for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { c1[i + 1][j + 1] = c1[i + 1][j] + c1[i][j + 1] - c1[i][j] + a1[i][j]; c2[i + 1][j + 1] = c2[i + 1][j] + c2[i][j + 1] - c2[i][j] + a2[i][j]; } auto f1 = [&](int il, int ir, int jl, int jr) -> int { return c1[ir][jr] - c1[ir][jl] - c1[il][jr] + c1[il][jl]; }; auto s1 = [&](int il, int ir, int jl, int jr) -> int { if (il >= d) il -= d, ir -= d; if (jl >= d) jl -= d, jr -= d; int res = 0; res += f1(il, min(ir, d), jl, min(jr, d)); if (ir > d) res += f1(0, ir - d, jl, min(jr, d)); if (jr > d) res += f1(il, min(ir, d), 0, jr - d); if (ir > d and jr > d) res += f1(0, ir - d, 0, jr - d); return res; }; auto f2 = [&](int il, int ir, int jl, int jr) -> int { return c2[ir][jr] - c2[ir][jl] - c2[il][jr] + c2[il][jl]; }; auto s2 = [&](int il, int ir, int jl, int jr) -> int { if (il >= d) il -= d, ir -= d; if (jl >= d) jl -= d, jr -= d; int res = 0; res += f2(il, min(ir, d), jl, min(jr, d)); if (ir > d) res += f2(0, ir - d, jl, min(jr, d)); if (jr > d) res += f2(il, min(ir, d), 0, jr - d); if (ir > d and jr > d) res += f2(0, ir - d, 0, jr - d); return res; }; auto chk = [&](int x) -> bool { for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { if (s2(i + x + 1, i + d, j + x + 1, j + d) > 0) continue; if (s1(i + x + 1, i + d, j, j + x + 1) > 0) continue; if (s1(i, i + x + 1, j + x + 1, j + d) > 0) continue; return true; } return false; }; int ng = -1, ok = d - 1; while (ok - ng > 1) { int mid = ng + ok >> 1; (chk(mid) ? ok : ng) = mid; } cout << k * d + ok << '\n'; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; int n,d; int cnt[1001][1001]; int deg[1001][1001]; int sum[2005][2005]; vector<int> de; int ma; bool C(int v){ memset(sum,0,sizeof(sum)); int co=0; for(int k=0;k<d;k++){ for(int l=0;l<d;l++){ if(deg[k][l]!=ma)continue; co++; if(cnt[k][l]<=ma*(ma-1)){ sum[k+1][l+1]++; sum[d+k+1][d+l+1]++; sum[d+k+1][l+1]--; sum[k+1][d+l+1]--; sum[k+1][l+1]--; sum[d+k-v][d+l-v]--; sum[d+k-v][l+1]++; sum[k+1][d+l-v]++; }else{ sum[d+k+1][d+l+1]++; sum[d+k+1][d+l-v]--; sum[d+k-v][d+l+1]--; sum[d+k-v][d+l-v]++; } } } for(int i=0;i<=d*2;i++){ for(int j=0;j<d*2;j++){ sum[i][j+1]+=sum[i][j]; } } for(int i=0;i<=d*2;i++){ for(int j=0;j<=d*2;j++){ sum[j+1][i]+=sum[j][i]; } } for(int i=0;i<d;i++){ for(int j=0;j<d;j++){ if(sum[i][j]+sum[i+d][j+d]+sum[i][j+d]+sum[i+d][j]>=co)return true; } } return false; } int main(void){ de.push_back(0); for(int i=1;i<=1000;i++){ de.push_back(i*i); } scanf("%d%d",&n,&d); for(int i=0;i<n;i++){ int x,y; scanf("%d%d",&x,&y); x%=d; y%=d; cnt[x][y]++; } ma=0; for(int i=0;i<d;i++){ for(int j=0;j<d;j++){ deg[i][j]=lower_bound(de.begin(),de.end(),cnt[i][j])-de.begin(); ma=max(deg[i][j],ma); } } int l=-1,r=d-1; while(l+1<r){ int mid=(l+r)/2; if(C(mid))r=mid; else l=mid; } printf("%d\n",d*(ma-1)+r); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; using int64 = long long; const int INF = 1 << 30; int N, D; int ex[2000][2000]; int chokudai[2][2001][2001]; bool check(int t, int y, int x) { int a = t / D; int b = t % D; int64 p[3]; p[0] = 1LL * (a + 1) * (a + 1); p[1] = 1LL * a * (a + 1); p[2] = 1LL * a * a; for(int i = 0; i < D; i++) { for(int j = 0; j < D; j++) { int add = ex[y + i][x + j]; if(i <= b && j <= b) { if(add > p[0]) return false; } else if(b < i && b < j) { if(add > p[2]) return false; } else { if(add > p[1]) return false; } } } return true; } inline int get_rect(int t, int a, int b, int c, int d) { return chokudai[t][c][d] + chokudai[t][a][b] - chokudai[t][a][d] - chokudai[t][c][b]; } bool check(int t) { int a = t / D; bool all = true; for(int i = 0; i < 2 * D; i++) { for(int j = 0; j < 2 * D; j++) { if(ex[i][j] > 1LL * (a + 1) * (a + 1)) return false; all &= ex[i][j] <= 1LL * a * a; chokudai[0][i + 1][j + 1] = ex[i][j] > 1LL * a * a; chokudai[1][i + 1][j + 1] = ex[i][j] > 1LL * a * (a + 1); } } if(all) return true; for(int k = 0; k < 2; k++) { for(int i = 1; i <= 2 * D; i++) { for(int j = 1; j <= 2 * D; j++) { chokudai[k][i][j] += chokudai[k][i - 1][j]; chokudai[k][i][j] += chokudai[k][i][j - 1]; chokudai[k][i][j] -= chokudai[k][i - 1][j - 1]; } } } int b = t % D + 1; for(int i = 0; i < D; i++) { for(int j = 0; j < D; j++) { int add = 0; add += get_rect(0, i + b, j + b, i + D, j + D); add += get_rect(1, i, j, i + D, j + D); add -= get_rect(1, i, j, i + b, j + b); add -= get_rect(1, i + b, j + b, i + D, j + D); if(add == 0) return true; } } return false; } int main() { cin >> N >> D; for(int i = 0; i < N; i++) { int x, y; cin >> x >> y; x %= D; y %= D; ++ex[x][y]; ++ex[x][y + D]; ++ex[x + D][y]; ++ex[x + D][y + D]; } int ok = (1 << 30) - 1; for(int k = 29; k >= 0; k--) { if(check(ok ^ (1 << k))) ok ^= 1 << k; } cout << ok << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); DSquareRotation solver = new DSquareRotation(); solver.solve(1, in, out); out.close(); } } static class DSquareRotation { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.readInt(); int d = in.readInt(); int d2 = d * 2; int[][] mat = new int[d2][d2]; int[][][] sum = new int[3][d2][d2]; for (int i = 0; i < n; i++) { int x = DigitUtils.mod(in.readInt(), d); int y = DigitUtils.mod(in.readInt(), d); mat[x][y]++; } for (int i = 0; i < d2; i++) { for (int j = 0; j < d2; j++) { mat[i][j] = mat[i % d][j % d]; } } IntBinarySearch ibs = new IntBinarySearch() { public boolean check(int mid) { //0 int a = mid / d; int b = mid % d; long[] limits = new long[]{(long) a * a, (long) a * (a + 1), (long) (a + 1) * (a + 1)}; for (int t = 0; t < 3; t++) { for (int i = 0; i < d2; i++) { for (int j = 0; j < d2; j++) { sum[t][i][j] = mat[i][j] > limits[t] ? 1 : 0; } } } for (int t = 0; t < 3; t++) { for (int i = 0; i < d2; i++) { for (int j = 0; j < d2; j++) { if (j > 0) { sum[t][i][j] += sum[t][i][j - 1]; } } } } for (int t = 0; t < 3; t++) { for (int i = 0; i < d2; i++) { for (int j = 0; j < d2; j++) { if (i > 0) { sum[t][i][j] += sum[t][i - 1][j]; } } } } for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { int total = interval(sum[2], i, j, i + b - 1, j + b - 1) + interval(sum[1], i + b, j, i + d - 1, j + b - 1) + interval(sum[1], i, j + b, i + b - 1, j + d - 1) + interval(sum[0], i + b, j + b, i + d - 1, j + d - 1); if (total == 0) { return true; } } } return false; } }; int ans = ibs.binarySearch(0, (int) 1e9) - 1; out.println(ans); } public int interval(int[][] sum, int x1, int y1, int x2, int y2) { if (x1 > x2 || y1 > y2) { return 0; } int ans = sum[x2][y2]; if (x1 > 0) { ans -= sum[x1 - 1][y2]; } if (y1 > 0) { ans -= sum[x2][y1 - 1]; } if (x1 > 0 && y1 > 0) { ans += sum[x1 - 1][y1 - 1]; } return ans; } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class DigitUtils { private DigitUtils() { } public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } } static abstract class IntBinarySearch { public abstract boolean check(int mid); public int binarySearch(int l, int r) { if (l > r) { throw new IllegalArgumentException(); } while (l < r) { int mid = (l + r) >>> 1; if (check(mid)) { r = mid; } else { l = mid + 1; } } return l; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput println(int c) { return append(c).println(); } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
JAVA
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; #define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i ) #define rep(i,n) REP(i,0,n) int get(vector<vector<int>> &v,int x1,int y1,int x2,int y2){ return v[x2][y2]+v[x1][y1]-v[x2][y1]-v[x1][y2]; } int main(){ int n,d; cin>>n>>d; vector<vector<int>>cnt(d,vector<int>(d)); rep(i,n){ int x,y; cin>>x>>y; cnt[x%d][y%d]++; } int ma=0; rep(i,d)rep(j,d){ int rt=sqrt(cnt[i][j]); if(rt*rt<cnt[i][j])++rt; ma=max(rt,ma); } vector<vector<int>> sum1(2*d+2,vector<int>(2*d+2)),sum2(2*d+2,vector<int>(2*d+2)); int tot1=0,tot2=0; rep(i,d)rep(j,d){ if(cnt[i][j]>ma*(ma-1)){ ++sum1[i][j], ++sum1[i+d][j], ++sum1[i][j+d], ++sum1[i+d][j+d], ++tot1; } else if(cnt[i][j]>(ma-1)*(ma-1)){ ++sum2[i][j], ++sum2[i+d][j], ++sum2[i][j+d], ++sum2[i+d][j+d], ++tot2; } } rep(i,2*d)rep(j,2*d){ sum1[i+1][j]+=sum1[i][j]; sum2[i+1][j]+=sum2[i][j]; } rep(i,2*d)rep(j,2*d){ sum1[i][j+1]+=sum1[i][j]; sum2[i][j+1]+=sum2[i][j]; } int ok=d,ng=0,mid; while(ok-ng>1){ mid=(ok+ng)/2; bool can=false; rep(i,d)rep(j,d){ if(get(sum1,i,j,i+mid,j+mid)==tot1&& get(sum2,i+mid,j+mid,i+d,j+d)==0){ can=true; break; } } if(can)ok=mid; else ng=mid; } cout<<(ma-1)*d+ok-1 <<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.BufferedReader; import java.io.Reader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyInput in = new MyInput(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, MyInput in, PrintWriter out) { final int n = in.nextInt(); final int d = in.nextInt(); int[][] cnt = new int[d][d]; for (int i = 0; i < n; i++) { int x = in.nextInt() % d; int y = in.nextInt() % d; cnt[y][x]++; } int[][][] sum = new int[3][2 * d + 1][2 * d + 1]; int low = 0, high = 1_000_000; LOOP: while (high - low > 1) { final int mid = (low + high) >>> 1; long a = mid / d; int b = mid % d; final long[] th = new long[]{(a + 1) * (a + 1), a * (a + 1), a * a,}; for (int t = 0; t < sum.length; t++) { for (int y = 0; y < d; y++) { for (int x = 0; x < d; x++) { final int v = cnt[y][x] <= th[t] ? 1 : 0; sum[t][y + 1][x + 1] = v; sum[t][y + d + 1][x + 1] = v; sum[t][y + 1][x + d + 1] = v; sum[t][y + d + 1][x + d + 1] = v; } } for (int y = 0; y < sum[t].length; y++) { for (int x = 0; x + 1 < sum[t][y].length; x++) { sum[t][y][x + 1] += sum[t][y][x]; } } for (int y = 0; y + 1 < sum[t].length; y++) { for (int x = 0; x < sum[t][y].length; x++) { sum[t][y + 1][x] += sum[t][y][x]; } } } for (int y = 0; y < d; y++) { for (int x = 0; x < d; x++) { boolean ok = true; final int c = d - b; ok &= sum[0][y + b][x + b] - sum[0][y + b][x] - sum[0][y][x + b] + sum[0][y][x] == b * b; ok &= sum[1][y + d][x + b] - sum[1][y + d][x] - sum[1][y + b][x + b] + sum[1][y + b][x] == c * b; ok &= sum[1][y + b][x + d] - sum[1][y][x + d] - sum[1][y + b][x + b] + sum[1][y][x + b] == c * b; ok &= sum[2][y + d][x + d] - sum[2][y + d][x + b] - sum[2][y + b][x + d] + sum[2][y + b][x + b] == c * c; if (ok) { high = mid; continue LOOP; } } } low = mid; } out.println(high - 1); } } static class MyInput { private final BufferedReader in; private static int pos; private static int readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500 * 8 * 2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for (int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public MyInput(InputStream is) { in = new BufferedReader(new InputStreamReader(is)); } public int read() { if (pos >= readLen) { pos = 0; try { readLen = in.read(buffer); } catch (IOException e) { throw new RuntimeException(); } if (readLen <= 0) { throw new MyInput.EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public char nextChar() { while (true) { final int c = read(); if (!isSpace[c]) { return (char) c; } } } int reads(int len, boolean[] accept) { try { while (true) { final int c = read(); if (accept[c]) { break; } if (str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char) c; } } catch (MyInput.EndOfFileRuntimeException e) { } return len; } static class EndOfFileRuntimeException extends RuntimeException { } } }
JAVA
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
/* [dwacon5th-prelims] D - Square Rotation */ #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; const int MAX_N = 1e5; const int MAX_D = 1000; int N, D; int x[MAX_N], y[MAX_N]; int c[MAX_D][MAX_D]; int s[3][MAX_D * 2 + 1][MAX_D * 2 + 1]; bool can(int q) { for (int a = 0; a < D; a++) { for (int b = 0; b < D; b++) { bool is_ok = true; is_ok = is_ok && (s[2][a + q + 1][b + q + 1] - s[2][a + q + 1][b] - s[2][a][b + q + 1] + s[2][a][b] == 0); is_ok = is_ok && (s[1][a + q + 1][b + D] - s[1][a + q + 1][b + q + 1] - s[1][a][b + D] + s[1][a][b + q + 1] == 0); is_ok = is_ok && (s[1][a + D][b + q + 1] - s[1][a + D][b] - s[1][a + q + 1][b + q + 1] + s[1][a + q + 1][b] == 0); is_ok = is_ok && (s[0][a + D][b + D] - s[0][a + D][b + q + 1] - s[0][a + q + 1][b + D] + s[0][a + q + 1][b + q + 1] == 0); if (is_ok) { return true; } } } return false; } ll solve() { int p = 0; for (int i = 0; i < N; i++) { c[x[i] % D][y[i] % D]++; } for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { while ((p + 1) * (p + 1) < c[i][j]) { p++; } } } int thresh[3] = {p * p, p * (p + 1), (p + 1) * (p + 1)}; for (int k = 0; k < 3; k++) { for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (c[i][j] > thresh[k]) { s[k][1 + i][1 + j]++; s[k][1 + i + D][1 + j]++; s[k][1 + i][1 + j + D]++; s[k][1 + i + D][1 + j + D]++; } } } } for (int k = 0; k < 3; k++) { for (int i = 1; i <= 2 * D; i++) { for (int j = 0; j <= 2 * D; j++) { s[k][i + 1][j] += s[k][i][j]; } } for (int i = 0; i <= 2 * D; i++) { for (int j = 1; j <= 2 * D; j++) { s[k][i][j + 1] += s[k][i][j]; } } } int ng = -1, ok = D - 1; while (ok - ng > 1) { int m = (ok + ng) / 2; if (can(m)) { ok = m; } else { ng = m; } } ll ans = p * D + ok; return ans; } int main() { cin >> N >> D; for (int i = 0; i < N; i++) { cin >> x[i] >> y[i]; } cout << solve() << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define mk make_pair using namespace std; typedef long long ll; const int N = 2e3 + 5; const int mod = 1e9 + 7; int n, d, cnt[N][N]; int x[N * 100], y[N * 100]; int type[N][N]; int sum[N][N][2]; inline int S(int x, int y, int z) { if (x < 0 || y < 0) return 0; return sum[x][y][z]; } bool check(int k) { int tmp, tmp1, tmp2, p = k % d; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { tmp = S(i + p - 1, j + p - 1, 1) - S(i + p - 1, j - 1, 1) - S(i - 1, j + p - 1, 1) + S(i - 1, j - 1, 1); //cout << i << " " << j << " " << tmp << endl; if (sum[d - 1][d - 1][1] != tmp) continue; tmp1 = S(d - 1, j + p - 1, 0) - S(d - 1, j - 1, 0); tmp2 = S(i + p - 1, d - 1, 0) - S(i - 1, d - 1, 0); tmp = S(i + p - 1, j + p - 1, 0) - S(i + p - 1, j - 1, 0) - S(i - 1, j + p - 1, 0) + S(i - 1, j - 1, 0); if (tmp1 + tmp2 - tmp != sum[d - 1][d - 1][0]) continue; return 1; } } return 0; } int main() { scanf("%d %d", &n, &d); int mmax = 0; for (int i = 1; i <= n; ++i) { scanf("%d %d", x + i, y + i); cnt[x[i] % d][y[i] % d]++; mmax = max(mmax, cnt[x[i] % d][y[i] % d]); } //check(4);return 0; int f = 1; for (int i = 0; i <= 1000; i++) { int x = i; if ((x + 1) * (x + 1) >= mmax) { f = i; break; } } int x = f; ll t1 = (x + 1) * (x + 1ll); ll t2 = (x + 1ll) * x; ll t3 = (ll)x * x; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { if (cnt[i][j] <= t3) { type[i][j] = 2; } else if (cnt[i][j] <= t2) { type[i][j] = 0; } else if (cnt[i][j] <= t1) { type[i][j] = 1; } //cout << type[i][j] << ","; } //cout << endl; } for (int i = 0; i < 2 * d; ++i) { for (int j = 0; j < 2 * d; ++j) { for (int k = 0; k < 2; ++k) { sum[i][j][k] = 0; if (i) sum[i][j][k] += sum[i - 1][j][k]; if (j) sum[i][j][k] += sum[i][j - 1][k]; if (i && j) sum[i][j][k] -= sum[i - 1][j - 1][k]; if (k == type[i % d][j % d]) sum[i][j][k]++; } } } int l = 0, r = d; while (l < r) { int mid = l + r >> 1; //cout << mid << endl; if (l + 1 == r) { if (check(l)) --r; else ++l; break; } if (check(mid)) r = mid; else l = mid; } cout << x * d + l - 1; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } int N, D; int C[1011][1011]; int F[1011][1011]; int dp[1011][1011]; int row[1011], col[1011]; bool ok(int mid) { LL q = (mid+1)/D; int r = (mid+1)%D; LL val[3]; val[0] = q*q; val[1] = val[0] + q; val[2] = val[1] + q + 1; LL p = val[0] * (D-r)*(D-r) + val[1] * (D-r)*r*2 + val[2] * r*r; assert(p == (LL)(mid+1)*(mid+1)); memset(row, 0, sizeof row); memset(col, 0, sizeof col); REP (i, D) REP (j, D) { REP (t, 4) { if (t == 3) return false; if (C[i][j] <= val[t]) { amax(row[i], t); amax(col[j], t); F[i][j] = t; break; } } } memset(dp, 0, sizeof dp); REP (t, 4) REP (i, D) REP (j, D) { if (F[i][j] == 0 && row[i] < 2 && col[j] < 2) { int mi = 1<<29; int i2 = (i+D-1)%D; int j2 = (j+D-1)%D; amin(mi, dp[i2][j]); amin(mi, dp[i][j2]); amin(mi, dp[i2][j2]); amax(dp[i][j], mi+1); } } int s = 0; REP (i, D) REP (j, D) amax(s, dp[i][j]); return s >= D-r; } void MAIN() { scanf("%d%d", &N, &D); REP (i, N) { int x, y; scanf("%d%d", &x, &y); x %= D; y %= D; C[x][y]++; } int lo = -1, hi = 1; while (!ok(hi)) { lo = hi; hi += hi; } while (hi - lo > 1) { int mid = (hi + lo) / 2; (ok(mid)? hi: lo) = mid; } printf("%d\n", hi); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int Maxn = 1005; int n, D; int cnt[Maxn][Maxn]; int B[Maxn][Maxn]; void Add(int X1, int X2, int Y1, int Y2, int val) { if (X1 > X2) { Add(X1, D - 1, Y1, Y2, val); Add(0, X2, Y1, Y2, val); return; } if (Y1 > Y2) { Add(X1, X2, Y1, D - 1, val); Add(X1, X2, 0, Y2, val); return; } B[X1][Y1] += val; B[X2 + 1][Y1] -= val; B[X1][Y2 + 1] -= val; B[X2 + 1][Y2 + 1] += val; } bool Check(int k) { int t = k / D; ll got = ll(t) * (t); int k2 = k % D; fill((int*)B, (int*)B + Maxn * Maxn, 0); for (int i = 0; i < D; i++) for (int j = 0; j < D; j++) if (cnt[i][j] <= got) Add(0, D - 1, 0, D - 1, 1); else { if (k2 == 0) return false; int lft = cnt[i][j] - got; int begi = (i - k2 + 1 + D) % D; int begj = (j - k2 + 1 + D) % D; if (lft <= t) { Add(begi, i, 0, D - 1, 1); Add(0, D - 1, begj, j, 1); Add(begi, i, begj, j, -1); } else if (lft <= 2 * t + 1) Add(begi, i, begj, j, 1); else return false; } for (int i = 0; i < D; i++) for (int j = 0; j < D; j++) { if (i > 0) B[i][j] += B[i - 1][j]; if (j > 0) B[i][j] += B[i][j - 1]; if (i > 0 && j > 0) B[i][j] -= B[i - 1][j - 1]; if (B[i][j] >= D * D) return true; } return false; } int main() { scanf("%d %d", &n, &D); for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); cnt[x % D][y % D]++; } int lef = 0, rig = 1000000007; while (lef <= rig) { int mid = lef + rig >> 1; if (Check(mid)) rig = mid - 1; else lef = mid + 1; } printf("%d\n", rig); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <fstream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <set> #include <map> #include <stack> #include <queue> #include <deque> #include <bitset> #include <algorithm> #include <complex> using namespace std; #define REP(i,n) for(int i=0; i<n; ++i) #define FOR(i,a,b) for(int i=a; i<=b; ++i) #define FORR(i,a,b) for (int i=a; i>=b; --i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int,int> P; typedef pair<ll,ll> PL; int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } template<typename T> struct cumsum2D{ int n, m; vector<vector<T>> dat; cumsum2D(){} cumsum2D(vector<vector<T>> &x){ n = x.size(); m = x[0].size(); dat.assign(n+1, vector<T>(m+1)); REP(i,n) REP(j,m){ dat[i+1][j+1] = dat[i+1][j] + dat[i][j+1] - dat[i][j] + x[i][j]; } } // [0, x) * [0, y) T sum(int x, int y){ return dat[x][y]; } // [x0, x1) * [y0 * y1) T sum(int x0, int y0, int x1, int y1){ return sum(x1, y1) - sum(x0, y1) - sum(x1, y0) + sum(x0, y0); } }; int main() { int n, d; cin >> n >> d; VVI cnt(d, VI(d)); REP(i,n){ int x = in(), y = in(); cnt[x % d][y % d]++; } int ma = 0; REP(i,d) REP(j,d) ma = max(ma, cnt[i][j]); int x = 0; while ((x + 1) * (x + 1) < ma) x++; int c = 0, c2 = 0; VVI f(2 * d, VI(2 * d)); VVI g(2 * d, VI(2 * d)); REP(i,d) REP(j,d) if (cnt[i][j] > x * (x + 1)) { f[i][j] = f[i + d][j] = f[i][j + d] = f[i + d][j + d] = 1; c++; } REP(i,d) REP(j,d) if (cnt[i][j] > x * x) { g[i][j] = g[i + d][j] = g[i][j + d] = g[i + d][j + d] = 1; c2++; } // REP(i,d){ // REP(j,d) cout << cnt[i][j]; // cout << endl; // } // REP(i,d){ // REP(j,d) cout << f[i][j]; // cout << endl; // } // cout << endl; // REP(i,d){ // REP(j,d) cout << g[i][j]; // cout << endl; // } // cout << endl; cumsum2D<int> s1(f), s2(g); int ok = d, ng = -1; while (ok - ng > 1){ int m = (ok + ng) / 2; bool f = false; REP(i,d) REP(j,d){ int tmp = s1.sum(i, j, i + m, j + m); int tmp2 = s2.sum(i, j, i + d, j + m) + s2.sum(i, j, i + m, j + d) - s2.sum(i, j, i + m, j + m); if (tmp == c && tmp2 == c2){ f = true; break; } } if (f) ok = m; else ng = m; } int ans = d * x - 1; ans += ok; cout << ans << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<vector> #include<cmath> #include<map> #include<cstdlib> #include<iostream> #include<sstream> #include<fstream> #include<string> #include<algorithm> #include<cstring> #include<cstdio> #include<set> #include<stack> #include<bitset> #include<functional> #include<ctime> #include<queue> #include<deque> #include<complex> #include<cassert> using namespace std; #define pb push_back #define pf push_front typedef long long lint; typedef complex<double> P; #define mp make_pair #define fi first #define se second typedef pair<int,int> pint; typedef pair<int,pint> tint; #define All(s) s.begin(),s.end() #define rAll(s) s.rbegin(),s.rend() #define REP(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) REP(i,0,n) //問題文および制約はちゃんと確認しよう! //サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも //とりあえずサンプルを読んでから解法を考えよう? lint num[1050][1050]; int dat[3][2010][2010]; int d; lint num2(int id,int lx,int ly,int hx,int hy){ if(hx<0 || hy<0) return 0; int ret=dat[id][hx][hy]; if(lx>0) ret-=dat[id][lx-1][hy]; if(ly>0) ret-=dat[id][hx][ly-1]; if(lx>0 && ly>0) ret+=dat[id][lx-1][ly-1]; return ret; } bool cal(int mi){ mi++; lint a=mi/d,b=mi%d; memset(dat,0,sizeof(dat)); rep(i,d) rep(j,d) rep(k,2) rep(l,2){ if(num[i][j]<=a*a) dat[0][i+k*d][j+l*d]++; if(num[i][j]<=a*(a+1)) dat[1][i+k*d][j+l*d]++; if(num[i][j]<=(a+1)*(a+1)) dat[2][i+k*d][j+l*d]++; } rep(i,d*2) rep(j,d*2) rep(k,3){ if(i>0) dat[k][i][j]+=dat[k][i-1][j]; if(j>0) dat[k][i][j]+=dat[k][i][j-1]; if(i>0 && j>0) dat[k][i][j]-=dat[k][i-1][j-1]; } rep(i,d) rep(j,d){ if(num2(2,i,j,i+b-1,j+b-1)>=b*b && num2(1,i,j+b,i+b-1,j+d-1)>=b*(d-b) && num2(1,i+b,j,i+d-1,j+b-1)>=b*(d-b) && num2(0,i+b,j+b,i+d-1,j+d-1)>=(d-b)*(d-b)){ return true; } } return false; } int main() { memset(num,0,sizeof(num)); int n,x,y; cin>>n>>d; /*if(d>30){ cout<<0<<endl;return 0; }*/ rep(i,n){ cin>>x>>y;num[x%d][y%d]++; } int lo=0,hi=1001001001; //cal(0); while(hi>lo){ lint mi=(hi+lo)/2; if(cal(mi)) hi=mi;else lo=mi+1; } cout<<lo<<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} //INSERT ABOVE HERE const Int MAX = 1010; Int cnt[MAX][MAX]; Int dp1[MAX*3][MAX*3]; Int dp2[MAX*3][MAX*3]; signed main(){ Int n,d; cin>>n>>d; vector<Int> x(n),y(n); for(Int i=0;i<n;i++) cin>>x[i]>>y[i]; memset(cnt,0,sizeof(cnt)); for(Int i=0;i<n;i++) cnt[x[i]%d][y[i]%d]++; Int num=0; for(Int i=0;i<d;i++) for(Int j=0;j<d;j++) chmax(num,cnt[i][j]); Int sq=0; { Int l=0,r=num+1; while(l+1<r){ Int m=(l+r)>>1; if(m*m>=num) r=m; else l=m; } assert(r*r>=num); assert(l*l<num); sq=l; } memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); for(Int i=0;i<d;i++){ for(Int j=0;j<d;j++){ //cout<<i<<" "<<j<<":"<<(cnt[i][j]>sq*(sq+1))<<endl; //cout<<i<<" "<<j<<":"<<(cnt[i][j]>sq*sq)<<endl; for(Int a=0;i+a<MAX*3;a+=d){ for(Int b=0;j+b<MAX*3;b+=d){ dp1[i+a][j+b]=cnt[i][j]>sq*(sq+1); dp2[i+a][j+b]=cnt[i][j]>sq*sq; } } } } for(Int i=0;i<MAX*3;i++){ for(Int j=1;j<MAX*3;j++){ dp1[i][j]+=dp1[i][j-1]; dp2[i][j]+=dp2[i][j-1]; } } for(Int i=1;i<MAX*3;i++){ for(Int j=0;j<MAX*3;j++){ dp1[i][j]+=dp1[i-1][j]; dp2[i][j]+=dp2[i-1][j]; } } auto sum1=[&](Int x1,Int x2,Int y1,Int y2){ return (dp1[x2][y2]+dp1[x1][y1])-(dp1[x1][y2]+dp1[x2][y1]);}; auto sum2=[&](Int x1,Int x2,Int y1,Int y2){ return (dp2[x2][y2]+dp2[x1][y1])-(dp2[x1][y2]+dp2[x2][y1]);}; auto check= [&](Int b)->Int{ for(Int i=1;i<=d;i++){ for(Int j=1;j<=d;j++){ if(sum1(i+b,i+d-1,j-1,j+d-1)) continue; if(sum1(i-1,i+d-1,j+b,j+d-1)) continue; if(sum2(i+b,i+d-1,j+b,j+d-1)) continue; return 1; } } return 0; }; Int l=-1,r=d; while(l+1<r){ Int m=(l+r)>>1; if(check(m)) r=m; else l=m; } cout<<sq*d+r<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
// 基本テンプレート // #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__) #define int long long int template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; const int SIZE = 1010; int N, D, cnt[SIZE][SIZE]; vector< vector<int> > get_table(int target) { vector< vector<int> > res(2*D+2, vector<int>(2*D+2)); for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { if(cnt[i][j] > target) { res[i+1 ][j+1 ]++; res[i+1+D][j+1+D]++; res[i+1 ][j+1+D]++; res[i+1+D][j+1 ]++; } } } for(int i=0; i<=2*D; i++) { for(int j=0; j<=2*D; j++) { res[i][j+1] += res[i][j]; } } for(int j=0; j<=2*D; j++) { for(int i=0; i<=2*D; i++) { res[i+1][j] += res[i][j]; } } return res; } int get_sum(const vector< vector<int> >& sum, int lx, int ly, int rx, int ry) { int res = 0; res += sum[rx][ry] + sum[lx][ly]; res -= sum[lx][ry] + sum[rx][ly]; return res; } signed main() { cin >> N >> D; for(int i=0; i<N; i++) { int x, y; cin >> x >> y; x %= D, y %= D; cnt[x][y]++; } int ansA = -1, ansB = -1; for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { int deg = 0; for(; (deg+1)*(deg+1)<cnt[i][j]; ) deg++; ansA = max(ansA, deg); } } vector< vector<int> > sum1 = get_table((ansA + 1) * (ansA + 1)); vector< vector<int> > sum2 = get_table( ansA * ansA ); vector< vector<int> > sum3 = get_table((ansA + 1) * ansA); int ub = D-1, lb = -1; while(ub - lb > 1) { int b = (ub + lb) / 2; bool can = false; // (i, j) が正方形の左下 for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { bool ok = true; ok &= (get_sum(sum1, i , j , i+b+1, j+b+1) == 0); ok &= (get_sum(sum3, i+b+1, j , i+D , j+b+1) == 0); ok &= (get_sum(sum3, i , j+b+1, i+b+1, j+D ) == 0); ok &= (get_sum(sum2, i+b+1, j+b+1, i+D , j+D ) == 0); can |= ok; } } if(can) ub = b; else lb = b; } ansB = ub; cout << ansA * D + ansB << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL read(){ LL x=0,f=1; char ch=getchar(); while (!isdigit(ch)&&ch!='-') ch=getchar(); if (ch=='-') f=0,ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?x:-x; } const int N=2005; int n,d; int v[N][N]; int s[4][N][N]; #define y1 __zzd001 int sum(int x1,int y1,int x2,int y2,int t){ return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x){ int r=x%d; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++){ int x1=i,y1=j; int x2=x1+r,y2=y1+r; int x3=x1+d-1,y3=y1+d-1; if (!sum(x1,y2+1,x3,y3,3) &&!sum(x2+1,y1,x3,y3,3) &&!sum(x2+1,y2+1,x3,y3,2)) return 1; } return 0; } int main(){ n=read(),d=read(); memset(v,0,sizeof v); while (n--){ int x=read(),y=read(); v[x%d+1][y%d+1]++; } for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j]; int Mx=0; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) Mx=max(Mx,v[i][j]); int t; for (t=0;(t+1)*(t+1)<Mx;t++); int l[5]={0,0,t*t+1,t*(t+1)+1,(int)1e9}; for (int i=1;i<=d*2;i++) for (int j=1;j<=d*2;j++) for (int t=1;t<=3;t++){ s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if (l[t]<=v[i][j]&&v[i][j]<l[t+1]) s[t][i][j]++; } int L=t*d,R=t*d+d-1,ans=R; while (L<=R){ int mid=(L+R)>>1; if (check(mid)) R=mid-1,ans=mid; else L=mid+1; } cout << ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define int long long #define inf 1000000007 #define pa pair<int,int> #define ll long long #define pal pair<double,double> #define ppap pair<pa,int> #define PI 3.14159265358979323846 #define paa pair<int,char> #define mp make_pair #define pb push_back #define EPS (1e-10) int dx[8]={0,1,0,-1,1,1,-1,-1}; int dy[8]={1,0,-1,0,-1,1,1,-1}; using namespace std; class pa3{ public: int x,y,z; pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} bool operator < (const pa3 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; return z<p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa3 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; return z>p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa3 &p) const{ return x==p.x && y==p.y && z==p.z; } bool operator != (const pa3 &p) const{ return !( x==p.x && y==p.y && z==p.z); } }; class pa4{ public: int x; int y,z,w; pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} bool operator < (const pa4 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; if(z!=p.z)return z<p.z; return w<p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa4 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; if(z!=p.z)return z>p.z; return w>p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa4 &p) const{ return x==p.x && y==p.y && z==p.z &&w==p.w; } }; class pa2{ public: int x,y; pa2(int x=0,int y=0):x(x),y(y) {} pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} bool operator < (const pa2 &p) const{ return y != p.y ? y<p.y: x<p.x; } bool operator > (const pa2 &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa2 &p) const{ return abs(x-p.x)==0 && abs(y-p.y)==0; } bool operator != (const pa2 &p) const{ return !(abs(x-p.x)==0 && abs(y-p.y)==0); } }; /* class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } */ string itos( int i ) { ostringstream s ; s << i ; return s.str() ; } int gcd(int v,int b){ if(v>b) return gcd(b,v); if(v==b) return b; if(b%v==0) return v; return gcd(v,b%v); } double distans(double x1,double y1,double x2,double y2){ double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); return sqrt(rr); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a%b, y, x); y -= a/b * x; return d; } int pr[1000010]; int inv[1000010]; int beki(int wa,int rr,int warukazu){ if(rr==0) return 1%warukazu; if(rr==1) return wa%warukazu; wa%=warukazu; if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu; ll zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } double bekid(double w,int r){ if(r==0) return 1.0; if(r==1) return w; if(r%2) return bekid(w,r-1)*w; double f=bekid(w,r/2); return f*f; } int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=mod; r*=inv[nn-rr]; r%=mod; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%mod; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],mod-2,mod); } // cin.tie(0); // ios::sync_with_stdio(false); //priority_queue<pa3,vector<pa3>,greater<pa3>> pq; //sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ int n,d; int ko[2011][2011]={}; int rui[2][2011][2011]={}; int r(int t,int x1,int x2,int y1,int y2){ // cout<<t<<" "<<x1<<" "<<x2<<" "<<y1<<" "<<y2<<" "<<rui[t][x2][y2]-rui[t][x1][y2]-rui[t][x2][y1]+rui[t][x1][y1]<<endl; return rui[t][x2][y2]-rui[t][x1][y2]-rui[t][x2][y1]+rui[t][x1][y1]; } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cin>>n>>d; // assert(d<=30); for(int i=0;i<n;i++){ int y,yy; cin>>y>>yy; ko[y%d+1][yy%d+1]++; } int ma=-1; for(int i=1;i<=d;i++){ for(int j=1;j<=d;j++){ // cout<<ko[i][j]<<" "; ko[i+d][j]=ko[i][j]; ko[i][j+d]=ko[i][j]; ko[i+d][j+d]=ko[i][j]; ma=max(ma,ko[i][j]); } //cout<<endl; } int t; for(t=0;;t++){ if(t*t>=ma)break; } // ans [(t-1)*d+1,t*d] // cout<<t<<endl; for(int i=1;i<=2*d;i++)for(int j=1;j<=2*d;j++){ rui[0][i][j]=-rui[0][i-1][j-1]+rui[0][i][j-1]+rui[0][i-1][j]+(ko[i][j]>t*(t-1)); rui[1][i][j]=-rui[1][i-1][j-1]+rui[1][i][j-1]+rui[1][i-1][j]+(ko[i][j]>(t-1)*(t-1)); } int ans=inf; for(int i=1;i<=d;i++)for(int j=1;j<=d;j++){ int si=0,ue=d,me; while(ue-si>1){ me=(ue+si)/2; bool bo=1; if(me==0) bo=0; else{ if(r(0,i+me-1,i+d-1,j-1,j+me-1))bo=0; if(r(0,i-1,i+me-1,j+me-1,j+d-1))bo=0; if(r(1,i+me-1,i+d-1,j+me-1,j+d-1))bo=0; } if(bo)ue=me; else si=me; } ans=min(ans,ue); // cout<<i<<" "<<j<<" "<<ue<<endl; } // cout<<ans<<endl; cout<<(t-1)*d+ans-1<<endl; //cout<<ue<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <vector> #include <utility> using namespace std; typedef long long ll; const int D = 1000; int arr[2][2*D + 1][2*D + 1]; int get(int j, int x1, int y1, int x2, int y2) { return arr[j][x2+1][y2+1] - arr[j][x1][y2+1] - arr[j][x2+1][y1] + arr[j][x1][y1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, d; cin >> n >> d; // Divide the plane into D^2 partitions // Say d = 3, // a b c a b c a b c // d e f d e f d e f // g h i g h i g h i // a b c a b c a b c // d e f d e f d e f // g h i g h i g h i // a b c a b c a b c // d e f d e f d e f // g h i g h i g h i // We can move points within the same partition arbitrarily. Points cannot switch partitions. vector<int> cou(d*d, 0); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; x %= d; y %= d; ++cou[x + y * d]; } // All partitions want to be packed into a square, or a rectangle with one side d shorter than the other // Find points with longest edge size, and put them into bads. Points not in bads are not a concern int mx = -1; vector<pair<int, int>> bads; for (int i = 0; i < d*d; ++i) { if (! cou[i]) continue; int w = 1; while(w*w < cou[i]) { ++w; } int h = (cou[i] + (w-1)) / w; if (w > mx) { mx = w; bads.clear(); } if (w == mx) bads.push_back({i, (bool)(w > h)}); } // Fill the two-dimensional sum-array so we can count how many points are in a rectangle fast. int sq_cou = 0; for (auto pr : bads) { int i = pr.first; int j = pr.second; int x = i % d; int y = i / d; ++x; ++y; if (j == 0) ++sq_cou; ++arr[j][x][y]; ++arr[j][x+d][y]; ++arr[j][x][y+d]; ++arr[j][x+d][y+d]; } for (int j = 0; j < 2; ++j) { for (int x = 0; x <= 2*d; ++x) { for (int y = 0; y <= 2*d; ++y) { if (x == 0) { arr[j][x][y] += arr[j][x][y-1]; } else if (y == 0) { arr[j][x][y] += arr[j][x-1][y]; } else { arr[j][x][y] += arr[j][x-1][y] + arr[j][x][y-1] - arr[j][x-1][y-1]; } } } } // Binary search answer int low = 0; int high = d-1; while(low != high) { int mid = (low + high) >> 1; bool works = false; for (int x = 0; x < d; ++x) { for (int y = 0; y < d; ++y) { // Rectangles must not be in [mid+1, d-1] x [mid+1, d-1] if (get(1, x + mid + 1, y + mid + 1, x + d-1, y + d-1) != 0) continue; // Squares must be in [0, mid] x [0, mid] if (get(0, x, y, x + mid, y + mid) != sq_cou) continue; works = true; } } if (works) high = mid; else low = mid + 1; } // Calculate answer if (mx == -1) { cout << "0\n"; } else { int ans = (mx - 1) * d + low; cout << ans << '\n'; } }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define MAX 1000 int cnt[MAX][MAX], N, D; int s2[MAX + 1][MAX + 1], s3[MAX + 1][MAX + 1]; int sum2( int x1, int y1, int x2, int y2 ) { if( x1 > x2 || y1 > y2 ) { return 0; } x1++; y1++; x2++; y2++; return s2[x2][y2] - s2[x2][y1 - 1] - s2[x1 - 1][y2] + s2[x1 - 1][y1 - 1]; } int sum3( int x1, int y1, int x2, int y2 ) { if( x1 > x2 || y1 > y2 ) { return 0; } x1++; y1++; x2++; y2++; return s3[x2][y2] - s3[x2][y1 - 1] - s3[x1 - 1][y2] + s3[x1 - 1][y1 - 1]; } bool check2( int x, int y, int b ) { int d1 = sum2( max( 0, x + b + 1 - D ), max( 0, y + b + 1 - D ), x - 1, y - 1 ); int d2 = sum2( x + b + 1, y + b + 1, D - 1, D - 1 ); int d3 = sum2( max( 0, x + b + 1 - D ), y + b + 1, x - 1, D - 1 ); int d4 = sum2( x + b + 1, max( 0, y + b + 1 - D ), D - 1, y - 1 ); return d1 == 0 && d2 == 0 && d3 == 0 && d4 == 0; } bool check3( int x, int y, int b ) { int d1 = sum3( x + b + 1, 0, D - 1, D - 1 ); int d2 = sum3( 0, y + b + 1, D - 1, D - 1 ); int d3 = sum3( max( 0, x + b + 1 - D ), 0, x - 1, D - 1 ); int d4 = sum3( 0, max( 0, y + b + 1 - D ), D - 1, y - 1 ); return d1 == 0 && d2 == 0 && d3 == 0 && d4 == 0; } int main() { cin >> N >> D; for(int i = 0; i < N ; i++ ) { int x, y; cin >> x >> y; cnt[x % D][y % D]++; } int a = 0; for( int i = 0 ; i < D ; i++ ) { for( int j = 0 ; j < D ; j++ ) { if( cnt[i][j] ) { int t = ceil( sqrt( cnt[i][j] ) ); a = max( t - 1, a ); } } } for(int i = 0 ; i < D ; i++ ) { for(int j = 0 ; j < D ; j++ ) { if( a * a < cnt[i][j] && cnt[i][j] <= a * (a + 1) ) { s2[i + 1][j + 1]++; } else if( cnt[i][j] > a * (a + 1) ) { s3[i + 1][j + 1]++; } } } for( int i = 1; i <= D; i++ ) { for( int j = 1; j <= D; j++ ) { s2[i][j] = s2[i][j] + s2[i - 1][j] + s2[i][j - 1] - s2[i - 1][j - 1]; s3[i][j] = s3[i][j] + s3[i - 1][j] + s3[i][j - 1] - s3[i - 1][j - 1]; } } int b; for( b = 0; b < D; b++ ) { if( check2( 0, 0, b ) && check3( 0, 0, b ) ) { break; } } for(int i = 0; i < D; i++ ) { for(int j = 0; j < D; j++ ) { if( i == 0 && j == 0 ) { continue; } while( b ) { if( check2( i, j, b - 1 ) && check3( i, j, b - 1 ) ) { b--; } else { break; } } } } cout << a * D + b << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define debug(x) cerr<<#x<<" = "<<x #define sp <<" " #define el <<endl #define LL long long #define M 100020 using namespace std; namespace IO{ const int BS=(1<<20)+5; int Top=0; char Buffer[BS],OT[BS],*OS=OT,*HD,*TL,SS[20]; const char *fin=OT+BS-1; char Getchar(){if(HD==TL){TL=(HD=Buffer)+fread(Buffer,1,BS,stdin);} return (HD==TL)?EOF:*HD++;} void flush(){fwrite(OT,1,OS-OT,stdout);} void Putchar(char c){*OS++ =c;if(OS==fin)flush(),OS=OT;} void write(int x){ if(!x){Putchar('0');return;} if(x<0) x=-x,Putchar('-'); while(x) SS[++Top]=x%10,x/=10; while(Top) Putchar(SS[Top]+'0'),--Top; } int read(){ int nm=0,fh=1; char cw=getchar(); for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh; for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0'); return nm*fh; } } using namespace IO; #define bas 1005 int n,m,p[1020][1020],X[M],Y[M],D,ans,c[2020][2020],len[M][2]; void calc(int id,int x){ int k=sqrt(x);while(k*k<x) k++; len[id][0]=k,len[id][1]=(x/k)+(x%k>0); len[id][0]*=D,len[id][0]-=D; len[id][1]*=D,len[id][1]-=D; if(len[id][0]>len[id][1]) swap(len[id][0],len[id][1]); } void add(int dt,int x,int y,int xx,int yy){ c[x][y]+=dt,c[x][yy+1]-=dt,c[xx+1][y]-=dt,c[xx+1][yy+1]+=dt; } void mdf(int kd,int x,int y,int t1,int t2,int dis){ int q1=dis-t1,q2=dis-t2; if(q1<0||q2<0) return; if(q1>=D) q1=D-1; if(q2>=D) q2=D-1; add(kd,x-q1+bas,y-q2+bas,x+bas,y+bas); } int tot(int x,int y){return c[x+bas][y+bas]+c[x-D+bas][y+bas]+c[x+bas][y-D+bas]+c[x-D+bas][y-D+bas];} bool solve(int dis){ memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ int x=X[i],y=Y[i]; mdf(1,x,y,len[i][0],len[i][1],dis); mdf(1,x,y,len[i][1],len[i][0],dis); mdf(-1,x,y,len[i][1],len[i][1],dis); } for(int i=bas-D;i<=bas+D;i++){ for(int j=bas-D;j<=bas+D;j++) c[i][j]=c[i][j]+c[i-1][j]+c[i][j-1]-c[i-1][j-1]; } for(int i=0;i<D;i++){ for(int j=0;j<D;j++) if(tot(i,j)==n){return true;} } return false; } int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); n=read(),D=read(); for(int i=1;i<=n;i++){ X[i]=read(),Y[i]=read(); X[i]%=D,Y[i]%=D,p[X[i]][Y[i]]++; } if(n==1){puts("0");return 0;} n=0; for(int i=0;i<D;i++){ for(int j=0;j<D;j++){ if(!p[i][j]) continue; n++,X[n]=i,Y[n]=j,calc(n,p[i][j]); } } for(int l=1,r=2000000002,mid=1000000001;l<=r;mid=((l+r)>>1)){ if(solve(mid)) ans=mid,r=mid-1;else l=mid+1; }printf("%d\n",ans); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
// 基本テンプレート // #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__) #define int long long int template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; const int SIZE = 1010; int N, D, cnt[SIZE][SIZE]; vector< vector<int> > get_table(int target) { vector< vector<int> > res(2*D+2, vector<int>(2*D+2)); for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { if(cnt[i][j] > target) { res[i+1 ][j+1 ]++; res[i+1+D][j+1+D]++; res[i+1 ][j+1+D]++; res[i+1+D][j+1 ]++; } } } for(int i=0; i<=2*D; i++) { for(int j=0; j<=2*D; j++) { res[i][j+1] += res[i][j]; } } for(int j=0; j<=2*D; j++) { for(int i=0; i<=2*D; i++) { res[i+1][j] += res[i][j]; } } return res; } int get_sum(const vector< vector<int> >& sum, int lx, int ly, int rx, int ry) { int res = 0; res += sum[rx][ry] + sum[lx][ly]; res -= sum[lx][ry] + sum[rx][ly]; return res; } void print_table(const vector< vector<int> >& sum) { for(int i=1; i<=2*D; i++) { for(int j=1; j<=2*D; j++) { cerr << get_sum(sum, i-1, j-1, i, j) << " "; } cerr << endl; } } signed main() { cin >> N >> D; for(int i=0; i<N; i++) { int x, y; cin >> x >> y; x %= D, y %= D; cnt[x][y]++; } int ansA = -1, ansB = -1; for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { // fprintf(stderr, "cnt[%lld][%lld] = %lld\n", i, j, cnt[i][j]); int deg = 0; for(; (deg+1)*(deg+1)<cnt[i][j]; ) deg++; ansA = max(ansA, deg); } } vector< vector<int> > sum1 = get_table((ansA + 1) * (ansA + 1)); vector< vector<int> > sum2 = get_table( ansA * ansA ); vector< vector<int> > sum3 = get_table((ansA + 1) * ansA); /* fprintf(stderr, "# sum1 (target = %lld)\n", (ansA + 1) * (ansA + 1)); print_table(sum1); fprintf(stderr, "# sum2 (target = %lld)\n", ansA * ansA); print_table(sum2); fprintf(stderr, "# sum3 (target = %lld)\n", ansA * (ansA + 1)); print_table(sum3); */ int ub = D-1, lb = -1; while(ub - lb > 1) { int b = (ub + lb) / 2; bool can = false; // (i, j) が正方形の左下 for(int i=0; i<D; i++) { for(int j=0; j<D; j++) { bool ok = true; ok &= (get_sum(sum1, i , j , i+b+1, j+b+1) == 0); ok &= (get_sum(sum3, i+b+1, j , i+D , j+b+1) == 0); ok &= (get_sum(sum3, i , j+b+1, i+b+1, j+D ) == 0); ok &= (get_sum(sum2, i+b+1, j+b+1, i+D , j+D ) == 0); can |= ok; } } if(can) ub = b; else lb = b; } ansB = ub; // fprintf(stderr, "ansA = %lld, ansB = %lld\n", ansA, ansB); cout << ansA * D + ansB << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; #define MOD @ #define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; int N, D, X[101010], Y[101010]; int grp[1010][1010]; int cnts[3][2020][2020]; bool rect(int t, int x0, int y0, int x1, int y1) { return (cnts[t][x1][y1] - cnts[t][x1][y0] - cnts[t][x0][y1] + cnts[t][x0][y0]) == (x1 - x0) * (y1 - y0); } bool test(i64 W) { i64 bas = W / D; int aux = (int)(W % D); i64 ns[] = {bas * bas, bas * (bas + 1), (bas + 1) * (bas + 1)}; for (int t = 0; t < 3; ++t) { for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { cnts[t][i + 1][j + 1] = (grp[i][j] <= ns[t] ? 1 : 0); cnts[t][i + 1][j + 1 + D] = cnts[t][i + 1 + D][j + 1] = cnts[t][i + 1 + D][j + 1 + D] = cnts[t][i + 1][j + 1]; } } for (int i = 0; i <= 2 * D; ++i) { for (int j = 1; j <= 2 * D; ++j) { cnts[t][i][j] += cnts[t][i][j - 1]; } } for (int i = 1; i <= 2 * D; ++i) { for (int j = 0; j <= 2 * D; ++j) { cnts[t][i][j] += cnts[t][i - 1][j]; } } continue; printf("%lld:\n", W); for (int i = 0; i <= 2 * N; ++i) { for (int j = 0; j <= 2 * N; ++j){ printf("%d ", cnts[t][i][j]); } puts(""); } puts(""); } for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { if (rect(2, i, j, i + aux, j + aux) && rect(1, i + aux, j, i + D, j + aux) && rect(1, i, j + aux, i + aux, j + D) && rect(0, i + aux, j + aux, i + D, j + D)) { return true; } } } return false; } int main() { scanf("%d%d", &N, &D); for (int i = 0; i < N; ++i) scanf("%d%d", X + i, Y + i); for (int i = 0; i < N; ++i) { ++grp[X[i] % D][Y[i] % D]; } i64 left = 2, right = 1001001001; while (left < right) { i64 mid = (left + right) / 2; if (test(mid)) { right = mid; } else { left = mid + 1; } } printf("%lld\n", left - 1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; template <typename T> void chmin(T&x,const T &y) { if(x>y)x=y; } template <typename T> void chmax(T &x,const T &y) { if(x<y)x=y; } typedef long long s64; typedef unsigned long long u64; typedef unsigned int u32; typedef pair<int,int> pii; #define rep(i,l,r) for(int i=l;i<=r;++i) #define per(i,r,l) for(int i=r;i>=l;--i) #define rep0(i,l,r) for(int i=l;i<r;++i) #define gc (c=getchar()) int read() { char c; while(gc<'-'); if(c=='-') { int x=gc-'0'; while(gc>='0')x=x*10+c-'0'; return -x; } int x=c-'0'; while(gc>='0')x=x*10+c-'0'; return x; } #undef gc const int N=1e5+5,D=1e3+5; int cnt[D][D],s[D][D]; int d; void add(int x1,int x2,int y1,int y2,int w) { chmax(x1,0);chmin(x2,d-1); chmax(y1,0);chmin(y2,d-1); if(x1>x2||y1>y2)return ; s[x2][y2]+=w; if(x1)s[x1-1][y2]-=w; if(y1)s[x2][y1-1]-=w; if(x1&&y1)s[x1-1][y1-1]+=w; } int main() { #ifdef kcz freopen("1.in","r",stdin);//freopen("1.out","w",stdout); #endif int n=read();d=read(); rep(i,1,n) { int x=read(),y=read(); ++cnt[x%d][y%d]; } int l=-1,r=1e9; while(l+1!=r) { memset(s,0,sizeof(s)); int mid=(l+r)/2; bool OK=0; s64 base=mid/d;int mo=mid%d; rep(x,0,d-1) rep(y,0,d-1) if(base*base>=cnt[x][y])add(0,d-1,0,d-1,1); else if(base*(base+1)>=cnt[x][y]) { add(0,d-1,0,d-1,1); rep(i,0,1) rep(j,0,1)add(mo-x+1+d*i,d-x-1+d*i,mo-y+1+d*j,d-y-1+d*j,-1); } else if((base+1)*(base+1)>=cnt[x][y]) { rep(i,0,1) rep(j,0,1)add(-x+d*i,mo-x+d*i,-y+d*j,mo-y+d*j,1); } per(x,d-1,0) per(y,d-1,0)s[x][y]+=s[x][y+1]; per(x,d-1,0) per(y,d-1,0)s[x][y]+=s[x+1][y]; per(x,d-1,0) per(y,d-1,0)OK|=s[x][y]==d*d; if(OK)r=mid; else l=mid; } cout<<r; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb emplace_back #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) a.erase(unique(all(a)),a.end()) typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; typedef multiset<ll> S; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} ll sum1[2000][2000],sum2[2000][2000]; ll get1(int i,int j,int k,int l){ if(k==0||l==0)return 0; ll res=sum1[k-1][l-1]; if(i)res-=sum1[i-1][l-1]; if(j)res-=sum1[k-1][j-1]; if(i&&j)res+=sum1[i-1][j-1]; return res; } ll get2(int i,int j,int k,int l){ if(k==0||l==0)return 0; ll res=sum2[k-1][l-1]; if(i)res-=sum2[i-1][l-1]; if(j)res-=sum2[k-1][j-1]; if(i&&j)res+=sum2[i-1][j-1]; return res; } int main(){ ll n,d;cin>>n>>d; vvi num(d,vi(d)); rep(i,n){ ll x,y;cin>>x>>y; num[x%d][y%d]++; } ll ok=1000000,ng=0; while(ok-ng>1){ ll md=(ok+ng)/2; bool able=false; ll a=md/d,b=md%d; rep(i,d*2)rep(j,d*2){sum1[i][j]=0;sum2[i][j]=0;} rep(i,d)rep(j,d){ if(num[i][j]>(a+1)*(a+1))able=true; if(num[i][j]>a*a){ sum1[i][j]++; sum1[i+d][j]++; sum1[i][j+d]++; sum1[i+d][j+d]++; } if(num[i][j]>a*(a+1)){ sum2[i][j]++; sum2[i+d][j]++; sum2[i][j+d]++; sum2[i+d][j+d]++; } } rep(i,d*2)rep(j,d*2-1){ sum1[i][j+1]+=sum1[i][j]; sum2[i][j+1]+=sum2[i][j]; } rep(j,d*2)rep(i,d*2-1){ sum1[i+1][j]+=sum1[i][j]; sum2[i+1][j]+=sum2[i][j]; } if(able){ ng=md;continue; } rep(i,d)rep(j,d){ if(get1(i+b,j+b,i+d,j+d)==0&&get2(i,j,i+d,j+d)==get2(i,j,i+b,j+b))able=true; } if(able)ok=md; else ng=md; } out(ok-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
const long long MOD = 998244353; const int N = 2010; #include <iostream> #include <vector> #include <string> #include <map> #include <set> #include <algorithm> #include <cmath> #include <time.h> #include <unordered_set> #include <bitset> #include <deque> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<ll> vi; #define forn(i, n) for (int (i) = 0; (i) != (n); (i)++) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define popcount(x) __builtin_popcount(x) #define popcountll(x) __builtin_popcountll(x) #define fi first #define se second #define re return #define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin()) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl #define ln cerr << __LINE__ << endl #else #define dbg(x) void(0) #define ln void(0) #endif // LOCAL int cx[4] = {-1, 0, 1, 0}; int cy[4] = {0, -1, 0, 1}; ll inq(ll x, ll y) { if (!y) re 1 % MOD; ll l = inq(x, y / 2); if (y % 2) re l * l % MOD * x % MOD; re l * l % MOD; } ll rev(ll x) { return inq(x, MOD - 2); } bool __precomputed_combinatorics = 0; vector<ll> __fact, __ufact, __rev; void __precompute_combinatorics() { __precomputed_combinatorics = 1; __fact.resize(N); __ufact.resize(N); __rev.resize(N); __rev[1] = 1; for (int i = 2; i < N; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD; __fact[0] = 1, __ufact[0] = 1; for (int i = 1; i < N; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD; } ll fact(int x) { if (!__precomputed_combinatorics) __precompute_combinatorics(); return __fact[x]; } ll cnk(int n, int k) { if (k < 0 || k > n) return 0; if (!__precomputed_combinatorics) __precompute_combinatorics(); return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD; } int n, d; int c[N][N]; int p1[N][N]; int p2[N][N]; int g1(int i, int j) { if (i >= 0 && j >= 0) return p1[i][j]; return 0; } int g2(int i, int j) { if (i >= 0 && j >= 0) return p2[i][j]; return 0; } int get1(int i1, int j1, int i2, int j2) { i2--, j2--; return g1(i2, j2) - g1(i1 - 1, j2) - g1(i2, j1 - 1) + g1(i1 - 1, j1 - 1); } int get2(int i1, int j1, int i2, int j2) { i2--, j2--; return g2(i2, j2) - g2(i1 - 1, j2) - g2(i2, j1 - 1) + g2(i1 - 1, j1 - 1); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> d; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; x = (x % d + d) % d; y = (y % d + d) % d; c[x][y]++; } int mx = 0; for (int i = 0; i < d; i++) for (int j = 0; j < d; j++) mx = max(mx, c[i][j]); int A = 0; for (A = 0; ; A++) if ((A + 1) * (A + 1) >= mx) break; int c1 = 0, c2 = 0; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { if (c[i][j] > (A + 1) * A) c2++, p2[i][j]++; if (c[i][j] > A * A) c1++, p1[i][j]++; } } for (int i = 0; i < 2 * d; i++) { for (int j = 0; j < 2 * d; j++) { p1[i][j] = p1[i % d][j % d]; p2[i][j] = p2[i % d][j % d]; } } for (int i = 0; i < 2 * d; i++) { for (int j = 0; j < 2 * d; j++) { p1[i][j] = p1[i][j] + (i ? p1[i - 1][j] : 0) + (j ? p1[i][j - 1] : 0) - (i && j ? p1[i - 1][j - 1] : 0); p2[i][j] = p2[i][j] + (i ? p2[i - 1][j] : 0) + (j ? p2[i][j - 1] : 0) - (i && j ? p2[i - 1][j - 1] : 0); } } int ans = 1e9; //cout << c1 << " " << c2 << endl << endl; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { int L = A * d - 1, R = A * d + d; //cout << L << " " << R << endl; while (L + 1 < R) { int M = (L + R) / 2; int b = M % d; int a = M / d; if (a != A) exit(1); int cnt2 = get2(i, j, i + b, j + b); int cnt1 = get1(i, j, i + b, j + b) + get1(i, j + b, i + b, j + d) + get1(i + b, j, i + d, j + b); //cout << i << " " << j << " " << M << " " << cnt1 << " " << cnt2 << endl; if (cnt1 == c1 && cnt2 == c2) R = M; else L = M; } ans = min(ans, R); } } cout << ans - 1; } /* Note: Check constants at the beginning of the code. N is set to 4e5 but be careful in problems with large constant factor. Setting N in every problem is more effective. Check corner cases. N = 1 No def int long long for now. Add something here. */
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<bitset> #include<stack> #include<memory> using namespace std; using namespace std::chrono; typedef long long int llint; typedef double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase /* cout<<fixed<<setprecision(20); cin.tie(0); ios::sync_with_stdio(false); */ const llint mod=1000000007; const llint big=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-15; template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} int main(void){ cin.tie(0); ios::sync_with_stdio(false); llint n,D,i,j,x,y;cin>>n>>D; static int kaz[1001][1001]={0}; for(i=0;i<n;i++){cin>>x>>y;kaz[y%D][x%D]++;} int syo=0; for(y=0;y<D;y++){ for(x=0;x<D;x++){maxeq(syo,kaz[y][x]);} } syo=(int)(sqrt(syo)+0.999999); //syoが最大のあれ トーラスに気を付ける //kazの意味を変える for(y=0;y<D;y++){ for(x=0;x<D;x++){ //cerr<<kaz[y][x]<<" "; if(kaz[y][x]>syo*(syo-1)){kaz[y][x]=2;} else if(kaz[y][x]>(syo-1)*(syo-1)){kaz[y][x]=1;} else {kaz[y][x]=0;} }//cerr<<endl; } //区間内に2や1があるかどうかを累積和で調べる static int niwa[2001][2001]={0}; static int itwa[2001][2001]={0}; for(y=0;y<D+D;y++){ for(x=0;x<D+D;x++){ niwa[y+1][x+1]=niwa[y+1][x]; itwa[y+1][x+1]=itwa[y+1][x]; if(kaz[y%D][x%D]==2){niwa[y+1][x+1]++;} if(kaz[y%D][x%D]>=1){itwa[y+1][x+1]++;} } } for(y=0;y<D+D;y++){ for(x=0;x<D+D;x++){ niwa[y+1][x+1]+=niwa[y][x+1]; itwa[y+1][x+1]+=itwa[y][x+1]; } } int bmin=0,bmax=D; int nisu=niwa[D][D];//全体の2の数 while(bmax-bmin>1){ int b=(bmin+bmax)/2; bool can=0; for(y=0;y<D;y++){ for(x=0;x<D;x++){ if(nisu==niwa[y+b][x+b]+niwa[y][x]-niwa[y+b][x]-niwa[y][x+b]&& 0==itwa[y+D][x+D]+itwa[y+b][x+b]-itwa[y+D][x+b]-itwa[y+b][x+D]) {can=1;break;} } } if(can){bmax=b;} else{bmin=b;} } cout<<(syo-1)*D+bmax-1<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #define infll 0x3f3f3f3f3f3f3f3fll using namespace std; typedef long long ll; template <typename Tp> inline void getint(Tp &num){ register int ch, neg = 0; while(!isdigit(ch = getchar())) if(ch == '-') neg = 1; num = ch & 15; while(isdigit(ch = getchar())) num = num * 10 + (ch & 15); if(neg) num = -num; } int N, D, A[1005][1005], k; ll ans = infll; int f[2005][2005], g[2005][2005], sf[2005][2005], sg[2005][2005]; inline bool check(int a[][2005], const int &lx, const int &ly, const int &rx, const int &ry){ if(lx > rx || ly > ry) return 1; return a[rx][ry] - (ly ? a[rx][ly - 1] : 0) - (lx ? a[lx - 1][ry] : 0) + (lx && ly ? a[lx - 1][ly - 1] : 0) == (rx - lx + 1) * (ry - ly + 1); } inline bool check(const int &x, const int &y, const int &b){ return check(sf, x + b + 1, y + b + 1, x + D - 1, y + D - 1) && check(sg, x, y + b + 1, x + D - 1, y + D - 1) && check(sg, x + b + 1, y, x + D - 1, y + D - 1); } inline ll calc(const int &x, const int &y){ int l = 0, r = D - 1; while(l < r){ const int mid = l + r >> 1; if(check(x, y, mid)) r = mid; else l = mid + 1; } return k * (ll)D + l; } int main(){ getint(N), getint(D); if(N == 1) return puts("0"), 0; for(register int i = 1, x, y; i <= N; i++) getint(x), getint(y), A[x % D][y % D]++; for(register int i = 0; i < D; i++) for(register int j = 0; j < D; j++) k = max(k, A[i][j]); k = (int)sqrt(k - 0.5); for(register int i = 0, F = k * k, G = k * (k + 1); i < D; i++) for(register int j = 0; j < D; j++){ f[i][j] = f[i + D][j] = f[i][j + D] = f[i + D][j + D] = A[i][j] <= F; g[i][j] = g[i + D][j] = g[i][j + D] = g[i + D][j + D] = A[i][j] <= G; } sf[0][0] = f[0][0], sg[0][0] = g[0][0]; for(register int i = 1; i < (D << 1); i++){ sf[0][i] = sf[0][i - 1] + f[0][i], sg[0][i] = sg[0][i - 1] + g[0][i]; sf[i][0] = sf[i - 1][0] + f[i][0], sg[i][0] = sg[i - 1][0] + g[i][0]; } for(register int i = 1; i < (D << 1); i++) for(register int j = 1; j < (D << 1); j++){ sf[i][j] = sf[i][j - 1] + sf[i - 1][j] - sf[i - 1][j - 1] + f[i][j]; sg[i][j] = sg[i][j - 1] + sg[i - 1][j] - sg[i - 1][j - 1] + g[i][j]; } for(register int i = 0; i < D; i++) for(register int j = 0; j < D; j++) ans = min(ans, calc(i, j)); return printf("%lld\n", ans), 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> using namespace std; #define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i ) #define rep(i,n) REP(i,0,n) typedef long long ll; typedef pair<int,int> pint; typedef pair<ll,int> pli; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; int cnt[1010][1010]; int get(vector<vector<int>> &v,int x1,int y1,int x2,int y2){ return v[x2][y2]+v[x1][y1]-v[x2][y1]-v[x1][y2]; } int main(){ int n,d; cin>>n>>d; rep(i,n){ int x,y; cin>>x>>y; cnt[x%d][y%d]++; } int ma=0; rep(i,d)rep(j,d){ int k=1; int rt=sqrt(cnt[i][j]); if(rt*rt<cnt[i][j])++rt; ma=max(rt,ma); } vector<vector<int>> sum1(2*d+2,vector<int>(2*d+2)),sum2(2*d+2,vector<int>(2*d+2)); int tot1=0,tot2=0; rep(i,d)rep(j,d){ if(cnt[i][j]>ma*(ma-1)){ ++sum1[i][j], ++sum1[i+d][j], ++sum1[i][j+d], ++sum1[i+d][j+d], ++tot1; } else if(cnt[i][j]>(ma-1)*(ma-1)){ ++sum2[i][j], ++sum2[i+d][j], ++sum2[i][j+d], ++sum2[i+d][j+d], ++tot2; } } rep(i,2*d)rep(j,2*d){ sum1[i+1][j]+=sum1[i][j]; sum2[i+1][j]+=sum2[i][j]; } rep(i,2*d)rep(j,2*d){ sum1[i][j+1]+=sum1[i][j]; sum2[i][j+1]+=sum2[i][j]; } int ok=d,ng=-1,mid; while(ok-ng>1){ mid=(ok+ng)/2; bool can=false; rep(i,d)rep(j,d){ if(get(sum1,i,j,i+mid,j+mid)==tot1&& get(sum2,i+mid,j+mid,i+d,j+d)==0){ can=true; break; } } if(can)ok=mid; else ng=mid; } cout<<(ma-1)*d+ok-1 <<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define eps 1e-8 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int cnt[1005][1005],N,D,a; int s2[1005][1005],s3[1005][1005]; int sum2(int x1,int y1,int x2,int y2) { if(x1 > x2 || y1 > y2) return 0; ++x1,++y1,++x2,++y2; return s2[x2][y2] - s2[x2][y1 - 1] - s2[x1 - 1][y2] + s2[x1 - 1][y1 - 1]; } int sum3(int x1,int y1,int x2,int y2) { if(x1 > x2 || y1 > y2) return 0; ++x1,++y1,++x2,++y2; return s3[x2][y2] - s3[x2][y1 - 1] - s3[x1 - 1][y2] + s3[x1 - 1][y1 - 1]; } bool check2(int x,int y,int b) { int d1 = sum2(max(0,b + x - D + 1),max(0,b + y - D + 1),x - 1,y - 1); int d2 = sum2(x + b + 1,y + b + 1,D - 1,D - 1); int d3 = sum2(max(0,b + x - D + 1),y + b + 1,x - 1,D - 1); int d4 = sum2(x + b + 1,max(0,b + y - D + 1),D - 1,y - 1); return !d1 && !d2 && !d3 && !d4; } bool check3(int x,int y,int b) { int d1 = sum3(x + b + 1,0,D - 1,D - 1); int d2 = sum3(0,y + b + 1,D - 1,D - 1); int d3 = sum3(max(0,b + x - D + 1),0,x - 1,D - 1); int d4 = sum3(0,max(0,b + y - D + 1),D - 1,y - 1); return !d1 && !d2 && !d3 && !d4; } void Solve() { read(N);read(D); int x,y; for(int i = 1 ; i <= N ; ++i) { read(x);read(y); cnt[x % D][y % D]++; } int t = 0; for(int i = 0 ; i < D ; ++i) { for(int j = 0 ; j < D ; ++j) { if(cnt[i][j]) { t = sqrt(cnt[i][j]); if(t * t < cnt[i][j]) ++t; a = max(t - 1,a); } } } for(int i = 0 ; i < D ; ++i) { for(int j = 0 ; j < D ; ++j) { if(cnt[i][j] > a * a && cnt[i][j] <= a * (a + 1)) s2[i + 1][j + 1]++; else if(cnt[i][j] > a * (a + 1)) s3[i + 1][j + 1]++; } } for(int i = 1 ; i <= D ; ++i) { for(int j = 1 ; j <= D ; ++j) { s2[i][j] = s2[i][j] + s2[i - 1][j] + s2[i][j - 1] - s2[i - 1][j - 1]; s3[i][j] = s3[i][j] + s3[i - 1][j] + s3[i][j - 1] - s3[i - 1][j - 1]; } } int b; for(b = 0 ; b < D ; ++b) { if(check2(0,0,b) && check3(0,0,b)) break; } for(int i = 0 ; i < D ; ++i) { for(int j = 0 ; j < D ; ++j) { if(!(i + j)) continue; while(b) { if(check2(i,j,b - 1) && check3(i,j,b - 1)) --b; else break; } } } out(a * D + b);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<map> #include<set> #include<string> #include<stack> #include<cstdio> #include<cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> P; typedef pair<int,P> P1; #define fr first #define sc second #define mp make_pair #define pb push_back #define rep(i,x) for(int i=0;i<x;i++) #define rep1(i,x) for(int i=1;i<=x;i++) #define rrep(i,x) for(int i=x-1;i>=0;i--) #define rrep1(i,x) for(int i=x;i>0;i--) #define sor(v) sort(v.begin(),v.end()) #define rev(s) reverse(s.begin(),s.end()) #define lb(vec,a) lower_bound(vec.begin(),vec.end(),a) #define ub(vec,a) upper_bound(vec.begin(),vec.end(),a) #define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end()) #define mp1(a,b,c) P1(a,P(b,c)) const int INF=1000000000; const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}}; int main(){ static int N,D; static int x[100010],y[100010]; scanf("%d%d",&N,&D); for(int i = 1 ; i <= N ; i ++){ scanf("%d%d",&x[i],&y[i]); } static int cnt[1002][1002] = {}; for(int i = 1 ; i <= N ; i ++){ cnt[x[i]%D][y[i]%D] ++; } static int A[1002][1002] = {},B[1002][1002] = {}; for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ while(1){ if(A[i][j]*B[i][j] >= cnt[i][j])break; A[i][j] ++; if(A[i][j]*B[i][j] >= cnt[i][j])break; B[i][j] ++; } } } int M = 0, l = 0, r = D-1; for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ M = max( M , A[i][j]-1 ); } } static int imos[1002][1002] = {}; while(l < r){ int m = (l+r)/2; for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ imos[i][j] = 0; } } int CNT = 0; for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ if(A[i][j] == M+1){ CNT ++; if(B[i][j] == M+1){ vector<P> X; vector<P> Y; if(i < m){ X.push_back(P(i-m+D,D-1)); X.push_back(P(0,i)); } else X.push_back(P(i-m,i)); if(j < m){ Y.push_back(P(j-m+D,D-1)); Y.push_back(P(0,j)); } else Y.push_back(P(j-m,j)); for(P p: X)for(P q: Y){ imos[p.fr][q.fr] ++; imos[p.fr][q.sc+1] --; imos[p.sc+1][q.fr] --; imos[p.sc+1][q.sc+1] ++; } } else { vector<P> X; vector<P> Y; if(i < m){ X.push_back(P(i+1,i-m+D-1)); } else { X.push_back(P(0,i-m-1)); X.push_back(P(i+1,D-1)); } if(j < m){ Y.push_back(P(j+1,j-m+D-1)); } else { Y.push_back(P(0,j-m-1)); Y.push_back(P(j+1,D-1)); } for(P p: X)for(P q: Y){ imos[p.fr][q.fr] --; imos[p.fr][q.sc+1] ++; imos[p.sc+1][q.fr] ++; imos[p.sc+1][q.sc+1] --; } imos[0][0] ++; imos[0][D] --; imos[D][0] --; imos[D][D] ++; } } } } for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ imos[i+1][j] += imos[i][j]; } } for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ imos[i][j+1] += imos[i][j]; } } bool b = false; for(int i = 0 ; i < D ; i ++){ for(int j = 0 ; j < D ; j ++){ if(imos[i][j] == CNT){ b = true; break; } } } if(b)r = m; else l = m+1; } cout << M*D+l << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <algorithm> #include <vector> #include <string.h> using namespace std; template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void POSS(T condition){ if(condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; } template<class T> inline void Poss(T condition){ if(condition) cout << "Possible" << endl; else cout << "Impossible" << endl; } template<class T> inline void First(T condition){ if(condition) cout << "First" << endl; else cout << "Second" << endl; } template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; } long power(long base, long exponent, long module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }} struct position{ int y, x; }; position mv[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++){ ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; } template<class itr> void cins(itr start, itr goal){ for(auto i = start; i != goal; i++){ cin >> (*i); } } template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; } #define mod long(1e9 + 7) #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl(long(n)) #define fcout cout << fixed << setprecision(20) #define highest(x) (63 - __builtin_clzl(x)) template< class T > struct CumulativeSum2D { vector< vector< T > > data; CumulativeSum2D(int W, int H) : data(W + 1, vector< int >(H + 1, 0)) {} void add(int x, int y, int z) { ++x, ++y; if(x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build() { for(int i = 1; i < data.size(); i++) { for(int j = 1; j < data[i].size(); j++) { data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(int sx, int sy, int gx, int gy) { return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; int main(){ int N, D; cin >> N >> D; int counts[D][D]; memset(counts, 0, sizeof(counts)); for(int i = 0; i < N; i++){ int x, y; cin >> x >> y; counts[x % D][y % D]++; } int maximum = 0; for(int i = 0; i < D; i++){ for(int j = 0; j < D; j++){ maximum = max(maximum, counts[i][j]); } } int maximum_sqrt = 0; while((maximum_sqrt + 1) * (maximum_sqrt + 1) < maximum){ maximum_sqrt++; } CumulativeSum2D<int> first_overflow(D * 2, D * 2); // maximum_sqrt * maximum_sqrt < counts[i][j] CumulativeSum2D<int> second_overflow(D * 2, D * 2); // maximum_sqrt * (maximum_sqrt + 1) < counts[i][j] for(int i = 0; i < D * 2; i++){ for(int j = 0; j < D * 2; j++){ first_overflow.add(i, j, maximum_sqrt * maximum_sqrt < counts[i % D][j % D]); second_overflow.add(i, j, maximum_sqrt * (maximum_sqrt + 1) < counts[i % D][j % D]); } } first_overflow.build(); second_overflow.build(); int second_overflow_sum = second_overflow.query(0, 0, D, D); int ans = 1e9; for(int i = 0; i < D; i++){ for(int j = 0; j < D; j++){ int left = -1, right = D - 1; // (left, right] while(left + 1 < right){ int middle = (left + right) / 2; if(second_overflow.query(i, j, i + middle + 1, j + middle + 1) == second_overflow_sum && first_overflow.query(i + middle + 1, j + middle + 1, i + D, j + D) == 0){ right = middle; }else{ left = middle; } } ans = min(ans, right); } } cout << ans + maximum_sqrt * D << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include "bits/stdc++.h" using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define LL long long int n,d; LL x,y; int cnt[1000][1000]; int counter[3][2001][2001]; LL nums(int sp,int x,int y,int z,int w){ return counter[sp][z][w]+counter[sp][x][y]-counter[sp][x][w]-counter[sp][z][y]; } bool test(LL len){ LL a = len/d+1; int b = (int)(len%d)+1; LL mask[3] = {a*a,a*(a-1),(a-1)*(a-1)}; REP(sp,3){ REP(i,2*d){ REP(j,2*d){ if(cnt[i%d][j%d] > mask[sp]) counter[sp][i][j]=1; else counter[sp][i][j]=0; if(i>0)counter[sp][i][j]+=counter[sp][i-1][j]; if(j>0)counter[sp][i][j]+=counter[sp][i][j-1]; if(i>0&&j>0)counter[sp][i][j]-=counter[sp][i-1][j-1]; } } } /* REP(i,2*d){ REP(j,2*d){ cerr<<counter[1][i][j]<<" "; } cerr<<endl; } REP(i,2*d){ REP(j,2*d){ cerr<<counter[2][i][j]<<" "; } cerr<<endl; } */ REP(i,d){ REP(j,d){ if(nums(0,i,j,i+b,j+b)>0)continue; if(nums(1,i+b,j,i+d,j+b)>0)continue; if(nums(1,i,j+b,i+b,j+d)>0)continue; if(nums(2,i+b,j+b,i+d,j+d)==0){ return true; } } } return false; } int solve(){ LL lb=1,ub=n*d; LL mid; while(lb<ub){ mid = (lb+ub)/2; if(test(mid)){ ub=mid; }else{ lb=mid+1; } } return lb; } int main(){ cin>>n>>d; REP(i,d){ REP(j,d) cnt[i][j]=0; } REP(i,n){ scanf("%d%d",&x,&y); cnt[x%d][y%d]+=1; } cout<<solve()<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(),v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL<<(n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; #define mdebug(m) cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; } template<typename T> void Out(T x) { cout << x << endl; } template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; //int dx[4] = {1,0,-1,0}; //int dy[4] = {0,1,0,-1}; //char d[4] = {'D','R','U','L'}; const int mod = 1000000007; //const int mod = 998244353; //#define Add(x, y) x = (x + (y)) % mod //#define Mult(x, y) x = (x * (y)) % mod int usqrt(int x){ FOR(i, 1, x + 1) if(i * i >= x) return i; return -1; } signed main(){ int N, D; cin >> N >> D; map<Pii, int> cnt; REP(i, N){ int x, y; cin >> x >> y; cnt[Pii(x % D, y % D)] += 1; } int S0 = -1, S1 = bit(20) - 1; mat a(2 * D, vec(2 * D, 0)); while(S1 - S0 > 1){ int S = (S0 + S1) / 2, T = S / D, r = S % D; REP(i, 2 * D) fill(all(a[i]), 0); for(PPi t: cnt){ int x = t.f.f, y = t.f.s, n = t.s; if(T * T >= n){ a[D - 1][D - 1]++; a[2 * D - 1][D - 1]--; a[D - 1][2 * D - 1]--; a[2 * D - 1][2 * D - 1]++; }else if(T * (T + 1) >= n){ a[D - 1][D - 1]++; a[2 * D - 1][D - 1]--; a[D - 1][2 * D - 1]--; a[2 * D - 1][2 * D - 1]++; a[x][y]--; a[x - r + D - 1][y]++; a[x][y - r + D - 1]++; a[x - r + D - 1][y - r + D - 1]--; }else if((T + 1) * (T + 1) >= n){ a[x - r + D - 1][y - r + D - 1]++; a[x + D][y - r + D - 1]--; a[x - r + D - 1][y + D]--; a[x + D][y + D]++; } } REP(i, 2 * D) REP(j, 2 * D - 1) a[i][j + 1] += a[i][j]; REP(j, 2 * D) REP(i, 2 * D - 1) a[i + 1][j] += a[i][j]; bool f = false; REP(i, D) REP(j, D) if(a[i][j] + a[i + D][j] + a[i][j + D] + a[i + D][j + D] >= SZ(cnt)) f = true; if(f) S1 = S; else S0 = S; } Out(S1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <functional> #include <set> #include <map> #include <string> #include <cmath> #include <cassert> #define MX 1005 #define INF 1000000005 #define EPS 1e-10 using namespace std; typedef long long int ll; typedef pair <int,int> P; int cnt[MX][MX]; int dpx[MX],dpy[MX]; int tx[MX],ty[MX]; bool use[MX]; int n,D,mx; bool check(int X) { for(int i=0;i<D;i++) ty[i]=-1; for(int i=0;i<D;i++) { for(int j=0;j<D;j++) { if(cnt[i][j]>mx*mx) { ty[j]=i; } } } for(int i=0;i<D;i++) { int x=-1; for(int j=0;j<D;j++) { if(ty[j]<=i+X) use[j]=false; else { use[j]=true; x=max(x,j); } } for(int j=0;j<D;j++) { if(dpx[i]<=X&&dpy[j]<=X&&x-j<=X) return true; if(use[j]) x=j+D; } for(int j=0;j<D;j++) { if(cnt[i][j]>mx*mx) { ty[j]=i+D; } } } return false; } int main() { scanf("%d %d",&n,&D); for(int i=0;i<n;i++) { int x,y; scanf("%d %d",&x,&y); cnt[x%D][y%D]++; } mx=0; for(int i=0;i<D;i++) { for(int j=0;j<D;j++) { if(cnt[i][j]>0) { int t=(int) ceil(sqrt(cnt[i][j])-EPS); t--; mx=max(mx,t); } else cnt[i][j]=-1; } } for(int i=0;i<D;i++) { int to=-1; for(int j=0;j<D;j++) { if(cnt[i][j]>mx*(mx+1)) { to=j; } } if(to>=0) { for(int j=0;j<D;j++) { dpy[j]=max(dpy[j],to-j); if(cnt[i][j]>mx*(mx+1)) to=j+D; } } } for(int i=0;i<D;i++) { int to=-1; for(int j=0;j<D;j++) { if(cnt[j][i]>mx*(mx+1)) { to=j; } } if(to>=0) { for(int j=0;j<D;j++) { dpx[j]=max(dpx[j],to-j); if(cnt[j][i]>mx*(mx+1)) to=j+D; } } } int l=-1,r=D; while(r-l>1) { int d=(l+r)/2; if(check(d)) r=d; else l=d; } printf("%lld\n",(ll) mx*(ll) D+r); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<x<<endl #define mmax(x,y) (x>y?x:y) #define mmin(x,y) (x<y?x:y) #define maxch(x,y) x=mmax(x,y) #define minch(x,y) x=mmin(x,y) #define uni(x) x.erase(unique(all(x)),x.end()) #define exist(x,y) (find(all(x),y)!=x.end()) #define bcnt __builtin_popcountll #define INF INT_MAX/3 #define mod 1000000007 int N,D; int ps[1000][1000]; int main(){ scanf("%d%d",&N,&D); vector<vector<int>> f(2*D,vector<int>(2*D)); ll mx=0; rep(i,N){ int x,y; scanf("%d%d",&x,&y); x%=D; y%=D; ps[x][y]++; } rep(i,D)rep(j,D){ maxch(mx,ps[i][j]); rep(k,2)rep(l,2){ f[i+k*D][j+l*D]=ps[i][j]; } } int M=0; while((M+1)*(M+1)<mx)M++; vector<vector<int>> table0(2*D+1,vector<int>(2*D+1,0)); vector<vector<int>> table1(2*D+1,vector<int>(2*D+1,0)); vector<vector<int>> table2(2*D+1,vector<int>(2*D+1,0)); rep(i,2*D)rep(j,2*D){ table0[i+1][j+1]=((M+1)*(M+1)-f[i][j]<0?1:0); table1[i+1][j+1]=(M*(M+1)-f[i][j]<0?1:0); table2[i+1][j+1]=(M*M-f[i][j]<0?1:0); } rep(i,2*D+1)rep(j,2*D){ table0[i][j+1]+=table0[i][j]; table1[i][j+1]+=table1[i][j]; table2[i][j+1]+=table2[i][j]; } rep(i,2*D)rep(j,2*D+1){ table0[i+1][j]+=table0[i][j]; table1[i+1][j]+=table1[i][j]; table2[i+1][j]+=table2[i][j]; } int lb=-1,ub=D-1; while(ub-lb>1){ int X=(lb+ub)/2; int r=X+1; bool aok=false; rep(i,D)rep(j,D){ bool ok=true; if(table0[i+r][j+r]-table0[i+r][j]-table0[i][j+r]+table0[i][j]>0)ok=false; if(table1[i+D][j+r]-table1[i+D][j]-table1[i+r][j+r]+table1[i+r][j]>0)ok=false; if(table1[i+r][j+D]-table1[i][j+D]-table1[i+r][j+r]+table1[i][j+r]>0)ok=false; if(table2[i+D][j+D]-table2[i+r][j+D]-table2[i+D][j+r]+table2[i+r][j+r]>0)ok=false; if(ok)aok=true; } if(aok) ub=X; else lb=X; } printf("%d\n", D*M+ub); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; int n,d,f[2005][2005],x,y,i,j,l,r,s1[3005][3005],s2[3005][3005]; bool check(int m) { ++m; int l=m%d,i,j,t=m/d+1; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); for(i=0;i<2*d;i++) for(j=0;j<2*d;j++) { if(f[i][j]>1ll*(t-1)*(t-1)) s1[i][j]++; if(f[i][j]>1ll*t*(t-1)) s2[i][j]++; if(f[i][j]>1ll*t*t) return false; } for(i=0;i<2*d;++i) for(j=0;j<2*d;++j) if(j!=0) { s1[i][j]+=s1[i][j-1]; s2[i][j]+=s2[i][j-1]; } for(i=0;i<2*d;++i) for(j=0;j<2*d;++j) if(j!=0) { s1[j][i]+=s1[j-1][i]; s2[j][i]+=s2[j-1][i]; } for(i=d;i<2*d;++i) for(j=d;j<2*d;++j) if(s1[i-l][j-l]-s1[i-d][j-l]-s1[i-l][j-d]+s1[i-d][j-d]==0&&s2[i][j-l]-s2[i-l][j-l]-s2[i][j-d]+s2[i-l][j-d]==0&&s2[i-l][j]-s2[i-l][j-l]-s2[i-d][j]+s2[i-d][j-l]==0) return true; return false; } int main() { scanf("%d %d",&n,&d); for(i=1;i<=n;++i) { scanf("%d %d",&x,&y); ++f[(x%d+d)%d][(y%d+d)%d]; } for(i=0;i<2*d;++i) for(j=0;j<2*d;++j) f[i][j]=f[i%d][j%d]; l=0; r=10000000; while(l<r) { int mid=l+r>>1; if(check(mid)) r=mid; else l=mid+1; } cout<<l; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define uint unsigned #define db double #define pii pair<int,int> #define pll pair<ll,ll> #define pli pair<ll,int> #define vi vector<int> #define vpi vector<pii > #define IT iterator #define PB push_back #define MK make_pair #define LB lower_bound #define UB upper_bound #define y1 wzpakking #define fi first #define se second #define BG begin #define ED end #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define UPD(x,y) (((x)+=(y))>=mo?(x)-=mo:233)%s #define CLR(a,v) memset(a,v,sizeof(a)) #define CPY(a,b) memcpy(a,b,sizeof(a)) #define sqr(x) (1ll*x*x) #define LS3 k*2,l,mid #define RS3 k*2+1,mid+1,r #define LS5 k*2,l,mid,x,y #define RS5 k*2+1,mid+1,r,x,y #define GET pushdown(k);int mid=(l+r)/2 #define INF (1ll<<60) using namespace std; const int N=2005; int a[N][N],b[N][N],c[N][N]; int n,D; int calc(int x){ int l=0,r=400,ans=-1; while (l<=r){ int mid=(l+r)/2; if (mid*mid<x) ans=mid,l=mid+1; else r=mid-1; } return ans; } int SUMB(int l,int r,int x,int y){ if (l>r||x>y) return 0; return b[r][y]-(l?b[l-1][y]:0)-(x?b[r][x-1]:0)+(l&&x?b[l-1][x-1]:0); } int SUMC(int l,int r,int x,int y){ if (l>r||x>y) return 0; return c[r][y]-(l?c[l-1][y]:0)-(x?c[r][x-1]:0)+(l&&x?c[l-1][x-1]:0); } bool check(int mid,int v){ memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); int sum=0; For(i,0,D-1) For(j,0,D-1) if (a[i][j]) if (a[i][j]<=v) b[i][j]++; else c[i][j]++,sum++; For(i,0,2*D-1) For(j,0,2*D-1) b[i][j]=b[i%D][j%D],c[i][j]=c[i%D][j%D]; For(i,1,2*D-1) For(j,0,2*D-1) b[i][j]+=b[i-1][j],c[i][j]+=c[i-1][j]; For(i,0,2*D-1) For(j,1,2*D-1) b[i][j]+=b[i][j-1],c[i][j]+=c[i][j-1]; For(i,0,D-1) For(j,0,D-1){ int s1=SUMB(i+mid,i+D-1,j+mid,j+D-1); int s2=SUMC(i,i+mid-1,j,j+mid-1); if (s1==0&&s2==sum) return 1; } return 0; } int main(){ scanf("%d%d",&n,&D); For(i,1,n){ int x,y; scanf("%d%d",&x,&y); a[x%D][y%D]++; } int mx=0; For(i,0,D-1) For(j,0,D-1) mx=max(mx,calc(a[i][j])); For(i,0,D-1) For(j,0,D-1) a[i][j]=max(0,a[i][j]-mx*mx); int l=0,r=D,ans=23333; while (l<=r){ int mid=(l+r)/2; if (check(mid,mx)) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",mx*D+ans-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// vector<vector<int>> cou; vector<vector<int>> n, rect; lint D; bool solve(lint C) { vector<vector<int>> imos(D * 2 + 1, vector<int>(D * 2 + 1)); REP(i, D) REP(j, D) { lint nb = cou[i][j]; if ((C / D + 1) * (C / D + 1) < nb) return false; if (((C + 1) / D) * ((C + 1) / D) >= nb) { imos[0][0]++; continue; } int m = C % D; if (rect[i][j]) { imos[max(i - m, 0)][0]++; imos[i + 1][0]--; imos[i + D - m][0]++; imos[i + D + 1][0]--; imos[0][max(j - m, 0)]++; imos[0][j + 1]--; imos[0][j + D - m]++; imos[0][j + D + 1]--; } int add = rect[i][j] ? -1 : 1; for (int ofx = 0; ofx <= D; ofx += D) { for (int ofy = 0; ofy <= D; ofy += D) { int x = max(i - m + ofx, 0); int y = max(j - m + ofy, 0); imos[x][y] += add; imos[x][j + ofy + 1] -= add; imos[i + ofx + 1][y] -= add; imos[i + ofx + 1][j + ofy + 1] += add; } } } REP(i, 2 * D) REP(j, 2 * D) imos[i][j + 1] += imos[i][j]; REP(i, 2 * D) REP(j, 2 * D) imos[i + 1][j] += imos[i][j]; REP(i, 2 * D) REP(j, 2 * D) if (imos[i][j] >= D * D) return true; return false; } int main() { int N; cin >> N >> D; cou.resize(D, vector<int>(D)); while (N--) { int x, y; cin >> x >> y; cou[x % D][y % D]++; } n.resize(D, vector<int>(D)); rect.resize(D, vector<int>(D)); REP(i, D) REP(j, D) { while(n[i][j] * n[i][j] < cou[i][j]) n[i][j]++; if (n[i][j] * n[i][j] - cou[i][j] >= n[i][j]) rect[i][j] = 1; } lint l = 0, r = 1e8; while (r > l + 1) { lint c = (l + r) / 2; (solve(c) ? r : l) = c; } cout << r << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define MAX 1000 int N, D; int cnt[MAX][MAX]; int A[2][MAX][MAX]; int sum[2][MAX * 2 + 1][MAX * 2 + 1]; int count( int idx, int x1, int y1, int x2, int y2 ) { int res = sum[idx][x2][y2]; res -= sum[idx][x1 - 1][y2]; res -= sum[idx][x2][y1 -1]; res += sum[idx][x1 - 1][y1 - 1]; return res; } bool check( int b ) { for( int x = 1; x <= D; x++ ) { for( int y = 1; y <= D; y++ ) { if( count( 0, x + b + 1, y + b + 1, x + D - 1, y + D - 1 ) ) { continue; } if( count( 1, x + b + 1, y, x + D - 1, y + b ) ) { continue; } if( count( 1, x, y + b + 1, x + b, y + D - 1 ) ) { continue; } return true; } } return false; } int main() { cin >> N >> D; for( int i = 0; i < N; i++ ) { int x, y; cin >> x >> y; cnt[x % D][y % D]++; } int ma = 0; for( int i = 0; i < D; i++ ) { for( int j = 0; j < D; j++ ) { ma = max( ma, cnt[i][j] ); } } int R = ceil( sqrt( ma ) ) - 1; for( int i = 0; i < D; i++ ) { for( int j = 0; j < D; j++ ) { int c = cnt[i][j]; if( c > R * R ) { A[0][i][j]++; } if( c > R * (R + 1) ) { A[1][i][j]++; } } } for( int i = 1; i <= D * 2; i++ ) { for( int j = 1; j <= D * 2; j++ ) { sum[0][i][j] += sum[0][i][j - 1] + sum[0][i - 1][j] - sum[0][i - 1][j - 1] + A[0][(i - 1) % D][(j - 1) % D]; sum[1][i][j] += sum[1][i][j - 1] + sum[1][i - 1][j] - sum[1][i - 1][j - 1] + A[1][(i - 1) % D][(j - 1) % D]; } } int l = 0; int r = D; while( r > l ) { int m = (l + r) / 2; if( check( m ) ) { r = m; } else { l = m + 1; } } cout << R * D + r << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #include <sys/types.h> #include <unistd.h> #define _overload(_1,_2,_3,name,...) name #define _rep(i,n) _range(i,0,n) #define _range(i,a,b) for(int i=int(a);i<int(b);++i) #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__) #define _rrep(i,n) _rrange(i,n,0) #define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i) #define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__) #define _all(arg) begin(arg),end(arg) #define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg)) #define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary) #define clr(a,b) memset((a),(b),sizeof(a)) #define bit(n) (1LL<<(n)) #define popcount(n) (__builtin_popcountll(n)) using namespace std; template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;} template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;} using ll=long long; using R=long double; const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7 inline int sgn(const R& r){return(r > EPS)-(r < -EPS);} inline R sq(R x){return sqrt(max(x,0.0L));} const int dx[8]={1,0,-1,0,1,-1,-1,1}; const int dy[8]={0,1,0,-1,1,1,-1,-1}; const pid_t pid = getpid(); // Problem Specific Parameter: inline ll mul(ll a, ll b){ if(a == 0 or b == 0) return 0LL; const ll limit = 1LL << 20; if(a > limit or b > limit) return limit; return min<ll>(limit, a * b); } int ary[2010][2010]; int csum[3][2010][2010]; bool query(int k,int a,int b,int c,int d){ int total = (b - a) * (d - c); total -= csum[k][a][c]; total += csum[k][b][c]; total += csum[k][a][d]; total -= csum[k][b][d]; return (total == 0); } bool check(ll len,ll d){ vector<ll> limit; limit.push_back(mul(len / d + 1LL, len / d + 1LL)); limit.push_back(mul(len / d + 0LL, len / d + 1LL)); limit.push_back(mul(len / d + 0LL, len / d + 0LL)); rep(k,3){ rep(i,2 * d)rep(j, 2 * d){ csum[k][i][j] = (ary[i][j] <= limit[k]); } rrep(i, 2 * d)rrep(j, 2 * d){ csum[k][i][j] += csum[k][i+1][j]; csum[k][i][j] += csum[k][i][j+1]; csum[k][i][j] -= csum[k][i+1][j+1]; } } // (len / d) + (pos <= len % d) rep(i,d)rep(j,d){ const int im = i + len % d + 1; const int ie = i + d; const int jm = j + len % d + 1; const int je = j + d; bool ok = true; if(query(0,i,im,j,jm) == false) ok = false; if(query(1,im,ie,j,jm) == false) ok = false; if(query(1,i,im,jm,je) == false) ok = false; if(query(2,im,ie,jm,je) == false) ok = false; if(ok) return true; } return false; } int main(void){ int n,d; cin >> n >> d; vector<int> x(n),y(n); rep(i,n){ cin >> x[i] >> y[i]; ary[x[i] % d][y[i] % d]++; } rep(i,2*d)rep(j,2*d){ if(i < d and j < d) continue; ary[i][j] = ary[i % d][j % d]; } ll low = 0LL, high = 1LL << 40; while(high - low > 1){ const ll mid = (low + high) / 2LL; if(check(mid,d)) high = mid; else low = mid; } cout << high << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define DEBUG(x) cerr<<#x<<": "<<x<<endl; #define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl typedef long long ll; #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i<b;i++) #define in(x, a, b) (a <= x && x < b) #define all(c) c.begin(),c.end() template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = 1e18; const ll MOD = 1000000007; //const ll MOD = 998244353; const double pi = 3.14159265358979323846; #define Sp(p) cout<<setprecision(15)<< fixed<<p; int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 }; int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; #define fio() cin.tie(0); ios::sync_with_stdio(false); //#define mp make_pair int main() { ll n, d; cin >> n >> d; vii num(d, vi(d)); rep (i, n) { int x, y; cin >> x >> y; num[x % d][y % d]++; } /* cout << endl; rep (i, d) { rep (j, d) { cout << num[i][j] << " "; } cout << endl; } //*/ ll ans = INF; vector<vector<pii> > ab(d, vector<pii>(d)); int ma = 0; rep (i, d) { rep (j, d) { chmax(ma, num[i][j]); } } int a = max(0, (int)sqrt(ma) - 4); while (ma > (a + 1) * (a + 1)) a++; //DEBUG(a); vii a1(2*d, vi(2*d)), a2(2*d, vi(2*d)); rep (i, 2*d) { rep (j, 2*d) { a1[i][j] = num[i%d][j%d] > a*a; a2[i][j] = num[i%d][j%d] > a*(a + 1); } } vii sum1(2*d + 1, vi(2*d + 1)), sum2(2*d + 1, vi(2*d + 1)); rep (i, 2*d) { rep (j, 2*d) { sum1[i + 1][j + 1] = sum1[i + 1][j] + a1[i][j]; sum2[i + 1][j + 1] = sum2[i + 1][j] + a2[i][j]; } } rep (i, 2*d) { rep (j, 2*d) { sum1[i + 1][j + 1] += sum1[i][j + 1]; sum2[i + 1][j + 1] += sum2[i][j + 1]; } } int ng = -1, ok = d; while (ng + 1 < ok) { int b = (ng + ok) / 2; bool flag = false; rep (i, d) { rep (j, d) { int c1 = sum1[i + d][j + d] - sum1[i + d][j + b] - sum1[i + b][j + d] + sum1[i + b][j + b]; int c2 = sum2[i + d][j + b] - sum2[i + d][j] - sum2[i + b][j + b] + sum2[i + b][j]; int c3 = sum2[i + b][j + d] - sum2[i + b][j + b] - sum2[i][j + d] + sum2[i][j + b]; //DEBUG(c1); //DEBUG(c2); //DEBUG(c3); if (c1 == 0 && c2 + c3 == 0) { flag = true; } } } if (flag) { ok = b; } else { ng = b; } } cout << a * d + ok - 1 << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; using P = pair<int, int>; const int M = 1000000007; int main() { int n, d; cin >> n >> d; vector<vector<int>> cnt(d, vector<int>(d)); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; ++cnt[x % d][y % d]; } vector<vector<int>> ma(d, vector<int>(d)); vector<vector<int>> mi(d, vector<int>(d)); for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { int ng = -2, ok = 1000; while (ng < ok - 1) { int m = (ok + ng) / 2; if ((m + 1) * (m + 1) >= cnt[i][j]) { ok = m; } else { ng = m; } } ma[i][j] = ok; if ((ok + 1) * ok >= cnt[i][j]) { mi[i][j] = ok - 1; } else { mi[i][j] = ok; } } } vector<P> v; int mma = 0; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { mma = max(mma, ma[i][j]); } } vector<bool> vi(d, false), vj(d, false); for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { if (mi[i][j] == mma) { vi[i] = true; vj[j] = true; } else if (ma[i][j] == mma) { v.push_back(P(i, j)); } } } int ans = M; int mai = 0; for (int i = 0; i < d; ++i) { if (vi[i]) mai = i; } for (int i = 0; i < d; ++i) { int maj = 0; for (int j = 0; j < d; ++j) { if (vj[j]) maj = j; } for (int j = 0; j < d; ++j) { int maa = max(mai, maj) + mma * d; if (maa < ans) { for (auto& p : v) { int k = p.first, l = p.second; int ofsi = (i <= k ? k - i : k - i + d); int ofsj = (j <= l ? l - j : l - j + d); if (ofsi < ofsj) { maa = max({ maa, ofsi + ma[k][l] * d, ofsj + mi[k][l] * d }); } else { maa = max({ maa, ofsi + mi[k][l] * d, ofsj + ma[k][l] * d }); } } } ans = min(ans, maa); maj = max(0, maj - 1); if (vj[j]) { maj = d - 1; } } mai = max(0, mai - 1); if (vi[i]) { mai = d - 1; } } cout << ans << "\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; int cnt[1111][1111]; int rot[1210100]; typedef pair<int, int>pii; int maxis[1111]; int get(vector<pii>v1, vector<pii>v2, int hen) { for (int i = 0; i < hen; i++)maxis[i] = 0; for (int i = 0; i < v1.size(); i++) { if (v1[i].first < v1[i].second) { maxis[0] = max(maxis[0], v1[i].first); maxis[v1[i].first + 1] = max(maxis[v1[i].first + 1], v1[i].second); maxis[v1[i].second + 1] = max(maxis[v1[i].second + 1], v1[i].first + hen); } else { maxis[0] = max(maxis[0], v1[i].second); maxis[v1[i].second + 1] = max(maxis[v1[i].second + 1], v1[i].first); maxis[v1[i].first + 1] = max(maxis[v1[i].first + 1], v1[i].second + hen); } } for (int i = 0; i < v2.size(); i++) { if (v2[i].first < v2[i].second) { maxis[0] = max(maxis[0], v2[i].second); maxis[v2[i].first + 1] = max(maxis[v2[i].first + 1], v2[i].first+hen); maxis[v2[i].second + 1] = max(maxis[v2[i].second + 1], v2[i].second + hen); } else { maxis[0] = max(maxis[0], v2[i].first); maxis[v2[i].first + 1] = max(maxis[v2[i].first + 1], v2[i].first + hen); maxis[v2[i].second + 1] = max(maxis[v2[i].second + 1], v2[i].second + hen); } } int mini = 1000000000; for (int i = 0; i < hen; i++) { mini = min(mini, maxis[i] - i); maxis[i + 1] = max(maxis[i + 1], maxis[i]); } return mini; } int main() { int num, hen; scanf("%d%d", &num, &hen); for (int i = 0; i < num; i++) { int za, zb; scanf("%d%d", &za, &zb); cnt[za%hen][zb%hen]++; } for (int i = 1; i < 1050; i++) { for (int j = (i - 1)*(i - 1) + 1; j <= i*i; j++)rot[j] = i; } vector<pii>v1, v2; int maxi = -1; for (int i = 0; i < hen; i++) { for (int j = 0; j < hen; j++) { if (cnt[i][j]>0) { int l = rot[cnt[i][j]] - 1; if (maxi < l)v1.clear(), v2.clear(), maxi = l; if (maxi == l) { if (cnt[i][j] <= l*(l + 1))v1.push_back(make_pair(i, j)); else v2.push_back(make_pair(i, j)); } } } } int mini = 1000000000; for (int i = 0; i < hen; i++) { for (int j = 0; j < v1.size(); j++)v1[j].first = (v1[j].first + 1) % hen; for (int j = 0; j < v2.size(); j++)v2[j].first = (v2[j].first + 1) % hen; mini = min(mini, get(v1, v2, hen)); } printf("%d\n", mini + hen*maxi); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <string> #include <iomanip> #include <algorithm> #include <cmath> #include <stdio.h> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N, D; cin >> N >> D; vector<pair<int,int> > A(N); vector<vector<int> > C(D, vector<int>(D, 0)); for (int i = 0; i < N; i++) { cin >> A[i].first >> A[i].second; C[A[i].first % D][A[i].second % D]++; } int ok = 1e7; int ng = 0; int m; vector<vector<int> > X(2 * D + 1, vector<int>(2 * D + 1, 0)); int vcnt = 0; int t; while (ok - ng > 1) { m = (ok + ng) / 2; bool f = false; int div = (m + 1) / D; int bd = (m + 1) % D; for (int i = 0; i < X.size(); i++) { for (int j = 0; j < X.size(); j++) { X[i][j] = 0; } } bool bk = false; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (C[i][j] <= div*div) { } else if (C[i][j] <= (div + 1) * div) { X[i + bd][j + bd]++; X[i + D][j + bd]--; X[i + bd][j + D]--; X[i + D][j + D]++; } else if (C[i][j] <= (div + 1) * (div + 1)) { X[i][j]++; X[i + D][j]--; X[i][j + D]--; X[i + D][j + D]++; X[i][j]--; X[i + bd][j]++; X[i][j + bd]++; X[i + bd][j + bd]--; } else { bk = true; break; } } if (bk)break; } if (!bk) { for (int i = 0; i < 2 * D; i++) { for (int j = 0; j < 2 * D; j++) { if (i > 0)X[i][j] += X[i - 1][j]; if (j > 0)X[i][j] += X[i][j - 1]; if (i > 0 && j > 0)X[i][j] -= X[i - 1][j - 1]; } } //t = 0; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (X[i][j] + X[i + D][j] + X[i][j + D] + X[i + D][j + D] == 0) { f = true; //t++; } } } } if (f) { ok = m; //vcnt = t; } else { ng = m; } } //cerr << vcnt << endl; cout << ok << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #define maxn 1005 #define ll long long #define ld double #define mod 998244353 using namespace std; int n, d; int tu(int x) { int res = x % d; if(res == 0) res += d; return res; } struct th { int a[maxn][maxn]; th() { memset(a, 0, sizeof(a)); } void ins(int x, int y) { a[x][y]++; } void build() { for(int i = 1; i <= d; i++) for(int j = 1; j <= d; j++) a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } int q(int l, int r, int x, int y) { int ans = a[r][y] - a[l - 1][y] - a[r][x - 1] + a[l - 1][x - 1]; return ans; } }q1, q2; int s[maxn][maxn]; bool check(int t) { for(int i = 1; i <= d; i++) for(int j = 1; j <= d; j++) { int tp[2][3], pl[2][4]; pl[0][3] = pl[1][3] = d + 1; pl[0][0] = pl[1][0] = 1; if(i + t <= d + 1) tp[0][0] = 0, tp[0][1] = 1, tp[0][2] = 0, pl[0][1] = i, pl[0][2] = i + t; else tp[0][0] = 1, tp[0][1] = 0, tp[0][2] = 1, pl[0][1] = i + t - d, pl[0][2] = i; if(j + t <= d + 1) tp[1][0] = 0, tp[1][1] = 1, tp[1][2] = 0, pl[1][1] = j, pl[1][2] = j + t; else tp[1][0] = 1, tp[1][1] = 0, tp[1][2] = 1, pl[1][1] = j + t - d, pl[1][2] = j; bool fl = 1; for(int x = 0; x < 3; x++) for(int y = 0; y < 3; y++) if(tp[0][x] && tp[1][y]) continue; else { if(tp[0][x] + tp[1][y]) { if(q1.q(pl[0][x], pl[0][x + 1] - 1, pl[1][y], pl[1][y + 1] - 1)) fl = 0; } else if(q2.q(pl[0][x], pl[0][x + 1] - 1, pl[1][y], pl[1][y + 1] - 1)) fl = 0; } if(fl) { // cout<<"CAN"<<i<<" "<<j<<" "<<t<<endl; return 1; } } return 0; } int main() { scanf("%d%d", &n, &d); for(int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); s[tu(x)][tu(y)]++; } int mxs = 0; for(int i = 1; i <= d; i++) for(int j = 1; j <= d; j++) mxs = max(mxs, s[i][j]); int ns = 1; while(ns * ns < mxs) ns++; for(int i = 1; i <= d; i++) for(int j = 1; j <= d; j++) { if(ns * (ns - 1) < s[i][j]) q1.ins(i, j); if((ns - 1) * (ns - 1) < s[i][j]) q2.ins(i, j); } q1.build(), q2.build(); int l = 1, r = d; while(l < r) { int mid = (l + r) >> 1; if(check(mid)) r = mid; else l = mid + 1; } printf("%d\n", (ns - 1) * d + l - 1); return 0; } /* bbbbbb bababa */
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <vector> #include <cmath> #include <cstring> #include <algorithm> using namespace std; int D; int a[1010][1010]; // [x, y) に ..., i-D, i, i+D, i+2D, ... が何個あるか // [x-i, y-i) に -D, 0, D, ... が何個あるか long long num(long long x, long long y, long long i) { x -= i, y -= i; while (x <= 1) x += D, y += D; return (y-1) / D - (x-1) / D; } int dame[1010][1010]; bool judge(long long d) { //vector<vector<long long>> dame(D+1, vector<long long>(D+1, 0)); memset(dame, 0, sizeof(dame)); for (int x = 0; x < D; ++x) { for (int y = 0; y < D; ++y) { long long need = a[x][y]; long long s = (long long)((long long)(sqrt(need)) + 0.5); if (s * s < need) ++s; --s; if (d < s * D) return false; if (d >= (s + 1) * D) continue; vector<long long> ax({0, 1, D-1, D}), ay({0, 1, D-1, D}); for (int it = 0; it <= 1; ++it) { if (x + it >= 0 && x + it < D) ax.push_back(x + it); if (y + it >= 0 && y + it < D) ay.push_back(y + it); } { long long kox = x + D*s - d; for (int it = 0; it <= 1; ++it) if (kox + it >= 0 && kox + it < D) ax.push_back(kox+it); kox = x + D*(s+1) - d; for (int it = 0; it <= 1; ++it) if (kox + it >= 0 && kox + it < D) ax.push_back(kox+it); } { long long koy = y + D*s - d; for (int it = 0; it <= 1; ++it) if (koy + it >= 0 && koy + it < D) ay.push_back(koy+it); koy = y + D*(s+1) - d; for (int it = 0; it <= 1; ++it) if (koy + it >= 0 && koy + it < D) ay.push_back(koy+it); } sort(ax.begin(), ax.end()); sort(ay.begin(), ay.end()); ax.erase(unique(ax.begin(), ax.end()), ax.end()); ay.erase(unique(ay.begin(), ay.end()), ay.end()); for (int i = 0; i+1 < ax.size(); ++i) { for (int j = 0; j+1 < ay.size(); ++j) { long long can = num(ax[i], ax[i] + d+1, x) * num(ay[j], ay[j] + d+1, y); if (can < need) { dame[ax[i]][ay[j]]++; dame[ax[i+1]][ay[j]]--; dame[ax[i]][ay[j+1]]--; dame[ax[i+1]][ay[j+1]]++; } } } } } for (int i = 0; i < D; ++i) for (int j = 0; j < D; ++j) dame[i+1][j] += dame[i][j]; for (int i = 0; i < D; ++i) for (int j = 0; j < D; ++j) dame[i][j+1] += dame[i][j]; for (int i = 0; i < D; ++i) for (int j = 0; j < D; ++j) if (dame[i][j] <= 0) return true; return false; } long long solve() { long long left = 0, right = 1LL<<20; while (right - left > 1) { long long x = (left + right) / 2; if (judge(x)) right = x; else left = x; } return right; } int main() { int N; while (cin >> N >> D) { memset(a, 0, sizeof(a)); for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; a[x%D][y%D]++; } cout << solve() << endl; } }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.BufferedReader; import java.io.Reader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyInput in = new MyInput(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, MyInput in, PrintWriter out) { final int n = in.nextInt(); final int d = in.nextInt(); int[][] cnt = new int[d][d]; for (int i = 0; i < n; i++) { int x = in.nextInt() % d; int y = in.nextInt() % d; cnt[y][x]++; } int[][][] sum = new int[3][2 * d + 1][2 * d + 1]; int low = 0, high = 1_000_000; LOOP: while (high - low > 1) { int mid = (low + high) >>> 1; for (int t = 0; t < sum.length; t++) { for (int y = 0; y < sum[t].length; y++) { Arrays.fill(sum[t][y], 0); } } long a = mid / d; // final int b = (mid - 1) % d + 1; int b = mid % d; final long[] th = new long[]{(a + 1) * (a + 1), a * (a + 1), a * a,}; for (int t = 0; t < sum.length; t++) { for (int y = 0; y < d; y++) { for (int x = 0; x < d; x++) { final int v = cnt[y][x] <= th[t] ? 1 : 0; sum[t][y + 1][x + 1] += v; sum[t][y + d + 1][x + 1] += v; sum[t][y + 1][x + d + 1] += v; sum[t][y + d + 1][x + d + 1] += v; } } for (int y = 0; y < sum[t].length; y++) { for (int x = 0; x + 1 < sum[t][y].length; x++) { sum[t][y][x + 1] += sum[t][y][x]; } } for (int x = 0; x < sum[t][0].length; x++) { for (int y = 0; y + 1 < sum[t].length; y++) { sum[t][y + 1][x] += sum[t][y][x]; } } } for (int y = 0; y <= d; y++) { for (int x = 0; x <= d; x++) { boolean ok = true; final int c = d - b; ok &= sum[0][y + b][x + b] - sum[0][y + b][x] - sum[0][y][x + b] + sum[0][y][x] == b * b; ok &= sum[1][y + d][x + b] - sum[1][y + d][x] - sum[1][y + b][x + b] + sum[1][y + b][x] == c * b; ok &= sum[1][y + b][x + d] - sum[1][y][x + d] - sum[1][y + b][x + b] + sum[1][y][x + b] == c * b; ok &= sum[2][y + d][x + d] - sum[2][y + d][x + b] - sum[2][y + b][x + d] + sum[2][y + b][x + b] == c * c; if (ok) { high = mid; continue LOOP; } } } low = mid; } out.println(high - 1); } } static class MyInput { private final BufferedReader in; private static int pos; private static int readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500 * 8 * 2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for (int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public MyInput(InputStream is) { in = new BufferedReader(new InputStreamReader(is)); } public int read() { if (pos >= readLen) { pos = 0; try { readLen = in.read(buffer); } catch (IOException e) { throw new RuntimeException(); } if (readLen <= 0) { throw new MyInput.EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public char nextChar() { while (true) { final int c = read(); if (!isSpace[c]) { return (char) c; } } } int reads(int len, boolean[] accept) { try { while (true) { final int c = read(); if (accept[c]) { break; } if (str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char) c; } } catch (MyInput.EndOfFileRuntimeException e) { } return len; } static class EndOfFileRuntimeException extends RuntimeException { } } }
JAVA
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <algorithm> #include <cmath> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i,n) for(int i=0;i<(int)(n);++i) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i) #define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i) #define each(a,b) for(auto& (a): (b)) #define all(v) (v).begin(),(v).end() #define len(v) (int)(v).size() #define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end()) #define cmx(x,y) x=max(x,y) #define cmn(x,y) x=min(x,y) #define fi first #define se second #define pb push_back #define show(x) cout<<#x<<" = "<<(x)<<endl #define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl #define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl #define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl #define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl #define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl #define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl using namespace std; const int MAX_N = 1005; int cnt[MAX_N][MAX_N]; int n, D, mx; int sm[2*MAX_N][2*MAX_N][2]; #define comp(a, b, c, d, k) (sm[c+1][d+1][k] - sm[c+1][b][k] - sm[a][d+1][k] + sm[a][b][k]) bool possible(int b) { rep(i,D){ rep(j,D){ if(comp(i+b+1,j+b+1,i+D-1,j+D-1,0) == 0 && comp(i+b+1,j,i+D-1,j+b,1) == 0 && comp(i,j+b+1,i+b,j+D-1,1) == 0) return true; } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> D; rep(i,n){ int x,y; cin >> x >> y; cnt[x%D][y%D]++; } rep(i,D){ rep(j,D){ cmx(mx,(int)ceil(sqrt(cnt[i][j]))-1); } } rep(i,D){ rep(j,D){ sm[i+1][j+1][0] = sm[i+D+1][j+1][0] = sm[i+1][j+D+1][0] = sm[i+D+1][j+D+1][0] = (cnt[i][j] > mx*mx); sm[i+1][j+1][1] = sm[i+D+1][j+1][1] = sm[i+1][j+D+1][1] = sm[i+D+1][j+D+1][1] = (cnt[i][j] > mx*(mx+1)); } } rep(i,2*D+1){ rep(j,2*D){ rep(k,2){ sm[i][j+1][k] += sm[i][j][k]; } } } rep(j,2*D+1){ rep(i,2*D){ rep(k,2){ sm[i+1][j][k] += sm[i][j][k]; } } } int l = -1, r = D-1; while(r-l>1){ int mid = (l+r)/2; if(possible(mid)){ r = mid; }else{ l = mid; } } cout << (ll)mx*D+r << "\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; #define maxn 100020 #define N 2020 #define rep(i,l,r) for(register int i = l ; i <= r ; i++) #define repd(i,r,l) for(register int i = r ; i >= l ; i--) #define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++) #define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--) #define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next) #define pb push_back #define prev prev_ #define stack stack_ #define mp make_pair #define fi first #define se second #define inf 0x3f3f3f3f typedef long long ll; typedef pair<int,int> pr; struct node{ int x,y,num,k; bool operator < (node a)const{ if ( x == a.x ) return y < a.y; return x < a.x; } }dt[maxn],dt2[maxn]; int n,D,tot; int sum[2][N][N],a[N][N]; int m,b; inline int cal(int x1,int y1,int x2,int y2,int t){ if ( x1 > x2 || y1 > y2 ) return 0; int res = sum[t][x2][y2]; if ( x1 > 0 ) res -= sum[t][x1 - 1][y2]; if ( y1 > 0 ) res -= sum[t][x2][y1 - 1]; if ( x1 > 0 && y1 > 0 ) res += sum[t][x1 - 1][y1 - 1]; return res; } bool check(int x,int y,int b){ return cal(x + b + 1,y + b + 1,x + D - 1,y + D - 1,0) == 0 && cal(x,y + b + 1,x + b,y + D - 1,1) == 0 && cal(x + b + 1,y,x + D - 1,y + b,1) == 0; } int main(){ scanf("%d %d",&n,&D); rep(i,1,n) scanf("%d %d",&dt[i].x,&dt[i].y) , dt[i].x %= D , dt[i].y %= D; sort(dt + 1,dt + n + 1); dt[0] = (node){-1,-1,-1,-1}; rep(i,1,n){ if ( dt[i].x == dt[i - 1].x && dt[i].y == dt[i - 1].y ){ dt2[tot].num++; } else{ dt2[++tot] = dt[i]; dt2[tot].num = 1; } } rep(i,1,tot){ int x = dt2[i].x , y = dt2[i].y; a[x][y] = dt2[i].num; m = max(m,dt2[i].num); } m = ceil(sqrt(m) - 1) , b = D - 1; rep(i,0,2 * D - 1){ rep(j,0,2 * D - 1){ rep(k,0,1){ if ( j > 0 ) sum[k][i][j] += sum[k][i][j - 1]; if ( i > 0 ) sum[k][i][j] += sum[k][i - 1][j]; if ( i > 0 && j > 0 ) sum[k][i][j] -= sum[k][i - 1][j - 1]; } sum[0][i][j] += (a[i % D][j % D] > m * m); sum[1][i][j] += (a[i % D][j % D] > m * (m + 1)); } } rep(i,0,D - 1){ rep(j,0,D - 1){ while ( b && check(i,j,b - 1) ) b--; if ( !b ) break; } } cout<<m * D + b<<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <vector> #include <iostream> #include <utility> #include <algorithm> #include <string> #include <deque> #include <tuple> #include <queue> #include <stack> #include <functional> #include <cmath> #include <iomanip> #include <map> #include <set> #include <numeric> #include <unordered_map> #include <unordered_set> #include <complex> #include <iterator> #include <array> #include <memory> #include <random> #include <valarray> //cin.sync_with_stdio(false); //streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template<class T, int s>using va = vector<array<T, s>>; template<class T, class T2> using umap = unordered_map<T, T2>; template<class T> using uset = unordered_set<T>; template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; } template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; } #define ALL(a) a.begin(),a.end() #define rep(i,a) for(int i=0;i<a;i++) #define rep1(i,a) for(int i=1;i<=a;i++) #define rrep(i,a) for(int i=(a)-1;i>=0;i--) #define rrep1(i,a) for(int i=a;i;i--) #define repi(i,a,b) for(int i=a;i<b;i++) //const ll mod = 1000000007; template<class T>using heap = priority_queue<T, vector<T>, greater<T>>; template<class T>using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t & seed, const T & v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2); } namespace std { template<typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> & v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename ... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const& tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...> >::apply(seed, tt); return seed; } }; } template<class T>int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b)*b; } #ifdef _MSC_VER #include <intrin.h> #endif int popcnt(unsigned long long a) { #ifndef _MSC_VER return __builtin_popcountll(a); #elif defined _WIN64 return _mm_popcnt_u64(a); #else return _mm_popcnt_u32(a >> 32) + _mm_popcnt_u32(a); a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); #endif } int BitScanF(unsigned long long a) { #ifndef _MSC_VER return __builtin_ctzll(a); #elif defined _WIN64 unsigned long ret; _BitScanForward64(&ret, a); return a; #else unsigned long ret; if (!(unsigned long)a) { _BitScanForward(&ret, a); ret += 32; } else _BitScanForward(&ret, (unsigned long)a); return ret; #endif } int BitScanR(unsigned long long a) { #ifndef _MSC_VER return 63 - __builtin_clzll(a); #elif defined _WIN64 unsigned long ret; _BitScanReverse64(&ret, a); return a; #else unsigned long ret; if (a >> 32) { _BitScanReverse(&ret, a); ret += 32; } else _BitScanReverse(&ret, (unsigned long)a); return ret; #endif } template<class T> class matrix { public: vector<valarray<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) :matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) :obj(n, valarray<T>(e, m == -1 ? n : m)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++)a[i][i] = 1; return a; } matrix& operator+=(const matrix &p) { if (s != p.s)throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++)obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix& operator-=(const matrix &p) { if (s != p.s)throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++)obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix& operator*=(T p) { for (auto &a : obj) for (auto &b : a)b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first)throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++)ret[i] += obj[i][j] * p.obj[j]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this*p; } bool operator==(const matrix&p) { if (s != p.s)return 0; for (int i = 0; i<s.first; i++)for (int j; j< s.second; j++)if (obj[i][j] != p.obj[i][j])return 0; return 1; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a)b %= m; return *this; } valarray<T>& operator[](int t) { return obj[t]; } void gauss() { if (size().first + 1 != size().second)return; rep(i, size().first) { int p = i; repi(j, i, size().first)if (abs(obj[j][i]) > abs(obj[p][i]))p = j; swap(obj[i], obj[p]); if (abs(obj[i][i]) < 1e-8)return;//contniue; repi(j, i + 1, size().second)obj[i][j] /= obj[i][i]; rep(j, size().first) { if (i != j) { repi(k, i + 1, size().second)obj[j][k] -= obj[j][i] * obj[i][k]; } } } } }; template<class T> std::pair<matrix<T>, vector<int>> LU_decomposition(matrix<T> a) { if (a.size().first != a.size().second)throw runtime_error("matrix error"); std::vector<int> pi(a.size().first); std::iota(ALL(pi), 0); valarray<T> tmp(a.size().first); for (int i = 0; i < a.size().first; i++) { //int pivot = i; //T max = abs(a[i][i]); //for (int j = i + 1; j < a.size().first; j++) { // if (max < abs(a[j][i])) { // max = abs(a[j][i]); // pivot = j; // } //} //std::swap(i, pivot); //pi.push_back(pivot); std::slice slice(i + 1, a.size().first - i - 1, 1); for (int j = i + 1; j < a.size().first; j++) { tmp[slice] = a[i][slice]; tmp *= a[j][i] / a[i][i]; a[j][slice] -= tmp[slice]; a[j][i] = a[j][i] / a[i][i]; } } return std::make_pair(std::move(a), std::move(pi)); } template<class T> matrix<T>LU_solve(pair<matrix<T>, std::vector<int>> a, matrix<T> b) { auto pi = std::move(a.second); auto A = std::move(a.first); if (A.size().first != A.size().second || A.size().first != b.size().first)throw runtime_error("matrix error"); for (int i = 0; i < A.size().first; i++) { std::swap(b[i], b[pi[i]]); } for (int i = 0; i < A.size().first; i++) { for (int j = 0; j < i; j++)b[i] -= A[i][j] * b[j]; } for (int i = A.size().first - 1; i >= 0; i--) { for (int j = i + 1; j < A.size().first; j++)b[i] -= A[i][j] * b[j]; b[i] /= A[i][i]; } return b; } template<class T> inline matrix<T> pow(matrix<T> &base, unsigned long long exp) { auto base_(base); if (base_.size().first != base_.size().second)throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1)res *= base_; if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2))break; base_ *= base_; } return res; } template<class T> inline matrix<T> modpow(matrix<T> &base, unsigned long long exp, ll m) { auto base_(base); if (base.size().first != base_.size().second)throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1)(res *= base_).mod(m); if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2))break; (base_ *= base_).mod(m); } return res; } class unionfind { vector<int> par, rank, size_;//速度ではなくメモリ効率を考えるならrankのかわりにsizeを使う public: unionfind(int n) :par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x)return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y)return; if (rank[x] < rank[y])swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y])rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; //end of lib //template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; //public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } //}; //template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // } //}; template<class T, class Func = function<T(T, T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num >i; i <<= 1); offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l)return e; if (a <= l && r <= b)return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: void propagate(int a, int b, int k, int l, int r, int p) { if (r <= a || b <= l)return; if (a <= l && r <= b) { obj[k].second += p; (obj[k].first *= pow(2, p, (ll)1e9 + 7)) %= (ll)(1e9 + 7); return; } else { if (obj[k].second) { (obj[k * 2 + 1].first *= pow(2, obj[k].second, (ll)1e9 + 7)) %= (ll)(1e9 + 7); (obj[k * 2 + 2].first *= pow(2, obj[k].second, (ll)1e9 + 7)) %= (ll)(1e9 + 7); obj[k * 2 + 1].second += obj[k].second; obj[k * 2 + 2].second += obj[k].second; } propagate(a, b, k * 2 + 1, l, (l + r) / 2, p), propagate(a, b, k * 2 + 2, (l + r) / 2, r, p); obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } T query(int a, int b) {//[a,b) propagate(a, b, 0); return query(a, b, 0, 0, offset + 1); } void propagate(int a, int b, int p = 1) {//[a,b) return propagate(a, b, 0, 0, offset + 1, p); } void updateall(int l = 0, int r = -1) { if (r < 0)r = offset + 1; l += offset, r += offset; if (l == 0)return; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++)obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, const T &a) { propagate(k, k + 1, 0); k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater = Func()) :obj(bufsize(n), e), e(e), updater(updater) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) :obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } T& operator[](int n) { return obj[n + offset]; } }; template<class T = int> class BIT {//多次元BITはループをネストすればいいらしい。 vector<T> bit; public: BIT(int n) :bit(n + 1, 0) {} void add(int i, T x) { i++; while (i <= bit.size()) { bit[i - 1] += x; i += i & -i; } } T sum(int i) { T s = 0; i++; while (i) { s += bit[i - 1]; i &= i - 1; } return s; } }; template<class T> class rangeadd { BIT<T> b0, b1; int n; rangeadd(int n) :b0(n), b1(n), n(n) {} void add(int l, int r, T x) {//[l,r) b0.add(l, -x(l - 1)); b1.add(l, x); b0.add(r, x*r); b1.add(r, -x); } T sum(int i) { return b0.sum(i) + b1.sum(i)*i; } }; class Flow { int V; struct edge { int to, cap, rev, cost, add; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t)return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int res = 0; int flow = 0; int s, t; int max_flow(int s, int t, int fmax = numeric_limits<int>::max()) { level.resize(V); iter.resize(V); for (;;) { bfs(s); if (level[t] < 0)return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, fmax))>0) { flow += f; fmax -= f; } if (fmax == 0)return flow; } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({ 0,s }); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first)continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to]>dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({ dist[e.to],e.to }); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++)h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } public: Flow(int size, int s, int t) :G(size + 1), V(size + 1), s(s), t(t) { } void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{ to, cap, (int)G[to].size(),cost,1 }); G[to].push_back(edge{ from,0,(int)G[from].size() - 1,-cost,0 }); } void remove_edge_max(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost)continue; int prev = flow; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; cap -= max_flow(from, to, cap) - prev; max_flow(t, to, cap); max_flow(from, s, cap); flow = prev - cap; break; } } void remove_edge_cost(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost)continue; cost += G[to][x.rev].cap*G[to][x.rev].cost; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; min_cost_flow(from, to, cap); break; } } int max_flow() { return max_flow(s, t); } int min_cost_flow(int f) { return min_cost_flow(s, t, f); } }; ll extgcd(ll a, ll b, ll&x, ll&y) { int d = a; if (b != 0) { d = extgcd(b, a%b, y, x); y -= (a / b)*x; } else { x = 1; y = 0; } return d; } ll mod_inv(ll a, ll m) { ll x, y; extgcd(a, m, x, y); return(m + x % m) % m; } pll linear_congruence(const vector<ll>& A, const vll& B, const vll&M) { ll x = 0, m = 1; for (int i = 0; i < A.size(); i++) { ll a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a); if (b%d != 0)return make_pair(0, -1); ll t = b / d * mod_inv(a / d, M[i] / d) % (M[i] / d); //if (x + m * t < 0)return pll(x%m, m); x = x + m * t; m *= M[i] / d; } return make_pair(x%m, m); } bool check(ll x, vi &a) { auto x2 = x; repi(i, 2, 31) { ll sum = 0; x = x2; while (x) { sum += x % i; x /= i; } if (sum != a[i])return false; } return 1; } int main() { int n, d; cin >> n >> d; vpii p(n); rep(i, n)cin >> p[i].first >> p[i].second; umap<pii, int> m; rep(i, n)m[pii(p[i].first%d, p[i].second%d)]++; int M = 0; for (auto &x : m)cmax(M, x.second); int off = ll(ceil(sqrt(M)) - 1); if (M > 1) { vector<pair<pii, int>> m2; for (auto &x : m) { if (x.second > off*off)m2.push_back(x); } ll min_ = 1e9; rep(i,d)rep(j,d){ auto x = make_pair(pii(i, j), 0); ll max_ = 0; for (auto &y : m2) { if (y.second > off * (off + 1)) { cmax(max_, max((y.first.first - x.first.first + d) % d, (y.first.second - x.first.second + d) % d)); } else { cmax(max_, min((y.first.first - x.first.first + d) % d, (y.first.second - x.first.second + d) % d)); } if (min_ < max_)break; } cmin(min_, max_); } cout << min_ + off * d << endl; return 0; } vi x(2*d), y(2*d); rep(i, n)x[p[i].first%d]++, y[p[i].second%d]++, x[p[i].first%d+d]++, y[p[i].second%d+d]++; int xm = 0, ym = 0; int xn = 0, yn = 0; rep(i, 2*d) { if (x[i] == 0) { xn++; cmax(xm, xn); } else xn = 0; if (y[i] == 0) { yn++; cmax(ym, yn); } else yn = 0; } cout << max(d - xm - 1, d - ym - 1) << endl; } //template<class T, class map = std::unordered_map<T,unique_ptr<node>>> /*class AhoCorasick { struct node { map<char,unique_ptr<node>> next; node* fail = nullptr, *match_list = nullptr; std::vector<int> match; }root; int pattern; //template<class string> AhoCorasick(std::vector<string> &vs) :pattern(vs.size()) { root.fail = &root; for (int i = 0; i < vs.size(); i++) { node* now = &root; for (auto c : vs[i]) { if (!now->next[c])now->next[c]=make_unique<node>(); now = now->next[c].get(); } now->match.push_back(i); } std::queue<node*> que; que.push(&root); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto &next : now->next) { if (!next.second)continue; if (now->fail->next.count(next.first))next.second->fail = now->fail->next[next.first].get(); else next.second->fail = now->fail->fail; //next.second->match.insert(next.second->match.end(), next.second->fail->match.begin(), next.second->fail->match.end()); if (next.second->fail->match.empty())next.second->match_list = next.second->fail->match_list; else next.second->match_list = next.second->fail; que.push(next.second.get()); } } } auto match_n(string str) { vector<int> num(pattern); } auto match_list(string str) { vector<pair<int, int>> list; auto now = &root; for (int i = 0; i < str.size(); i++) { if (now->next.count(str[i]))now = now->next[str[i]].get(); else now = now->fail; auto match = now->match_list; do { match } } } };*/
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
// warm heart, wagging tail,and a smile just for you! // ▓▓▓▓▓▓▓▓▓▓▓▓ // ▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓▓▓▓▓▓ // ▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓ // ▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ //▓▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ //╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ //╬╬╬╬╬╬╬▓▓╬╬╬╬╬▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ //╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▒▒▒▒▒▒╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓ //╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▒▒▒▒▒▒╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓ //╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓ //╬╬╬╬╬╬▓▓╬▒▒▒▒▒▒╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓ //╬╬╬╬╬╬▓▓╬▒▒▒▒▒▒╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ ▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓ //╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓ //╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ ▓▓▓╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓ //╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ ▓▓▓▓╬╬╬╬╬╬╬╬▓▓▓ //╬╬╬╬╬╬╬╬╬▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ ▓▓▓▓╬╬╬╬▓▓▓ //╬╬╬╬╬╬▓▓ ▓▓▓▓╬╬╬╬▓▓╬╬╬╬▓▓╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓╬╬▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓ //╬╬╬╬▓▓ ▓▓▓▓╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ //▓▓▓▓ ▓▓╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬▓▓╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬▓▓╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬▓▓▓▓ // ▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓▓▓▓▓▓▓ // ▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓ #include "bits/stdc++.h" using namespace std; #define MOD 1000000007 #define INF 1LL<<60 #define fs first #define sc second #define pb push_back #define int long long #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define RFOR(i,a,b) for(int i = (b-1);i>=a;i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define debug(x) cout << #x << " = " << (x) << endl; typedef pair<int,int> P; typedef vector<vector<P>> Graph; int n,d,sum1 = 0; const int DMAX = 1000; vector<int> mod[DMAX],tmp[2][2*DMAX+1]; int sum(int t, int x, int y, int ds){ return tmp[t][x+ds][y+ds]-tmp[t][x+ds][y]-tmp[t][x][y+ds]+tmp[t][x][y]; } bool check(int mid, int x, int y){ bool f = true; if(sum(0,x+mid,y+mid,d-mid) != 0) f = false; if(sum(1,x,y,mid) != sum1) f = false; return f; } signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> d; vector<int> x(n),y(n); REP(i,n) cin >> x[i] >> y[i]; REP(i,d) mod[i].assign(d,0); REP(i,2*d+1){ tmp[0][i].assign(2*d+1,0); tmp[1][i].assign(2*d+1,0); } REP(i,n) mod[x[i]%d][y[i]%d]++; int mx = 0; REP(i,d) REP(j,d) mx = max(mx,mod[i][j]); int r = ceil(sqrt(mx)); //debug(r); REP(i,d){ REP(j,d){ if(mod[i][j] <= (r-1)*(r-1)) continue; if(mod[i][j] <= (r-1)*r){ i++;j++; tmp[0][i][j]++; tmp[0][i+d][j]++; tmp[0][i][j+d]++; tmp[0][i+d][j+d]++; } else{ i++;j++; tmp[1][i][j]++; tmp[1][i+d][j]++; tmp[1][i][j+d]++; tmp[1][i+d][j+d]++; sum1++; } i--;j--; } } //debug(sum1); REP(i,2*d+1){ REP(j,2*d){ tmp[0][i][j+1] += tmp[0][i][j]; tmp[1][i][j+1] += tmp[1][i][j]; } } REP(i,2*d+1){ REP(j,2*d){ tmp[0][j+1][i] += tmp[0][j][i]; tmp[1][j+1][i] += tmp[1][j][i]; } } // REP(i,2*d+1){ // REP(j,2*d+1){ // cout << tmp[0][j][i] << " "; // } // cout << endl; // } // cout << endl; // REP(i,2*d+1){ // REP(j,2*d+1){ // cout << tmp[1][j][i] << " "; // } // cout << endl; // } int ans = INF; REP(i,d){ REP(j,d){ int ng = 0, ok = d; while (abs(ng-ok)>1) { int mid = (ng+ok)/2; (check(mid,i,j)?ok:ng) = mid; //debug(mid); } //debug(ok); ans = min(ans,ok); } } cout << ans-1 +(r-1)*d << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define N 1010 using namespace std; template <typename T> void read(T &x){ x=0;char ch=getchar();int fh=1; while (!isdigit(ch)){if (ch=='-')fh=-1;ch=getchar();} while (isdigit(ch)){x=x*10+ch-'0';ch=getchar();} x=x*fh; } int n,d,f[N][N],x,y,g[N][N][3],sum,ansn; int que(int nu,int l,int r,int p,int q){ if (p>=d)return que(nu,l,r,d-1,q)+que(nu,0,r,p-d,q); if (q>=d)return que(nu,l,r,p,d-1)+que(nu,l,0,p,q-d); int sum=g[p][q][nu]; if (l!=0)sum-=g[l-1][q][nu]; if (r!=0)sum-=g[p][r-1][nu]; if (l!=0&&r!=0)sum+=g[l-1][r-1][nu]; return sum; } bool pa(int x,int y,int le){ int x1=que(2,x,y,x+le,y+le); int x2=que(1,0,y,d-1,y+le)+que(1,x,0,x+le,d-1)-que(1,x,y,x+le,y+le); if (x1==g[d-1][d-1][2]&&x2==g[d-1][d-1][1])return 1; return 0; } int main(){ read(n);read(d); for (int i=1;i<=n;i++){read(x);read(y);f[x%d][y%d]++;} for (int i=0;i<d;i++) for (int j=0;j<d;j++)sum=max(sum,f[i][j]); int y=sqrt(sum);while (y*y<sum) y++; for (int i=0;i<d;i++) for (int j=0;j<d;j++){ if (f[i][j]<=(y-1)*(y-1))g[i][j][0]=1; if ((y-1)*(y-1)<f[i][j]&&f[i][j]<=(y-1)*y)g[i][j][1]=1; if (f[i][j]>(y-1)*y)g[i][j][2]=1; } for (int k=0;k<=2;k++){ for (int i=0;i<d;i++) for (int j=1;j<d;j++)g[i][j][k]+=g[i][j-1][k]; for (int i=1;i<d;i++) for (int j=0;j<d;j++)g[i][j][k]+=g[i-1][j][k]; } ansn=d; for (int i=0;i<d;i++){ for (int j=0;j<d;j++){ int l=0,r=d-1,mid; while (l<r){ mid=(l+r)/2; if (pa(i,j,mid))r=mid;else l=mid+1; } ansn=min(ansn,l); } } cout<<ansn+d*(y-1)<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; void rd(int &x){ int k, m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } void wt_L(int x){ char f[10]; int m=0, s=0; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } template<class S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } int D, N, mn[1000][1000], mx[1000][1000], p[1000][1000], t[1001][1001]; inline void ad(int x1, int x2, int y1, int y2, int v){ if(x1 < 0){ ad(x1+D, D-1, y1, y2, v); ad(0, x2, y1, y2, v); return; } if(y1 < 0){ ad(x1, x2, y1+D, D-1, v); ad(x1, x2, 0, y2, v); return; } t[x2+1][y2+1]+=v; t[x1][y2+1]-=v; t[x2+1][y1]-=v; t[x1][y1]+=v; } int main(){ int Lj4PdHRW, i, j, k, m, res, tot, x, y, z; rd(N); rd(D); for(Lj4PdHRW=0;Lj4PdHRW<N;Lj4PdHRW++){ rd(i); rd(j); p[i%D][j%D]++; } for(i=0;i<D;i++){ for(j=0;j<D;j++){ k = p[i][j]; if(k==0){ mn[i][j] = mx[i][j] = 0; continue; } for(x=0;;x++){ if(x*x >= k){ break; } } if(x * (x-1) >= k){ mn[i][j] = x-1; mx[i][j] = x; } else{ mn[i][j] = mx[i][j] = x; } } } m = 0; for(i=0;i<D;i++){ for(j=0;j<D;j++){ chmax(m, mx[i][j]); } } for(i=0;i<D;i++){ for(j=0;j<D;j++){ p[i][j] = 0; if(mx[i][j] != m){ continue; } if(mx[i][j] == mn[i][j]){ p[i][j] = 2; } else{ p[i][j] = 1; } } } x = 0; y = D-1; while(x < y){ z = (x+y)/2; tot = 0; for(i=0;i<D+1;i++){ for(j=0;j<D+1;j++){ t[i][j] = 0; } } for(i=0;i<D;i++){ for(j=0;j<D;j++){ if(p[i][j] == 2){ ad(i-z,i,j-z,j,1); tot++; } if(p[i][j] == 1){ ad(0,D-1,j-z,j,1); ad(i-z,i,0,D-1,1); ad(i-z,i,j-z,j,-1); tot++; } } } k = 0; for(i=1;i<D+1;i++){ t[i][0] += t[i-1][0]; } for(i=1;i<D+1;i++){ t[0][i] += t[0][i-1]; } for(i=1;i<D+1;i++){ for(j=1;j<D+1;j++){ t[i][j] += t[i-1][j] + t[i][j-1] - t[i-1][j-1]; } } for(i=0;i<D;i++){ for(j=0;j<D;j++){ if(t[i][j]==tot){ k = 1; } } } if(k){ y = z; } else{ x = z+1; } } res = (m-1) * D + x; wt_L(res); putchar_unlocked('\n'); return 0; } // cLay varsion 20180730-1 // --- original code --- // int N, D; // int p[1000][1000]; // int mn[1000][1000], mx[1000][1000]; // // int t[1001][1001]; // // inline void ad(int x1, int x2, int y1, int y2, int v){ // if(x1 < 0){ // ad(x1+D, D-1, y1, y2, v); // ad(0, x2, y1, y2, v); // return; // } // if(y1 < 0){ // ad(x1, x2, y1+D, D-1, v); // ad(x1, x2, 0, y2, v); // return; // } // t[x2+1][y2+1]+=v; // t[x1][y2+1]-=v; // t[x2+1][y1]-=v; // t[x1][y1]+=v; // } // // { // int i, j, k, x, y, z, m, tot; // int res; // // rd(N,D); // rep(N){ // rd(i,j); // p[i%D][j%D]++; // } // // rep(i,D) rep(j,D){ // k = p[i][j]; // if(k==0){ // mn[i][j] = mx[i][j] = 0; // continue; // } // for(x=0;;x++) if(x*x >= k) break; // if(x * (x-1) >= k){ // mn[i][j] = x-1; // mx[i][j] = x; // } else { // mn[i][j] = mx[i][j] = x; // } // } // // m = 0; // rep(i,D) rep(j,D) m >?= mx[i][j]; // // rep(i,D) rep(j,D){ // p[i][j] = 0; // if(mx[i][j] != m) continue; // if(mx[i][j] == mn[i][j]) p[i][j] = 2; // else p[i][j] = 1; // } // // // rep(i,D) wt(p[i](D)); // // x = 0; y = D-1; // while(x < y){ // z = (x+y)/2; // tot = 0; // // rep(i,D+1) rep(j,D+1) t[i][j] = 0; // // rep(i,D) rep(j,D){ // if(p[i][j] == 2){ // ad(i-z,i,j-z,j,1); // tot++; // } // if(p[i][j] == 1){ // ad(0,D-1,j-z,j,1); // ad(i-z,i,0,D-1,1); // ad(i-z,i,j-z,j,-1); // tot++; // } // } // // k = 0; // rep(i,1,D+1) t[i][0] += t[i-1][0]; // rep(i,1,D+1) t[0][i] += t[0][i-1]; // rep(i,1,D+1) rep(j,1,D+1) t[i][j] += t[i-1][j] + t[i][j-1] - t[i-1][j-1]; // // // wt(z); // // rep(i,D+1) wt(t[i](D+1)); // // rep(i,D) rep(j,D) if(t[i][j]==tot) k = 1; // if(k) y = z; else x = z+1; // } // // res = (m-1) * D + x; // wt(res); // }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
//#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #define owo(i,a, b) for(int i=(a);i<(b); ++i) #define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i) #define senpai push_back #define ttgl pair<int, int> #define ayaya cout<<"ayaya~"<<endl using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; gpu_hash_table<int, int> mp; #define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update> */ using ll = long long; using ld = long double; const ll MOD = 924844033; const ll root = 62; int gcd(int a,int b){return b?gcd(b,a%b):a;} ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;} ll modInv(ll a){return binpow(a, MOD-2);} const double PI = acos(-1); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const ll NINFLL = 0xc0c0c0c0c0c0c0c0; const int mxN = 100001; const int mxD = 1001; int cnt[mxD][mxD]; int psum1[2*mxD][2*mxD]; int psum2[2*mxD][2*mxD]; //bigger to deal with negative shifts int n, d; int mxk = 0; int get1(int x1, int y1, int x2, int y2) { return psum1[x2][y2] + psum1[x1][y1] - psum1[x1][y2] - psum1[x2][y1]; } int get2(int x1, int y1, int x2, int y2) { return psum2[x2][y2] + psum2[x1][y1] - psum2[x1][y2] - psum2[x2][y1]; } bool check(int over) { int k = psum1[d][d]; owo(i, 0, d) { owo(j, 0, d) { //cout<<over<<" "<<i<<" "<<j<<" "<<get1(i, j, i+over, j+over)<<" "<<get2(i+over, j+over, i+d, j+d)<<"\n"; if(get1(i, j, i+over, j+over)==k&&get2(i+over, j+over, i+d, j+d)==0)return true; } } //cout<<over<<"\n"; return false; } int main() { //freopen("file.in", "r", stdin); //freopen("file.out", "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin.tie(0)->sync_with_stdio(0); cin>>n>>d; int a, b; owo(i, 0, n) { cin>>a>>b; cnt[a%d][b%d]++; } owo(i, 0, d) { owo(j, 0, d) { mxk = max(mxk, cnt[i][j]); } } mxk = (int)(sqrt(mxk)-eps); owo(i, 0, d) { owo(j, 0, d) { //cout<<cnt[i][j]<<"\n"; if(cnt[i][j]>(mxk+1)*(mxk))cnt[i][j] = 0; else if(cnt[i][j]>mxk*mxk)cnt[i][j] = 1; else cnt[i][j] = 2; } } owo(i, 0, 2*d) { owo(j, 0, 2*d) { psum1[i+1][j+1] = psum1[i+1][j]; psum2[i+1][j+1] = psum2[i+1][j]; if(cnt[i%d][j%d]==0) { psum1[i+1][j+1]++; } if(cnt[i%d][j%d]==1||cnt[i%d][j%d]==0) { psum2[i+1][j+1]++; //cout<<i<<" "<<j<<"\n"; } } } owo(i, 0, 2*d) { owo(j, 0, 2*d) { psum1[i+1][j+1] = psum1[i+1][j+1] + psum1[i][j+1]; psum2[i+1][j+1] = psum2[i+1][j+1] + psum2[i][j+1]; } } owo(i, 0, 2*d) owo(j, 0, 2*d) { //cout<<i<<" "<<j<<" "<<psum1[i][j]<<" "<<psum2[i][j]<<"\n"; } int l = 0; int r = d; while(l<r) { int mid = l+(r-l)/2; if(check(mid))r = mid; else l = mid+1; } cout<<(mxk*d+l-1)<<"\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; template<typename T> vector<vector<T>> MakeVector2D(int d1, int d2, T default_value) { return vector<vector<T>>(d1, vector<T>(d2, default_value)); } template<class T> inline bool UpdateMin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline bool UpdateMax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) { int len = v.size(); s << "["; for (int i = 0; i < len; i++) { if (i > 0) s << ", "; s << v[i]; } s << "]"; return s; } vector<vector<int>> CumulativeSum2D(vector<vector<int>>& input) { int d1 = input.size(); int d2 = input[0].size(); auto c_sum = MakeVector2D(d1 + 1, d2 + 1, 0); for (int i = 1; i <= d1; i++) { for (int j = 1; j <= d2; j++) { c_sum[i][j] = c_sum[i - 1][j] + c_sum[i][j - 1] - c_sum[i - 1][j - 1] + input[i - 1][j - 1]; } } return c_sum; } // Returns a tuple of (l, u) s.t. // - l is the minimum value s.t. is_upper_fn(l) is false. // - r is the minimum value s.t. is_upper_fn(r) is true. // // Required: // - is_upper_fn is a function s.t. // - There exists a constant c s.t. is_upper_fn(x) is true for x >= c and // is_upper_fn(x) is false for x < c. // - is_upper_fn(min_value) is false. // - is_upper_fn(max_value) is true. // // Verified: ABC144E tuple<int64, int64> BinarySearch(function<bool(int64)> is_upper_fn, int64 l, int64 u) { while (u - l >= 2) { int64 m = (l + u) / 2; if (is_upper_fn(m)) u = m; else l = m; } return make_tuple(l, u); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, d; cin >> n >> d; vector<int64> xs(n), ys(n); for (int i = 0; i < n; i++) { cin >> xs[i] >> ys[i]; } vector<int> sqrt_ceils(1000001, 0); for (int k = 1; k <= 1000; k++) { for (int x = (k - 1) * (k - 1) + 1; x <= k * k; x++) { sqrt_ceils[x] = k; } } auto count_table = MakeVector2D(d, d, 0); for (int i = 0; i < n; i++) { count_table[ys[i] % d][xs[i] % d]++; } int max_k = 0; for (int y = 0; y < d; y++) { for (int x = 0; x < d; x++) { UpdateMax(max_k, sqrt_ceils[count_table[y][x]]); } } // flag_matrix_1[y][x]: Whether count[y][x] > max_k * (max_k - 1) // flag_matrix_2[y][x]: Whether count[y][x] > (max_k - 1) * (max_k - 1) auto flag_matrix_1 = MakeVector2D(2 * d, 2 * d, 0); auto flag_matrix_2 = MakeVector2D(2 * d, 2 * d, 0); for (int y = 0; y < 2 * d; y++) { for (int x = 0; x < 2 * d; x++) { if (count_table[y % d][x % d] > max_k * (max_k - 1)) { flag_matrix_1[y][x] = 1; } if (count_table[y % d][x % d] > (max_k - 1) * (max_k - 1)) { flag_matrix_2[y][x] = 1; } } } auto c_sum_1 = CumulativeSum2D(flag_matrix_1); auto c_sum_2 = CumulativeSum2D(flag_matrix_2); auto count_fn = [](const vector<vector<int>>& c_sum, int l1, int u1, int l2, int u2) { return c_sum[u1][u2] - c_sum[l1][u2] - c_sum[u1][l2] + c_sum[l1][l2]; }; int min_r = INT32_MAX; for (int base_y = 0; base_y < d; base_y++) { for (int base_x = 0; base_x < d; base_x++) { auto check_fn = [&](int a) -> bool { int f1_count = count_fn(c_sum_1, base_y, base_y + d, base_x, base_x + d) - count_fn(c_sum_1, base_y, base_y + a + 1, base_x, base_x + a + 1); int f2_count = count_fn(c_sum_2, base_y + a + 1, base_y + d, base_x + a + 1, base_x + d); return f1_count == 0 && f2_count == 0; }; if (check_fn(0)) { min_r = 0; } else { int l = 0; int u = d - 1; tie(l, u) = BinarySearch(check_fn, l, u); UpdateMin(min_r, u); } } } cout << d * (max_k - 1) + min_r << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; const long long d2=(mod+1)/2; const long double EPS=1e-9; const long double PI=acos(-1.0); int ABS(int a){return max(a,-a);} long long ABS(long long a){return max(a,-a);} long double ABS(long double a){return max(a,-a);} int cnt[1100][1100]; int las[1100][1100]; int las2[1100][1100]; int Las[1100][1100]; int Las2[1100][1100]; int req[1100]; int main(){ int a,b;scanf("%d%d",&a,&b); int M=0; for(int i=0;i<a;i++){ int x,y;scanf("%d%d",&x,&y); cnt[x%b][y%b]++; M=max(M,cnt[x%b][y%b]); } int ret=mod; for(int i=0;i<b;i++){ for(int j=0;j<b;j++){ las[i][j]=las2[i][j]=Las[i][j]=Las2[i][j]=-1; } } for(int i=0;i<b;i++)for(int j=0;j<b;j++){ if(cnt[i][j]==0)continue; int tmp=sqrt(cnt[i][j]-0.5)+1; if((int)(sqrt(M-0.5)+1)!=tmp)continue; if(cnt[i][j]>tmp*(tmp-1)){ Las[i][j]=j; }else{ Las2[i][j]=j; } } for(int i=0;i<b;i++){ int cur=-1; int pr=-1; for(int k=0;k<2;k++){ for(int j=0;j<b;j++){ if(Las[i][j]!=-1)cur=Las[i][j]; if(pr!=-1){ las[i][j]=pr; } pr=cur; } } } for(int i=0;i<b;i++){ int cur=-1; int pr=-1; for(int k=0;k<2;k++){ for(int j=0;j<b;j++){ if(Las2[i][j]!=-1)cur=Las2[i][j]; if(pr!=-1){ las2[i][j]=pr; } pr=cur; } } } /* for(int i=0;i<b;i++){ for(int j=0;j<b;j++)printf("%d ",cnt[i][j]);printf("\n"); } for(int i=0;i<b;i++){ for(int j=0;j<b;j++)printf("%d ",las[i][j]);printf("\n"); } for(int i=0;i<b;i++){ for(int j=0;j<b;j++)printf("%d ",las2[i][j]);printf("\n"); } */ int bl=sqrt(M-0.5)+1; for(int i=0;i<b;i++){ int left=-1; int right=b; while(left+1<right){ int m=(left+right)/2; bool ok=true; for(int j=0;j<b;j++)req[j]=0; for(int j=0;j<b;j++){ if(las[j][i]!=-1&&(las[j][i]-i+b)%b>m)ok=false; if(las[j][i]!=-1)req[j]=1; if(las2[j][i]!=-1&&(las2[j][i]-i+b)%b>m)req[j]=1; } int mm=0; int cur=0; for(int k=0;k<2;k++){ for(int j=0;j<b;j++){ if(req[j]){ cur=0; }else cur++; mm=max(cur,mm); } } if(b-mm-1>m)ok=false; if(ok){ right=m; }else left=m; } ret=min(ret,b*(bl-1)+right); } printf("%d\n",ret); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; int N, D; int num[1000][1000]; int rad1[1000][1000], rad2[1000][1000]; int imos[2001][2001]; bool check(int X){ int d = X/D, r = X%D; memset(imos, 0, sizeof(imos)); for(int i=0; i<D; i++){ for(int j=0; j<D; j++){ if(rad2[i][j] > d+1) return false; if(rad2[i][j] <= d){ imos[i][j]++; imos[i][j+D]--; imos[i+D][j]--; imos[i+D][j+D]++; continue; } if(rad1[i][j] == d){ imos[i][j]++; imos[i][j+D]--; imos[i+D][j]--; imos[i+r+1][j+r+1]--; imos[i+D][j+r+1]++; imos[i+r+1][j+D]++; }else{ imos[i][j]++; imos[i][j+r+1]--; imos[i+r+1][j]--; imos[i+r+1][j+r+1]++; } } } for(int i=0; i<=2*D; i++){ for(int j=1; j<=2*D; j++) imos[i][j] += imos[i][j-1]; } for(int i=1; i<=2*D; i++){ for(int j=0; j<=2*D; j++) imos[i][j] += imos[i-1][j]; } for(int i=0; i<D; i++){ for(int j=0; j<D; j++){ if(imos[i][j] + imos[i][j+D] + imos[i+D][j] + imos[i+D][j+D] == D*D) return true; } } return false; } int main(){ cin >> N >> D; for(int i=0; i<N; i++){ int x, y; cin >> x >> y; num[x%D][y%D]++; } for(int i=0; i<D; i++){ for(int j=0; j<D; j++){ while(rad1[i][j] * (rad1[i][j]+1) < num[i][j]) rad1[i][j]++; while(rad2[i][j] * rad2[i][j] < num[i][j]) rad2[i][j]++; } } int ng = -1, ok = 1e9; while(ok-ng>1){ int mid = (ok+ng)/2; (check(mid) ? ok : ng) = mid; } cout << ok << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> using namespace std; typedef int ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-4; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; typedef pair<ld, ld> LDP; int n;ll d; ll x[1 << 17], y[1 << 17]; ll c[1000][1000]; ll ly[1000] = {}; vector<P> vv; ll calc(ll i,ll mid) { ll co = 1 + (mid + i) / d; rep(aa, (int)vv.size()) { int j = vv[aa].first; int k = vv[aa].second; ll num = co; if (j < i)num--; if ((mid + i) % d < j)num--; if (num<=0) { return mod; } else { ll ad = (c[j][k] + num - 1) / num-1; ly[k] = max(ly[k], ad); } } ll ma = 0; rep(j, d) { ma = max(ma, ly[j]); } vector<ll> v; rep(j, d) { if (ma == ly[j]) { v.push_back(j); } } ll rest = 0; int llen = v.size(); if (llen >= 2) { rest = v[llen - 1] - v[0]; Rep(j, 1, llen) { rest = min(rest, v[j - 1] + d - v[j]); } } return rest + ma * d; } int main() { cin >> n >> d; rep(i, n) { cin >> x[i] >> y[i]; c[x[i] % d][y[i] % d]++; } ll rang = 0; rep(i, d) { rep(j, d) { if (c[i][j]) { vv.push_back({ i,j }); ll x = sqrt(c[i][j]); if (x*x != c[i][j])x++; rang=max(rang, x); } } } rang *= d; ll out = mod; rep(i, d) { ll le = rang-d-1; ll ri = rang; while (ri - le > 1) { fill(ly, ly + d, -1); ll mid = (ri + le) >> 1; ll y = calc(i, mid); if (y > mid) { le = mid; out = min(out, y); } else { ri = mid; out = min(out, mid); } } //out = min(out, max(le,calc(i, le))); //out = min(out, max(calc(i, ri),ri)); //cout << le << endl; } cout << out << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define y1 __zzd001 using namespace std; const int N=2005; int n,d,v[N][N],s[3][N][N]; int S(int x1,int y1,int x2,int y2,int t){ return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x){ int r=x%d; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++){ int x1=i,y1=j,x2=x1+r,y2=y1+r,x3=x1+d-1,y3=y1+d-1; if (!S(x1,y2+1,x3,y3,1)&&!S(x2+1,y1,x3,y3,1)&&!S(x2+1,y2+1,x3,y3,0)) return 1; } return 0; } int main(){ scanf("%d%d",&n,&d); memset(v,0,sizeof v); while (n--){ int x,y; scanf("%d%d",&x,&y); v[x%d+1][y%d+1]++; } int Mx=0,t; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j],Mx=max(Mx,v[i][j]); for (t=0;(t+1)*(t+1)<Mx;t++); int l[3]={t*t+1,t*(t+1)+1,(int)1e9}; for (int i=1;i<=d*2;i++) for (int j=1;j<=d*2;j++) for (int t=0;t<2;t++){ s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if (l[t]<=v[i][j]) s[t][i][j]++; } int L=t*d,R=t*d+d-1,ans=R,mid; while (L<=R) if (check(mid=(L+R)>>1)) R=mid-1,ans=mid; else L=mid+1; cout << ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG_MODE #define DBG(n) n; #else #define DBG(n) ; #endif #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;} #define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 1000000007 typedef long long ll; typedef pair<ll,ll> P; int aa[2222][2222]; int aa1[2222][2222]; int a1a1[2222][2222]; int mp[2222][2222]; int getImos(int a,int b,int c,int d,int v[2222][2222]){ int ret = v[c][d]; if(a > 0){ ret -= v[a-1][d]; } if(b > 0){ ret -= v[c][b-1]; } if(a > 0 && b > 0){ ret += v[a-1][b-1]; } return ret; } int main(){ int n, d;cin >> n >> d; REP(i,n){ int x, y;cin >> x >> y; x %= d;y %= d; mp[y][x]++; } int ma = 0; REP(i,d){ REP(j,d){ while((ma+1) * (ma+1) < mp[i][j])ma++; } } REP(i,d){ REP(j,d){ if(mp[i][j] > ma * ma){ aa[i][j]++;aa[i+d][j]++;aa[i][j+d]++;aa[i+d][j+d]++; } if(mp[i][j] > ma * (ma + 1)){ aa1[i][j]++;aa1[i+d][j]++;aa1[i][j+d]++;aa1[i+d][j+d]++; } if(mp[i][j] > (ma + 1) * (ma + 1)){ a1a1[i][j]++;a1a1[i+d][j]++;a1a1[i][j+d]++;a1a1[i+d][j+d]++; } } } REP(i,2*d){ REP(j,2*d){ aa[i+1][j] += aa[i][j]; aa1[i+1][j] += aa1[i][j]; a1a1[i+1][j] += a1a1[i][j]; } } REP(i,2*d){ REP(j,2*d){ aa[i][j+1] += aa[i][j]; aa1[i][j+1] += aa1[i][j]; a1a1[i][j+1] += a1a1[i][j]; } } int l = -2; int r = d-1; while(r - l > 1){ int mid = (l + r) / 2; bool flag = false; REP(i,d){ REP(j,d){ int tmp = 0; tmp += getImos(i,j,i+mid,j+mid,a1a1); tmp += getImos(i,j+mid+1,i+mid,j+d-1,aa1); tmp += getImos(i+mid+1,j,i+d-1,j+mid,aa1); tmp += getImos(i+mid+1,j+mid+1,i+d-1,j+d-1,aa); if(tmp == 0)flag = true; } } if(flag)r = mid; else l = mid; } cout << d * ma + r << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define y1 __zzd001 using namespace std; typedef long long LL; LL read(){ LL x=0,f=1; char ch=getchar(); while (!isdigit(ch)&&ch!='-') ch=getchar(); if (ch=='-') f=0,ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?x:-x; } const int N=2005; int n,d; int v[N][N]; int s[3][N][N]; int sum(int x1,int y1,int x2,int y2,int t){ return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x){ int r=x%d; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++){ int x1=i,y1=j; int x2=x1+r,y2=y1+r; int x3=x1+d-1,y3=y1+d-1; if (!sum(x1,y2+1,x3,y3,1)&&!sum(x2+1,y1,x3,y3,1) &&!sum(x2+1,y2+1,x3,y3,0)) return 1; } return 0; } int main(){ n=read(),d=read(); memset(v,0,sizeof v); while (n--){ int x=read(),y=read(); v[x%d+1][y%d+1]++; } for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j]; int Mx=0; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) Mx=max(Mx,v[i][j]); int t; for (t=0;(t+1)*(t+1)<Mx;t++); int l[3]={t*t+1,t*(t+1)+1,(int)1e9}; for (int i=1;i<=d*2;i++) for (int j=1;j<=d*2;j++) for (int t=0;t<2;t++){ s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if (l[t]<=v[i][j]&&v[i][j]<l[t+1]) s[t][i][j]++; } int L=t*d,R=t*d+d-1,ans=R; while (L<=R){ int mid=(L+R)>>1; if (check(mid)) R=mid-1,ans=mid; else L=mid+1; } cout << ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <stdio.h> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <list> #include <iterator> #include <assert.h> #pragma warning(disable:4996) typedef long long ll; #define MIN(a, b) ((a)>(b)? (b): (a)) #define MAX(a, b) ((a)<(b)? (b): (a)) #define LINF 9223300000000000000 #define INF 2140000000 const long long MOD = 1000000007; //const long long MOD = 998244353; using namespace std; int a[1000][1000]; int ss0[2005][2005]; int ss[2005][2005]; int main(int argc, char* argv[]) { int n,D; scanf("%d%d", &n, &D); int i,j; for(i=0; i<n; i++) { int x,y; scanf("%d%d", &x, &y); a[x%D][y%D]++; } int max=0; for(i=0; i<D; i++) { for(j=0; j<D; j++) { max=MAX(max,a[i][j]); } } double tmp=sqrt((double)max); int mm=(int)tmp; if(mm*mm<max) { mm++; } assert(mm*mm>=max); mm--; //printf("%d\n", mm); for(i=0; i<D; i++) { for(j=0; j<D; j++) { if(a[i][j]<=mm*mm) { a[i][j]=0; } else if(a[i][j]<=mm*(mm+1)) { a[i][j]=1; } else { assert(a[i][j]<=(mm+1)*(mm+1)); a[i][j]=2; } } } for(i=0; i<D*2; i++) { for(j=0; j<D*2; j++) { ss0[i][j+1]=ss0[i][j]+a[i%D][j%D]; } } for(j=0; j<=D*2; j++) { for(i=0; i<D*2; i++) { ss[i+1][j]=ss[i][j]+ss0[i][j]; } } vector<int> max0(D*2),max1(D*2); for(i=0; i<D; i++) { for(j=0; j<D; j++) { max0[i]=MAX(max0[i],a[i][j]-1); max1[j]=MAX(max1[j],a[i][j]-1); } } for(i=0; i<D; i++) { max0[i+D]=max0[i]; max1[i+D]=max1[i]; } vector<int> smax0(D*2+1),smax1(D*2+1); for(i=0; i<D*2; i++) { smax0[i+1]=smax0[i]+max0[i]; smax1[i+1]=smax1[i]+max1[i]; } ll ans=LINF; for(i=0; i<D; i++) { for(j=0; j<D; j++) { int l=0, r=D; while(r-l>1) { int m=(l+r)/2; int tmp0=smax0[i+D]-smax0[i+m]; int tmp1=smax1[j+D]-smax1[j+m]; int tmp2=ss[i+D][j+D]-ss[i+m][j+D]-ss[i+D][j+m]+ss[i+m][j+m]; if(tmp0==0 && tmp1==0 && tmp2==0) { r=m; } else { l=m; } } ans=MIN(ans,D*mm+l); } } printf("%lld\n", ans); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>pii; typedef pair<pii,int>piii; const int N=1e3+5; int n,D,x,y,i,j,k,h[N][N],hh[N][N],ans=2e9,mx,v3[N*2][N*2],zz; vector<int>v1,v2; int main(){ scanf("%d%d",&n,&D); for(i=1;i<=n;++i)scanf("%d%d",&x,&y),h[x%D][y%D]++; for(i=0;i<D;++i)for(j=0;j<D;++j){ for(k=0;k*k<h[i][j];++k); if(k*(k-1)>=h[i][j])hh[i][j]=1; h[i][j]=k; if(k>mx)mx=k; } for(i=0;i<D;++i)for(j=0;j<D;++j)if(h[i][j]==mx){ if(!hh[i][j])v1.push_back(i),v2.push_back(j); // else v3.push_back(pii(i,j)); else ++v3[i][j],++v3[i+D][j+D],++v3[i][j+D],++v3[i+D][j],++zz; } for(i=D*2-1;i>=0;--i)for(j=D*2-1;j>=0;--j)v3[i][j]+=v3[i][j+1]; for(i=D*2-1;i>=0;--i)for(j=D*2-1;j>=0;--j)v3[i][j]+=v3[i+1][j]; sort(v1.begin(),v1.end());sort(v2.begin(),v2.end()); v1.erase(unique(v1.begin(),v1.end()),v1.end()); v2.erase(unique(v2.begin(),v2.end()),v2.end()); for(i=0;i<D;++i)for(j=0;j<D;++j){ int v=0; // for(int x:v1)v=max(v,(x+D-i)%D); if(!v1.empty()){if(i<=v1[0])v=max(v,v1.back()-i);else v=max(v,*--lower_bound(v1.begin(),v1.end(),i)+D-i);} if(!v2.empty()){if(j<=v2[0])v=max(v,v2.back()-j);else v=max(v,*--lower_bound(v2.begin(),v2.end(),j)+D-j);} // for(pii u:v3)v=max(v,min((u.first+D-i)%D,(u.second+D-j)%D)); int l=0,r=D-1,m; for(;l<r;){ m=(l+r)>>1; // if(v3[i+m][j+m]-(i?v3[i-1][j+m]:0)-(j?v3[i+m][j-1]:0)+(i && j?v3[i-1][j-1]:0)==zz)r=m; if(v3[i][j]-v3[i+m+1][j+m+1]-(v3[i][j+D]-v3[i+m+1][j+D])-(v3[i+D][j]-v3[i+D][j+m+1])==zz)r=m; else l=m+1; } v=max(v,l); v+=(mx-1)*D; if(v==7){ ++v,--v; } if(v<ans)ans=v; } cout<<ans<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; const int max_n = 100011, max_d = 1011, inf = 1000111222; int n, d, x[max_n], y[max_n], cnt[max_d][max_d]; int f[2 * max_d][2 * max_d]; int f2[2 * max_d][2 * max_d], fx[2 * max_d], fy[2 * max_d]; void proc(int f[][2 * max_d]) { for (int i = 1; i < 2 * d; ++i) { f[0][i] += f[0][i - 1]; f[i][0] += f[i - 1][0]; } for (int i = 1; i < 2 * d; ++i) { for (int j = 1; j < 2 * d; ++j) { f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1]; } } } int get_sum(int f[][2 * max_d], int lx, int ly, int rx, int ry) { int res = f[rx][ry]; if (lx) { res -= f[lx - 1][ry]; } if (ly) { res -= f[rx][ly - 1]; } if (lx && ly) { res += f[lx - 1][ly - 1]; } return res; } int get_sum(int f[2 * max_d], int l, int r) { if (l == 0) { return f[r]; } return f[r] - f[l - 1]; } bool check(int len) { long long cnt2 = len / d; long long cnt1 = cnt2 + 1; long long cnt3 = cnt1 * cnt2; cnt2 *= cnt2; cnt1 *= cnt1; if (cnt2 >= n) { return true; } memset(f, 0, sizeof(f)); memset(f2, 0, sizeof(f2)); memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); int bad = 0; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { if (cnt[i][j] > cnt1) { return false; } if (cnt[i][j] <= cnt2) { continue; } //cout << i << " " << j << ": " << cnt[i][j] << endl; if (cnt[i][j] <= cnt3) { f2[i][j] = 1; f2[i][j + d] = 1; f2[i + d][j] = 1; f2[i + d][j + d] = 1; ++fx[i]; ++fx[i + d]; ++fy[j]; ++fy[j + d]; ++bad; } else if (cnt[i][j] > cnt2) { f[i][j] = 1; f[i + d][j] = 1; f[i][j + d] = 1; f[i + d][j + d] = 1; //cout << i << " " << j << endl; ++bad; } } } proc(f); proc(f2); for (int i = 1; i < 2 * d; ++i) { fx[i] += fx[i - 1]; fy[i] += fy[i - 1]; } int x = len % d; for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { int c = get_sum(f, i, j, i + x, j + x); c += get_sum(fx, i, i + x); c += get_sum(fy, j, j + x); c -= get_sum(f2, i, j, i + x, j + x); /*if (cnt[i][j]) { cout << i << " " << j << ": " << c << ", bad = " << bad << endl; }*/ if (c == bad) { return true; } } } return false; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); scanf("%d%d", &n, &d); for (int i = 0; i < n; ++i) { scanf("%d%d", &x[i], &y[i]); ++cnt[x[i] % d][y[i] % d]; } /*for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { if (cnt[i][j]) { cout << i << " " << j << ": " << cnt[i][j] << endl; } } }*/ //cout << check(4) << endl; //return 0; int l = 0, r = inf; while (r - l > 1) { int mid = (l + r) / 2; if (check(mid)) { r = mid; } else { l = mid; } } printf("%d\n", r); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) int n, d, e[1005][1005], l, r, m, s, o, p; bool h[1005], w[1005], bt[1005][1005]; int main(){ scanf("%d%d", &n, &d); rep(i,n){ scanf("%d%d", &l, &r); m = max(m,++e[l%d][r%d]); } for(s=1; s*s < m; s++); rep(i,d) rep(j,d){ if(s*(s-1) < e[i][j]) h[i] = w[j] = true; else if((s-1)*(s-1) < e[i][j]) bt[i][j] = true; } l=0;r=d; while(l+1 < r){ int c[1005] = {}, o = 0; bool ok = false; m = (l+r) / 2; rep(i,d){ for(; o < i+m && !h[o%d]; o++) rep(j,d) if(bt[o%d][j]) c[j]++; if(o < i+m){ rep(j,d) c[j] = 0; i = o++; continue; } int p = 0; rep(j,d){ for(; p < j+m && !w[p%d] && !c[p%d]; p++); if(p < j+m) j = p++; else ok = true; } rep(j,d) if(bt[i][j]) c[j]--; } (ok ? l : r) = m; } printf("%d\n", s*d-l-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,d,v[3000][3000],s[3][3000][3000]; int S(int x1,int y1,int x2,int y2,int t) { return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x) { int r=x%d; for(int i=1;i<=d;i++) for(int j=1;j<=d;j++) { int x1=i,y1=j,x2=x1+r,y2=y1+r,x3=x1+d-1,y3=y1+d-1; if(!S(x1,y2+1,x3,y3,1)&&!S(x2+1,y1,x3,y3,1)&&!S(x2+1,y2+1,x3,y3,0)) return 1; } return 0; } int main() { scanf("%d%d",&n,&d); memset(v,0,sizeof v); for(int i=0;i<n;i++) { int x,y; scanf("%d%d",&x,&y); v[x%d+1][y%d+1]++; } int Mx=0,t; for(int i=1;i<=d;i++) for(int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j],Mx=max(Mx,v[i][j]); for(t=0;(t+1)*(t+1)<Mx;t++); int l[3]={t*t+1,t*(t+1)+1,(int)1e9}; for(int i=1;i<=d*2;i++) for(int j=1;j<=d*2;j++) for(int t=0;t<2;t++) { s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if(l[t]<=v[i][j]) s[t][i][j]++; } int L=t*d,R=t*d+d,ans=R,mid; while(L<=R) if(check(mid=(L+R)>>1)) R=mid-1,ans=mid; else L=mid+1; cout<<ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #include <sstream> #include <complex> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> using namespace std; #define mod 1000000007 #define FOR(x,to) for(int x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) #define long long long inline int rei(){int x;cin>>x;return x;} inline long rel(){long x;cin>>x;return x;} inline string res(){string x;cin>>x;return x;} //------------------------------------------------------- int Num[1000000]; int D; int Map2[1000000]; int Map3[1000000]; int C2; int C3; int Mapsum2(int l,int r,int d,int u){ int c = Map2[r*D+u]; if(l != 0){ c -= Map2[(l-1)*D+u]; } if(d != 0){ c -= Map2[r*D+d-1]; } if(l != 0 && d != 0){ c += Map2[(l-1)*D+d-1]; } return c; } int Mapsum3(int l,int r,int d,int u){ int c = Map3[r*D+u]; if(l != 0){ c -= Map3[(l-1)*D+u]; } if(d != 0){ c -= Map3[r*D+d-1]; } if(l != 0 && d != 0){ c += Map3[(l-1)*D+d-1]; } return c; } int MapSum2(int l,int r,int d,int u){ if(u >= D){ if(r >= D){ return Mapsum2(0,r-D,0,u-D) + Mapsum2(l,D-1,d,D-1) + Mapsum2(0,r-D,d,D-1) + Mapsum2(l,D-1,0,u-D); } else{ return Mapsum2(l,r,0,u-D) + Mapsum2(l,r,d,D-1); } } else{ if(r >= D){ return Mapsum2(0,r-D,d,u) + Mapsum2(l,D-1,d,u); } else{ return Mapsum2(l,r,d,u); } } } int MapSum3(int l,int r,int d,int u){ if(u >= D){ if(r >= D){ return Mapsum3(0,r-D,0,u-D) + Mapsum3(l,D-1,d,D-1) + Mapsum3(0,r-D,d,D-1) + Mapsum3(l,D-1,0,u-D); } else{ return Mapsum3(l,r,0,u-D) + Mapsum3(l,r,d,D-1); } } else{ if(r >= D){ return Mapsum3(0,r-D,d,u) + Mapsum3(l,D-1,d,u); } else{ return Mapsum3(l,r,d,u); } } } bool check(int bc){ for(int i=0;i<D;i++){ for(int j=0;j<D;j++){ int c2 = MapSum2(0,D-1,j,j+bc-1) + MapSum2(i,i+bc-1,0,D-1) - MapSum2(i,i+bc-1,j,j+bc-1); int c3 = MapSum3(i,i+bc-1,j,j+bc-1); if(c2 == C2 && c3 == C3){ return true; } } } return false; } void Calc(){ int N = rei(); D = rei(); for(int i=0;i<N;i++){ int x = rei(); int y = rei(); Num[(x % D) * D + y % D]++; } int maxN = 0; for(int i=0;i<D*D;i++){ maxN = max(maxN,Num[i]); } int oneline; for(oneline=0;;oneline++){ if(oneline*oneline >= maxN){ break; } } long c1 = (oneline-1)*(oneline-1); long c2 = (oneline-1)*oneline; long c3 = oneline*oneline; for(int i=0;i<D*D;i++){ if(Num[i] > c2){ Map3[i]++; C3++; } else if(Num[i] > c1){ Map2[i]++; C2++; } } for(int i=0;i<D*D;i++){ if(i/D != 0){ Map2[i] += Map2[i-D]; Map3[i] += Map3[i-D]; } if(i%D != 0){ Map2[i] += Map2[i-1]; Map3[i] += Map3[i-1]; } if((i%D != 0) && (i/D != 0)){ Map2[i] -= Map2[i-D-1]; Map3[i] -= Map3[i-D-1]; } } int bf = 1; int bl = D; while(bf != bl){ long bc = (bf+bl)/2; if(check(bc)){ bl = bc; } else{ bf = bc+1; } } cout << bf-1 + (oneline-1)*D << endl; } int main(int argc,char** argv){ ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define _rep(i,n) _repl(i,0,n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define pb push_back #define all(x) begin(x),end(x) #define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x)) #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string){cerr<<endl;} template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);} template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.first<<","<<p.second<<")";return o;} template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} constexpr int SIZE = 100; #else #define dbg(...) {} constexpr int SIZE = 4096; #endif int val[2][2005][2005]; int main(){ int n,d; cin>>n>>d; vector<vector<int>> v(d,vector<int>(d,0)); rep(i,n){ int x,y; cin>>x>>y; v[x%d][y%d]++; } long l = 1, r = n*d; while(r-l > 1) { long mid = (l+r)/2; auto f = [&](){ if (mid * mid < n) return false; if (mid%d==0){ rep(i,d)rep(j,d) if(v[i][j] > (mid/d) * (mid/d)) return false; return true; } rep(i,d) rep(j,d) if(v[i][j] > (mid/d+1) * (mid/d+1)) return false; fill(val[0][0], val[2][0], 0); rep(i,1,d+1) rep(j,1,d+1){ val[0][i][j] = val[0][i+d][j] = val[0][i][j+d] = val[0][i+d][j+d] = v[i-1][j-1] <= (mid/d + 1)*(mid/d); val[1][i][j] = val[1][i+d][j] = val[1][i][j+d] = val[1][i+d][j+d] = v[i-1][j-1] <= (mid/d)*(mid/d); } rep(k,2){ rep(i,2*d+1){ rep(j,2*d+1){ if(i>0) val[k][i][j] += val[k][i-1][j]; if(j>0) val[k][i][j] += val[k][i][j-1]; if(i>0 && j>0) val[k][i][j] -= val[k][i-1][j-1]; } } } rep(i,d) rep(j,d){ if(val[1][i + d][j + d] - val[1][i + d][j + mid%d] - val[1][i + mid%d][j + d] + val[1][i + mid%d][j + mid%d] != (d - mid%d)*(d - mid%d)) continue; if(val[0][i + mid%d][j + d] - val[0][i][j + d] - val[0][i + mid%d][j + mid%d] + val[0][i][j + mid%d] != (d - mid%d)*(mid%d)) continue; if(val[0][i + d][j + mid%d] - val[0][i + d][j] - val[0][i + mid%d][j + mid%d] + val[0][i + mid%d][j] != (d - mid%d)*(mid%d)) continue; return true; } return false; }; if (f()) r = mid; else l = mid; } cout << l << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100100, maxd = 2020; int x[maxn], y[maxn]; int cnt[maxd][maxd]; int p[3][maxd][maxd]; long long lim[3]; int main(){ int n, d; cin >> n >> d; for(int i = 0; i < n; i++) cin >> x[i] >> y[i]; for(int i = 0; i < n; i++) cnt[x[i] % d][y[i] % d]++; for(int i = 0; i < d; i++) for(int j = 0; j < d; j++) cnt[i + d][j] = cnt[i][j + d] = cnt[i + d][j + d] = cnt[i][j]; int lo = 1, hi = n * d, ans = n * d; while(lo <= hi){ int mid = (lo + hi)>>1; int a = mid / d, b = mid % d; lim[0] = 1ll * (a + 1) * (a + 1); lim[1] = 1ll * (a + 1) * a; lim[2] = 1ll * a * a; for(int i = 0; i < 2*d; i++) for(int j = 0; j < 2*d; j++){ for(int k = 0; k < 3; k++){ p[k][i][j] = cnt[i][j] <= lim[k]; if(i) p[k][i][j] += p[k][i-1][j]; if(j) p[k][i][j] += p[k][i][j-1]; if(i && j) p[k][i][j] -= p[k][i-1][j-1]; } } auto query = [&](int ty, int x, int y, int xe, int ye){ if(x > xe || y > ye) return 0; int ans = p[ty][xe][ye]; if(x) ans -= p[ty][x - 1][ye]; if(y) ans -= p[ty][xe][y - 1]; if(x && y) ans += p[ty][x - 1][y - 1]; return ans; }; bool ok = false; for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ int p1 = query(0, i, j, i + b - 1, j + b - 1) == b * b; int p2 = (query(1, i + b, j, i + d - 1, j + b - 1) + query(1, i, j + b, i + b - 1, j + d - 1)) == 2 * b * (d - b); int p3 = query(2, i + b, j + b, i + d - 1, j + d - 1) == (d - b) * (d - b); if(p1 && p2 && p3) ok = true; } } if(ok) ans = mid, hi = mid - 1; else lo = mid + 1; } cout << ans - 1 << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; int a[2010][2010]; int sum1[2010][2010], sum2[2010][2010]; int n, d, now; int chmax(int &x, int y){ x = x > y ? x : y; } int count1(int a, int b, int c, int d){ if (a > c || b > d) return 0; return sum1[c][d] - sum1[a - 1][d] - sum1[c][b - 1] + sum1[a - 1][b - 1]; } int count2(int a, int b, int c, int d){ if (a > c || b > d) return 0; return sum2[c][d] - sum2[a - 1][d] - sum2[c][b - 1] + sum2[a - 1][b - 1]; } int check(int k){ for (int i = 1; i <= d; i++){ for (int j = 1; j <= d; j++){ int flag = 1; if (count1(i + k, j + k, d + i - 1, d + j - 1) != (d - k) * (d - k)) flag = 0; if (count2(1, 1, d, d) - count2(i, j, i + k - 1, j + k - 1) != d * d - k * k) flag = 0; if (flag) return 1; } } return 0; } int main(){ scanf("%d%d", &n, &d); for (int i = 0; i < n; i++){ int x, y; scanf("%d%d", &x, &y); a[x % d + 1][y % d + 1]++; } int maxj = 0; for (int i = 1; i <= d; i++){ for (int j = 1; j <= d; j++){ maxj = max(maxj, a[i][j]); } } now = 1; while (now * now < maxj) now++; now--; for (int i = 1; i <= 2 * d; i++){ for (int j = 1; j <= 2 * d; j++){ a[i][j] = a[(i - 1) % d + 1][(j - 1) % d + 1]; sum1[i][j] = sum1[i - 1][j] + sum1[i][j - 1] - sum1[i - 1][j - 1] + (a[i][j] <= now * now); sum2[i][j] = sum2[i - 1][j] + sum2[i][j - 1] - sum2[i - 1][j - 1] + (a[i][j] <= now * (now + 1)); } } int l = 0, r = d; while (l < r){ int mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } printf("%d\n", now * d - 1 + l); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
n,d = map(int,input().split()) num = [[0]*d for i in range(d)] a = 0 for i in range(n): x,y = map(int,input().split()) x %=d y%=d num[x][y] += 1 a = max(a,num[x][y]) x=1 while x*x<a: x += 1 r = (x-1)*d a = x-1 dai = d-1 syo = 0 anum = [[[0]*d for i in range(d)]for i in range(3)] rui = [[[0]*(2*d+1) for i in range(2*d+1)]for i in range(3)] for i in range(d): for j in range(d): z = num[i][j] if z > (a+1)**2: anum[0][i][j]=1 anum[1][i][j]=1 anum[2][i][j]=1 elif z> a*(a+1): anum[1][i][j]=1 anum[2][i][j]=1 elif z > a*a: anum[1][i][j]=1 for x in range(3): for i in range(1,2*d+1): for j in range(1,2*d+1): rui[x][i][j]+=rui[x][i-1][j]+rui[x][i][j-1]-rui[x][i-1][j-1]+anum[x][(i-1)%d][(j-1)%d] def cheak(kon): for i in range(1,d+1): for j in range(1,d+1): if rui[0][i+kon][j+kon] - rui[0][i - 1][j+kon] - rui[0][i+kon][j - 1] + rui[0][i - 1][j - 1]: continue if rui[1][i+d-1][j+d-1] - rui[1][i+kon+1 - 1][j+d-1] - rui[1][i+d-1][j+kon+1 - 1] + rui[1][i+kon+1 - 1][j+kon+1 - 1]: continue if rui[2][i+d-1][j+kon] - rui[2][i+kon+1 - 1][j+kon] - rui[2][i+d-1][j - 1] + rui[2][i+kon+1 - 1][j - 1]: continue if rui[2][i+kon][j+d-1] - rui[2][i - 1][j+d-1] - rui[2][i+kon][j+kon+1 - 1] + rui[2][i - 1][j+kon+1 - 1]: continue return 1 return 0 kon = (dai+syo)//2 while True: if dai-syo <= 1: if cheak(syo) == 1: dai = syo break kon = (dai+syo)//2 c = cheak(kon) if c == 1: dai = kon else: syo = kon print(dai+r)
PYTHON3
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> using namespace std; int n, D; int c[1001][1001]; int A[1001][1001][3]; int sum_A[2002][2002][3]; int count(int k, int x1, int y1, int x2, int y2) { return sum_A[x2][y2][k] - sum_A[x1 - 1][y2][k] - sum_A[x2][y1 - 1][k] + sum_A[x1 - 1][y1 - 1][k]; } bool check(int b) { for (int oy = 1; oy <= D; oy++) { for (int ox = 1; ox <= D; ox++) { if (count(0, ox, oy, ox + b, oy + b)) continue; if (count(1, ox + b + 1, oy + b + 1, ox + D - 1, oy + D - 1)) continue; if (count(2, ox + b + 1, oy, ox + D-1, oy + b)) continue; if (count(2, ox, oy + b + 1, ox + b, oy + D - 1)) continue; return true; } } return false; } int main(void) { cin >> n >> D; int cnt_max = 0; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; c[x % D][y % D]++; cnt_max = max(c[x % D][y % D], cnt_max); } int a = 0; while (cnt_max > (a + 1) * (a + 1)) a++; for (int j = 0; j < D; j++) { for (int i = 0; i < D; i++) { A[i][j][0] = c[i][j] > (a + 1) * (a + 1); A[i][j][1] = c[i][j] > a * a; A[i][j][2] = c[i][j] > a * (a + 1); } } for (int k = 0; k < 3; k++) for (int j = 1; j <= 2 * D; j++) for (int i = 1; i <= 2 * D; i++) sum_A[i][j][k] += sum_A[i][j-1][k] + sum_A[i-1][j][k] - sum_A[i-1][j-1][k] + A[(i-1) % D][(j-1) % D][k]; int l = -1, r = D - 1; while (l + 1 < r) { int b = (l + r) / 2; if (check(b)) r = b; else l = b; } cout << a * D + r << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
//https://beta.atcoder.jp/contests/dwacon5th-prelims/tasks/dwacon5th_prelims_d #include<bits/stdc++.h> using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if(1) #else #define CIN_ONLY if(1) struct cww {cww() {CIN_ONLY{ios::sync_with_stdio(false); cin.tie(0);}}}star; #define DEBUG if(0) #endif #define ALL(v) (v).begin(),(v).end() #define REC(ret, ...) std::function<ret (__VA_ARGS__)> template <typename T>inline bool chmin(T &l, T r){bool a = l>r; if (a)l = r; return a;} template <typename T>inline bool chmax(T &l, T r){bool a = l<r; if (a)l = r; return a;} template <typename T>istream& operator>>(istream &is, vector<T> &v){for (auto &it : v)is >> it;return is;} class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }}; int N; int D; int cnt[1123][1123]; int B[2123][2123]; int C[2123][2123]; bool f(int r){ //cout<<(r/D)*(LL)(r/D)<<endl; for(int i:range(D)){ for(int j:range(D)){ bool ok=true; for(int s:range(D)){ for(int t:range(D)){ int ni=(i+s)%D; int nj=(j+t)%D; LL rc=r/D; if(ni<r-rc*D)rc++; LL cc=r/D; if(nj<r-cc*D)cc++; if(rc*cc<cnt[s][t]){ ok=false; } } } if(ok)return true; } } return false; } bool ff(int r){ LL a=(r/D)*(LL)(r/D); LL b=(r/D)*(LL)(r/D+1); LL c=(r/D+1)*(LL)(r/D+1); int md=r%D; int sum=0; for(int i:range(D))for(int j:range(D)){ B[i+1][j+1]=B[i+1+D][j+1]=B[i+1][j+1+D]=B[i+1+D][j+1+D]=0; C[i+1][j+1]=C[i+1+D][j+1]=C[i+1][j+1+D]=C[i+1+D][j+1+D]=0; if(cnt[i][j]>c)return false; if(cnt[i][j]<=a)continue; sum++; if(cnt[i][j]<=b){ B[i+1][j+1]=B[i+1+D][j+1]=B[i+1][j+1+D]=B[i+1+D][j+1+D]=1; } if(cnt[i][j]<=c){ C[i+1][j+1]=C[i+1+D][j+1]=C[i+1][j+1+D]=C[i+1+D][j+1+D]=1; } } for(int i:range(2*D))for(int j:range(2*D)){ B[i+1][j+1]+=B[i+1][j]+B[i][j+1]-B[i][j]; C[i+1][j+1]+=C[i+1][j]+C[i][j+1]-C[i][j]; } for(int i:range(D))for(int j:range(D)){ int k = 0; k+= C[i+md][j+md]-C[i][j+md]-C[i+md][j]+C[i][j]; k+= B[i+md][j+D]-B[i][j+D]-B[i+md][j+md]+B[i][j+md]; k+= B[i+D][j+md]-B[i+md][j+md]-B[i+D][j]+B[i+md][j]; if(k==sum)return true; } return false; } int main(){ cin>>N>>D; //if(D>30)return -1; for(int i:range(N)){ int x,y; cin>>x>>y; cnt[x%D][y%D]++; } int lb=0,ub=1e7; while(ub-lb>1){ const int mid=(lb+ub)/2; if(ff(mid))ub=mid; else lb=mid; } cout<<lb<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
//Δ_D #include<iostream> #include<cstdio> #include<fstream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<bitset> #include<cmath> #include<cstring> #include<cstdlib> using namespace std; typedef long long LL; typedef double DB; const int N = 2222; const int inf = 2e9; int sq(int x){ int i=0; while(i*i<x){ i++; } return i; } int n,m,a[N][N],b[N][N],c[N],d[N],e[N][N],f[N][N],g[N][N]; int main() { int i,j,o,u; scanf("%d%d",&m,&n); while(m--){ scanf("%d%d",&i,&j); a[i%n][j%n]++; } u=0; for(i=0;i<n;i=i+1) for(j=0;j<n;j=j+1) u=max(u,sq(a[i][j])); for(i=0;i<n;i=i+1){ for(j=0;j<n;j=j+1){ if(a[i][j]>u*(u-1)) b[i][j]=1; if(a[i][j]>(u-1)*(u-1)) a[i][j]=1; else a[i][j]=0; } } for(i=0;i<n+n;i=i+1) for(j=0;j<n+n;j=j+1) a[i][j]=a[i%n][j%n],b[i][j]=b[i%n][j%n]; for(i=0;i<n+n;i=i+1) for(j=0;j<n+n;j=j+1) if(b[i][j]) c[i]=1,d[j]=1; o=0; for(i=0;i<n+n;i=i+1){ if(i>=n) c[i-n]=o-(i-n); if(c[i]) o=i; } o=0; for(i=0;i<n+n;i=i+1){ if(i>=n) d[i-n]=o-(i-n); if(d[i]) o=i; } for(i=0;i<n+n;i=i+1){ for(j=0;j<n+n;j=j+1){ if(a[i][j]||!j) e[i][j]=0; else e[i][j]=e[i][j-1]+1; if(a[i][j]||!i) f[i][j]=0; else f[i][j]=f[i-1][j]+1; g[i][j]=min(e[i][j],f[i][j]); if(i&&j) g[i][j]=min(g[i-1][j-1]+1,g[i][j]); } } o=n; for(i=0;i<n;i=i+1) for(j=0;j<n;j=j+1) o=min(o,max(n-1-g[i+n-1][j+n-1],max(c[i],d[j]))); cout<<(u-1)*n+o; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef pair<double, double> pdd; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef vector<pii> vii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; //const int mod = ; int a[2005][2005]; int s1[2005][2005]; int s2[2005][2005]; int calc1(int i, int j, int l) { return s1[i + l][j + l] - s1[i][j + l] - s1[i + l][j] + s1[i][j]; } int calc2(int i, int j, int l) { return s2[i + l][j + l] - s2[i][j + l] - s2[i + l][j] + s2[i][j]; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, d; cin >> n >> d; vvi c(d, vi(d)); int sz = 1; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; x %= d; y %= d; c[x][y]++; } for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { while (sz * sz < c[i][j]) ++sz; } vvi t(d, vi(d)); int cnt2 = 0; for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { if (sz * (sz - 1) < c[i][j]) { t[i][j] = 2; ++cnt2; } else if ((sz - 1) * (sz - 1) < c[i][j]) t[i][j] = 1; } for (int i = 0; i < 2 * d; ++i) for (int j = 0; j < 2 * d; ++j) { a[i][j] = t[i % d][j % d]; } for (int i = 0; i < 2 * d; ++i) for (int j = 0; j < 2 * d; ++j) { s1[i + 1][j + 1] = s1[i][j + 1] + s1[i + 1][j] - s1[i][j] + (int)(a[i][j] == 1); s2[i + 1][j + 1] = s2[i][j + 1] + s2[i + 1][j] - s2[i][j] + (int)(a[i][j] == 2); } int l = -1, r = d; while (r - l > 1) { int x = (l + r) / 2; bool ok = 0; for (int i = 0; i < d; ++i) for (int j = 0; j < d; ++j) { if (calc2(i, j, x + 1) == cnt2 && calc1(i + x + 1, j + x + 1, d - x - 1) == 0) { ok = 1; break; } } if (ok) r = x; else l = x; } cout << (sz - 1) * d + r << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; int n,d,x,y,t,mx,v[2005][2005],s[2][2005][2005]; int S(int a1,int b1,int a2,int b2,int t){return s[t][a2][b2]-s[t][a1-1][b2]-s[t][a2][b1-1]+s[t][a1-1][b1-1];} int chk(int x) { int r=x%d; for(int i=1;i<=d;i++) for(int j=1;j<=d;j++) { int a1=i,b1=j,a2=a1+r,b2=b1+r,x3=a1+d-1,y3=b1+d-1; if(!S(a1,b2+1,x3,y3,1)&&!S(a2+1,b1,x3,y3,1)&&!S(a2+1,b2+1,x3,y3,0)) return 1; } return 0; } int main() { scanf("%d%d",&n,&d); for(int i=1;i<=n;i++) scanf("%d%d",&x,&y),v[x%d+1][y%d+1]++; for(int i=1;i<=d;i++) for(int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j],mx=max(mx,v[i][j]); for(t=0;(t+1)*(t+1)<mx;t++);int l[3]={t*t+1,t*t+t+1,(int)1e9}; for(int i=1;i<=d*2;i++) for(int j=1;j<=d*2;j++) for(int k=0;k<2;k++) s[k][i][j]=(l[k]<=v[i][j])+s[k][i-1][j]+s[k][i][j-1]-s[k][i-1][j-1]; int L=t*d,R=t*d+d,ans=R,mid; for(mid=(L+R)>>1;L<=R;mid=(L+R)>>1) if(chk(mid)) R=mid-1,ans=mid;else L=mid+1; printf("%d\n",ans); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; template <class T> class cumulativeSum { private: int ny, nx; vector<vector<T> > sum; public: cumulativeSum(const vector<vector<T> >& a) { ny = a.size(); nx = a[0].size(); sum.assign(ny+1, vector<T>(nx+1, 0)); for(int i=0; i<ny; ++i){ for(int j=0; j<nx; ++j){ sum[i+1][j+1] = a[i][j] + sum[i][j+1] + sum[i+1][j] - sum[i][j]; } } } T getSum(int y1, int x1, int y2, int x2) { y1 = max(y1, 0); x1 = max(x1, 0); y2 = min(y2, ny); x2 = min(x2, nx); if(y1 >= y2 || x1 >= x2) return 0; return sum[y2][x2] - sum[y1][x2] - sum[y2][x1] + sum[y1][x1]; } }; bool solve(const vector<vector<int> >& cnt, int len) { int d = cnt.size(); int a = len / d; int b = len % d; vector<vector<int> > v1(2*d, vector<int>(2*d, 0)); vector<vector<int> > v2(2*d, vector<int>(2*d, 0)); for(int i=0; i<2*d; ++i){ for(int j=0; j<2*d; ++j){ int y = i % d; int x = j % d; if((a + 1) * (a + 1) < cnt[y][x]) return false; if(a * a < cnt[y][x]){ ++ v1[i][j]; if(a * (a + 1) < cnt[y][x]) ++ v2[i][j]; } } } cumulativeSum<int> cs1(v1), cs2(v2); for(int y=0; y<d; ++y){ for(int x=0; x<d; ++x){ if(cs1.getSum(y+b, x+b, y+d, x+d) == 0 && cs2.getSum(y+b, x, y+d, x+b) == 0 && cs2.getSum(y, x+b, y+b, x+d) == 0) return true; } } return false; } int main() { int n, d; cin >> n >> d; vector<vector<int> > cnt(d, vector<int>(d, 0)); for(int i=0; i<n; ++i){ int x, y; cin >> x >> y; y %= d; x %= d; ++ cnt[y][x]; } int left = 1; int right = 1000 * d; while(left < right){ int mid = (left + right) / 2; if(solve(cnt, mid)) right = mid; else left = mid + 1; } cout << (left - 1) << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> int num[2000][2000]; int num2[2000][2000]; int num3[2000][2000]; int main() { int N, D; std::cin >> N >> D; for (int i = 0, x, y; i < N; i++) { std::cin >> x >> y; num[y % D][x % D]++; num[D + y % D][x % D]++; num[y % D][D + x % D]++; num[D + y % D][D + x % D]++; } int max = 0; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { max = std::max(max, num[i][j]); } } int a = 0; for (; (a + 1) * (a + 1) < max; a++) {} for (int i = 0; i < 2 * D; i++) { for (int j = 0; j < 2 * D; j++) { num2[i][j] = num[i][j] > a * a ? 1 : 0, num3[i][j] = num[i][j] > a * (a + 1) ? 1 : 0; } } for (int i = 0; i < 2 * D; i++) { for (int j = 1; j < 2 * D; j++) { num2[i][j] += num2[i][j - 1], num3[i][j] += num3[i][j - 1]; } } for (int i = 1; i < 2 * D; i++) { for (int j = 0; j < 2 * D; j++) { num2[i][j] += num2[i - 1][j], num3[i][j] += num3[i - 1][j]; } } auto get2 = [&](const int xl, const int xr, const int yl, const int yr) { return num2[yr - 1][xr - 1] - (yl == 0 ? 0 : num2[yl - 1][xr - 1]) - (xl == 0 ? 0 : num2[yr - 1][xl - 1]) + (yl == 0 or xl == 0 ? 0 : num2[yl - 1][xl - 1]); }; auto get3 = [&](const int xl, const int xr, const int yl, const int yr) { return num3[yr - 1][xr - 1] - (yl == 0 ? 0 : num3[yl - 1][xr - 1]) - (xl == 0 ? 0 : num3[yr - 1][xl - 1]) + (yl == 0 or xl == 0 ? 0 : num3[yl - 1][xl - 1]); }; auto ok = [&](const int b) { for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { const int n2 = get2(i + b, i + D, j + b, j + D); const int n3 = get3(i + b, i + D, j, j + b) + get3(i, i + b, j + b, j + D); if (n3 == 0 and n2 == 0) { return true; } } } return false; }; int inf = 0, sup = D + 1; while (sup - inf > 1) { const int mid = (inf + sup) / 2; (ok(mid) ? sup : inf) = mid; } std::cout << a * D + inf << std::endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <iostream> #include <algorithm> #include <vector> #include <set> #define llint long long using namespace std; llint n, d; llint x[100005], y[100005]; llint cnt[1005][1005], need[1005][1005]; llint type[1005][1005]; llint t1, t2; llint sum1[2005][2005], sum2[2005][2005]; vector<llint> vec; llint rect(llint sx, llint sy, llint tx, llint ty, llint sum[2005][2005]) { llint ret = sum[tx][ty]; if(sx > 0) ret -= sum[sx-1][ty]; if(sy > 0) ret -= sum[tx][sy-1]; if(sx > 0 && sy > 0) ret += sum[sx-1][sy-1]; return ret; } bool check(llint i, llint j, llint a) { if(rect(i, j, i+a-1, j+a-1, sum2) < t2) return false; if(rect(i+a, j+a, i+d-1, j+d-1, sum1) > 0) return false; return true; } int main(void) { cin >> n >> d; for(int i = 0; i*i <= 1e9+7; i++) vec.push_back(i*i); for(int i = 1; i <= n; i++) cin >> x[i] >> y[i]; for(int i = 1; i <= n; i++) cnt[x[i]%d][y[i]%d]++; for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ if(cnt[i][j] == 0) continue; need[i][j] = lower_bound(vec.begin(), vec.end(), cnt[i][j]) - vec.begin(); } } llint mx = 0, mn = 1e9; for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ mx = max(mx, need[i][j]); mn = min(mn, need[i][j]); } } if(mx == mn){ cout << d*mx-1 << endl; return 0; } for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ if(cnt[i][j] <= (mx-1)*(mx-1)) type[i][j] = 0; else if(cnt[i][j] <= mx*(mx-1)) type[i][j] = 1, t1++; else type[i][j] = 2, t2++; } } //cout << mx << " " << t1 << " " << t2 << endl << endl;; for(int i = 0; i < 2*d; i++){ for(int j = 0; j < 2*d; j++){ if(type[i%d][j%d] == 1) sum1[i][j]++; if(type[i%d][j%d] == 2) sum2[i][j]++; if(i > 0) sum1[i][j] += sum1[i-1][j], sum2[i][j] += sum2[i-1][j]; if(j > 0) sum1[i][j] += sum1[i][j-1], sum2[i][j] += sum2[i][j-1]; if(i > 0 && j > 0) sum1[i][j] -= sum1[i-1][j-1], sum2[i][j] -= sum2[i-1][j-1]; } } /*for(int i = 0; i < 2*d; i++){ for(int j = 0; j < 2*d; j++){ cout << sum2[i][j] << " "; } cout << endl; }*/ llint ans = d; for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ llint ub = d, lb = 0, mid; while(ub-lb>1){ mid = (ub+lb)/2; if(check(i, j, mid)) ub = mid; else lb = mid; } //cout << i << " " << j << " " << ub << endl; ans = min(ans, ub); } } cout << (mx-1)*d + ans - 1 << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; const int N=2005; int n,d,ans,res,a[N][N],b[N][N],c[N][N]; int sumc(int x,int y) { if(x<0||y<0) return 0; return c[x][y]; } int sumb(int x,int y) { if(x<0||y<0) return 0; return b[x][y]; } int getc(int x1,int y1,int x2,int y2) { return sumc(x2,y2)+sumc(x1-1,y1-1)-sumc(x2,y1-1)-sumc(x1-1,y2); } int getb(int x1,int y1,int x2,int y2) { return sumb(x2,y2)+sumb(x1-1,y1-1)-sumb(x2,y1-1)-sumb(x1-1,y2); } bool judge(int i,int j,int res) { if(getc(i+res+1,j,i+d-1,j+res)+getc(i,j+res+1,i+res,j+d-1)) return false; if(getb(i+res+1,j+res+1,i+d-1,j+d-1)) return false; return true; } int main() { scanf("%d%d",&n,&d); int mx=0; for(int i=1;i<=n;i++) { int x,y;scanf("%d%d",&x,&y); x%=d;y%=d; a[x][y]++; mx=max(mx,a[x][y]); } for(int i=0;i<d;i++) for(int j=0;j<d;j++) a[i+d][j+d]=a[i+d][j]=a[i][j+d]=a[i][j]; ans=sqrt(mx); if(ans*ans==mx) ans--; res=d-1; for(int i=0;i<2*d;i++) for(int j=0;j<2*d;j++) { if(a[i][j]>ans*ans) b[i][j]=1; if(a[i][j]>ans*(ans+1)) c[i][j]=1; } for(int i=0;i<2*d;i++) for(int j=1;j<2*d;j++) { b[i][j]+=b[i][j-1]; c[i][j]+=c[i][j-1]; } for(int i=1;i<2*d;i++) for(int j=0;j<2*d;j++) { b[i][j]+=b[i-1][j]; c[i][j]+=c[i-1][j]; } for(int i=0;i<d;i++) for(int j=0;j<d;j++) while(judge(i,j,res-1)) res--; printf("%d\n",ans*d+res); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define y1 __zzd001 using namespace std; const int N=2005; int n,d,v[N][N],s[3][N][N]; int S(int x1,int y1,int x2,int y2,int t){ return s[t][x2][y2]-s[t][x2][y1-1]-s[t][x1-1][y2]+s[t][x1-1][y1-1]; } int check(int x){ int r=x%d; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++){ int x1=i,y1=j,x2=x1+r,y2=y1+r,x3=x1+d-1,y3=y1+d-1; if (!S(x1,y2+1,x3,y3,1)&&!S(x2+1,y1,x3,y3,1)&&!S(x2+1,y2+1,x3,y3,0)) return 1; } return 0; } int main(){ scanf("%d%d",&n,&d); memset(v,0,sizeof v); while (n--){ int x,y; scanf("%d%d",&x,&y); v[x%d+1][y%d+1]++; } int Mx=0,t; for (int i=1;i<=d;i++) for (int j=1;j<=d;j++) v[i+d][j]=v[i][j+d]=v[i+d][j+d]=v[i][j],Mx=max(Mx,v[i][j]); for (t=0;(t+1)*(t+1)<Mx;t++); int l[3]={t*t+1,t*(t+1)+1,(int)1e9}; for (int i=1;i<=d*2;i++) for (int j=1;j<=d*2;j++) for (int t=0;t<2;t++){ s[t][i][j]=s[t][i-1][j]+s[t][i][j-1]-s[t][i-1][j-1]; if (l[t]<=v[i][j]) s[t][i][j]++; } int L=t*d,R=t*d+d,ans=R,mid; while (L<=R) if (check(mid=(L+R)>>1)) R=mid-1,ans=mid; else L=mid+1; cout << ans; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) int n,d; int cnt[1005][1005]; vector<int>forbid[1005]; bool a[1005],b[1005]; int par[1005],ran[1005]; void init(){ for(int i=0;i<1005;i++) par[i] = i, ran[i] = 0; } int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x); y = find(y); if(x==y) return; if(ran[x] < ran[y]) par[x] = y; else{ par[y] = x; if(ran[x] == ran[y]) ran[x]++; } } bool same(int x,int y){ return find(x)==find(y); } int main(){ scanf("%d%d",&n,&d); rep(i,n){ int x,y; scanf("%d%d",&x,&y); cnt[x%d][y%d]++; } int mx = 0; rep(i,d)rep(j,d) mx=max(mx,cnt[i][j]); ll lb = 0,ub = 1000; //(lb,ub] while(ub-lb>1){ ll mid = (lb+ub)/2; if(mid*mid >= mx) ub = mid; else lb = mid; } //1辺ub*ubあれば十分 if(d == 1){ cout << ub-1 << endl; return 0; } //どこまで減らせるか rep(i,d)rep(j,d){ if(ub*(ub-1) < cnt[i][j]){ a[i] = 1; b[j] = 1; } else if((ub-1)*(ub-1) < cnt[i][j]){ forbid[i].pb(j); } } int mxx = 0; rep(beg,d){ //いくついけるか、[lb,ub) int lb = 0,ub = d; while(ub-lb>1){ int mid = (lb+ub)/2; bool ok[1005]={}; for(int j=0;j<d;j++) ok[j] = b[j]; int cnt = mid; int nw = beg; int fail = 0; while(cnt--){ if(a[nw]){ fail = 1; break; } rep(h,forbid[nw].size()) ok[forbid[nw][h]] = 1; nw = (nw+1)%d; } if(fail){ ub = mid; continue; } init(); for(int i=0;i<d;i++){ int j = (i+1)%d; if(!ok[i] && !ok[j]) unite(i,j); } int sz[1005]={}; for(int i=0;i<d;i++){ if(ok[i]) continue; sz[find(i)]++; } int MX = 0; rep(i,d) MX = max(MX,sz[i]); if(MX >= mid){ lb = mid; } else{ ub = mid; } } mxx = max(mxx,lb); } cout << ub*d-1-mxx << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << (x) << endl #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){ return o<<"("<<p.fs<<","<<p.sc<<")"; } template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){ o<<"{"; for(const T& v:vc) o<<v<<","; o<<"}"; return o; } using ll = long long; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; int N,D; int cnt[1000][1000]; int s0[2010][2010]; int s2[2010][2010]; int Count(int a,int b,int c,int d,int t){ if(t==0){ return s0[c][d] - s0[a][d] - s0[c][b] + s0[a][b]; }else{ return s2[c][d] - s2[a][d] - s2[c][b] + s2[a][b]; } } int solve(VV<int>& A){ int N = A.size(); // puts("----------A---------"); // rep(i,N){ // rep(j,N) cout<<A[i][j]; // puts(""); // } // puts("-----------------------"); int num0 = 0, num2 = 0; rep(i,D) rep(j,D){ if(A[i][j]==0) num0++; if(A[i][j]==2) num2++; } int V = D+D; rep(i,V) rep(j,V){ s0[i+1][j+1] = (A[i%D][j%D]==0); s2[i+1][j+1] = (A[i%D][j%D]==2); } rep(i,V+1) rep(j,V) s0[i][j+1] += s0[i][j], s2[i][j+1] += s2[i][j]; rep(j,V+1) rep(i,V) s0[i+1][j] += s0[i][j], s2[i+1][j] += s2[i][j]; int ub = D+1, lb = -1; while(ub-lb>1){ int L = (ub+lb)/2; bool can = false; rep(i,D) rep(j,D){ if(Count(i,j,i+L,j+L,2) == num2 && Count(i+L,j+L,i+N,j+N,0) == (N-L)*(N-L)){ can = true; } } if(can) ub = L; else lb = L; } return ub; } int main(){ cin>>N>>D; rep(i,N){ int x,y; cin>>x>>y; cnt[x%D][y%D]++; } int cntmax = 0; rep(i,D) rep(j,D) chmax(cntmax,cnt[i][j]); int A = 0; while(A*A<cntmax) A++; VV<int> a(D,V<int>(D,0)); rep(i,D) rep(j,D){ if(cnt[i][j]<=(A-1)*(A-1)) a[i][j] = 0; else if(cnt[i][j]<=(A-1)*A) a[i][j] = 1; else a[i][j] = 2; } // show(cntmax); // show(A); cout<<(A-1)*D+solve(a)-1<<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #include <type_traits> using namespace std; using ll=int64_t; #define int ll #define FOR(i,a,b) for(int i=int(a);i<int(b);i++) #define REP(i,b) FOR(i,0,b) #define MP make_pair #define PB push_back #define EB emplace_back #define ALL(x) x.begin(),x.end() auto& errStream=cerr; #ifdef LOCAL #define cerr (cerr<<"-- line "<<__LINE__<<" -- ") #else class CerrDummy{}cerrDummy; template<class T> CerrDummy& operator<<(CerrDummy&cd,const T&){ return cd; } using charTDummy=char; using traitsDummy=char_traits<charTDummy>; CerrDummy& operator<<(CerrDummy&cd,basic_ostream<charTDummy,traitsDummy>&(basic_ostream<charTDummy,traitsDummy>&)){ return cd; } #define cerr cerrDummy #endif #define REACH cerr<<"reached"<<endl #define DMP(x) cerr<<#x<<":"<<x<<endl #define ZERO(x) memset(x,0,sizeof(x)) #define ONE(x) memset(x,-1,sizeof(x)) using pi=pair<int,int>; using vi=vector<int>; using ld=long double; template<class T,class U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os<<"("<<p.first<<","<<p.second<<")"; return os; } template<class T> ostream& operator <<(ostream& os,const vector<T>& v){ os<<"{"; REP(i,(int)v.size()){ if(i)os<<","; os<<v[i]; } os<<"}"; return os; } ll read(){ ll i; scanf("%" SCNd64,&i); return i; } void printSpace(){ printf(" "); } void printEoln(){ printf("\n"); } void print(ll x,int suc=1){ printf("%" PRId64,x); if(suc==1) printEoln(); if(suc==2) printSpace(); } string readString(){ static char buf[3341000]; scanf("%s",buf); return string(buf); } char* readCharArray(){ static char buf[3341000]; static int bufUsed=0; char* ret=buf+bufUsed; scanf("%s",ret); bufUsed+=strlen(ret)+1; return ret; } template<class T,class U> void chmax(T& a,U b){ if(a<b) a=b; } template<class T,class U> void chmin(T& a,U b){ if(b<a) a=b; } template<class T> T Sq(const T& t){ return t*t; } #define CAPITAL void Yes(bool ex=true){ #ifdef CAPITAL cout<<"YES"<<endl; #else cout<<"Yes"<<endl; #endif if(ex)exit(0); } void No(bool ex=true){ #ifdef CAPITAL cout<<"NO"<<endl; #else cout<<"No"<<endl; #endif if(ex)exit(0); } const ll infLL=LLONG_MAX/3; #ifdef int const int inf=infLL; #else const int inf=INT_MAX/2-100; #endif const int Nmax=1010; int d,buf[3][Nmax][Nmax]; int GetSum(int k,int y1,int y2,int x1,int x2){ int res=buf[k][y2-1][x2-1]; if(y1-1>=0) res-=buf[k][y1-1][x2-1]; if(x1-1>=0) res-=buf[k][y2-1][x1-1]; if(y1-1>=0&&x1-1>=0) res+=buf[k][y1-1][x1-1]; return res; } int Wafrelka(int k,int y1,int y2,int x1,int x2){ if(y2<=y1)return 0; if(x2<=x1)return 0; if(y1>=d){ return Wafrelka(k,y1-d,y2-d,x1,x2); } if(x1>=d){ return Wafrelka(k,y1,y2,x1-d,x2-d); } if(y2>d){ return Wafrelka(k,y1,d,x1,x2)+Wafrelka(k,0,y2-d,x1,x2); } if(x2>d){ return Wafrelka(k,y1,y2,x1,d)+Wafrelka(k,y1,y2,0,x2-d); } return GetSum(k,y1,y2,x1,x2); } void Prepare(int k){ REP(y,d)REP(x,d) buf[k][y][x+1]+=buf[k][y][x]; REP(y,d)REP(x,d) buf[k][y+1][x]+=buf[k][y][x]; } bool Ok(const int mid,const vector<vi>&cnt){ cerr<<mid<<endl; ZERO(buf); int p=mid/d; int q=d-mid%d; REP(y,d)REP(x,d){ if(cnt[y][x]<=p*p){ cerr<<0<<endl; buf[0][y][x]++; }else if(cnt[y][x]<=p*(p+1)){ cerr<<1<<endl; buf[1][y][x]++; }else if(cnt[y][x]<=(p+1)*(p+1)){ cerr<<2<<endl; buf[2][y][x]++; }else return false; } REP(k,3) Prepare(k); REP(y,d)REP(x,d){ int a00=Wafrelka(1,y,y+q,x,x+q)+Wafrelka(2,y,y+q,x,x+q); int a01=Wafrelka(2,y,y+q,x+q,x+d); int a10=Wafrelka(2,y+q,y+d,x,x+q); if(a00==0&&a01==0&&a10==0) return true; } return false; } signed main(){ int n=read(); d=read(); vector<vi> cnt(d,vi(d,0)); REP(i,n){ int x=read(),y=read(); cnt[y%d][x%d]++; } int low=0,high=d*n; while(high-low>1){ const int mid=(low+high)/2; if(Ok(mid,cnt)){ high=mid; }else{ low=mid; } } print(high-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; // when range [l, r), has property notP~P, want first P. // when return r, means not found. template <typename T> T bs_first(T l, T r, function<bool (T)> f) { assert(l < r); T mid; while (l != r) { mid = l + (r-l)/2; if (f(mid)) { r = mid; }else { l = mid + 1; } } return r; } const int N = 1024; int cnt[N][N]; int a[N<<1][N<<1]; int b[N<<1][N<<1]; void solve() { int n, D; cin >> n >> D; for (int _ = 0; _ < n; _++) { int x,y; cin >> x >> y; x%=D; y%=D; cnt[x][y]++; } n = 0; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { int k = 0; while (k*k < cnt[i][j]) { k++; } n = max(n,k); } } int z = (n-1)*n; int p = (n-1)*(n-1); int Z = 0; int P = 0; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (cnt[i][j] > z) { Z++; int x=i+1, y=j+1; a[x][y] = a[x][y+D] = a[x+D][y] = a[x+D][y+D] = 1; } else if (cnt[i][j] > p) { P++; int x=i+1, y=j+1; b[x][y] = b[x][y+D] = b[x+D][y] = b[x+D][y+D] = 1; } } } const int LIM = D<<1; for (int i = 1; i <= LIM; i++) { for (int j = 1; j <= LIM; j++) { a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]; b[i][j] += b[i-1][j] + b[i][j-1] - b[i-1][j-1]; } } auto sum = [&](int i, int j, int d){ return a[i+d][j+d] - a[i][j+d] - a[i+d][j] + a[i][j]; }; auto sum_b = [&](int i, int j, int d){ return b[i+d][j+d] - b[i][j+d] - b[i+d][j] + b[i][j]; }; auto cover = [&](int i, int j, int d){ return P - sum_b(i+d,j+d,D-d); }; if (D == 1) { cout << (n-1); return; } int res = D; for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { int tmp = bs_first<int>(1, D, [&](int d){ return sum(i,j,d) >= Z; }); int cov = bs_first<int>(1, D, [&](int d){ return cover(i,j,d) >= P; }); tmp = max(tmp, cov); res = min(res, tmp); } } res += (n-1)*D - 1; cout << res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
CPP