跳过正文
  1. Posts/

hdu 5626 Clarke and points (曼哈顿距离变换,拆点)

·2 分钟

http://acm.split.hdu.edu.cn/showproblem.php?pid=5626

题意:给出n(1E6)个点的二维坐标,问距离最远的两个点的距离是多少。

思路:对曼哈顿距离进行变换。

先看曼哈顿距离的定义

|x1−x2|+|y1−y2|

拆绝对值

x1−x2+y1−y2或x1−x2+y2−y1

x2−x1+y1−y2或x2−x1+y2−y1

即|x1+y1−(x2+y2)|或|x1−y1−(x2−y2)|

设x1+y1为x′,x1−y1为y′

则|x1′−x2′|或|y1′−y2′|

所以原要求1转化为

max(|x1′−x2′|,|y1′−y2′|)<=c

然后分别对x,y排序即可..最大的距离一定是y[n-1]-y[0]或者x[n-1]-x[0]

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月24日 星期三 23时42分15秒
 4File Name :code/hdu/5626.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E6+7;
34LL seed;
35int n;
36int x[N],y[N];
37
38inline LL rand(LL l,LL r)
39{
40    static LL mo =1E9+7,g=78125;
41    return l+((seed*=g)%=mo)%(r-l+1);
42}
43int main()
44{
45	#ifndef  ONLINE_JUDGE
46	freopen("code/in.txt","r",stdin);
47  #endif
48	ios::sync_with_stdio(false);
49	int T;
50	cin>>T;
51	while (T--)
52	{
53	    cin>>n>>seed;
54	    for ( int i = 0 ; i < n ; i++)
55	    {
56		int xx,yy;
57		xx = rand(-1000000000,1000000000);
58		yy = rand(-1000000000,1000000000);
59		x[i] = xx-yy;
60		y[i] = xx+yy;
61	    }
62	    sort(x,x+n);
63	    sort(y,y+n);
64	    int ans = -1;
65	    ans = max(ans,y[n-1]-y[0]);
66	    ans = max(ans,x[n-1]-x[0]);
67	    cout<<ans<<endl;
68	}
69
70  #ifndef ONLINE_JUDGE
71  fclose(stdin);
72  #endif
73    return 0;
74}

相关文章

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (曼哈顿距离的转化【拆点】+set+并查集)

http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意:了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9];Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群. 给出奶牛们的位置,请计算草原上有多少个牛群,以及最大的牛群里有多少奶牛

nthu 10925 - Advanced Heap Sort

·2 分钟
http://140.114.86.238/problem/10925/ Description 有兩個序列S1和S2,各有N個元素。當我們在S1,S2各取一個數字時,總共會有NN這麼多可能的”和”(sum)。請找出這NN這麼多和裡最小的N個值,並將它們加總後輸出。

bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 (分块)

·1 分钟
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意+思路: 同codeforces 13 E holes. 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月21日 星期日 02时29分39秒 4File Name :code/bzoj/2002.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=2E5+7; 34int n,m; 35int siz = 450; //sqrt(2E5) 36int a[N]; 37int pos[N]; 38int cnt[N]; 39int nxt[N]; 40int end[N]; 41 42 43void go( int x) 44{ 45 int ans = 0 ; 46 while (1) 47 { 48 if (x>=n) 49 { 50 printf("%d\n",ans); 51 break; 52 } 53 ans +=cnt[x]; 54 x = nxt[x]; 55// cout<<"nxt[x]:"<<nxt[x]<<endl; 56 } 57} 58void update ( int i,int j) 59{ 60 if (j>=n) 61 { 62 cnt[i]=1; 63 nxt[i]=n; 64 end[i]=i; 65 } 66 else 67 { 68 if (pos[i]==pos[j]) 69 { 70 cnt[i] = cnt[j] + 1; 71 nxt[i] = nxt[j]; 72 end[i]=end[j]; 73 } 74 else 75 { 76 cnt[i] = 1; 77 nxt[i] = j; 78 end[i] = end[j]; 79 } 80 } 81} 82int main() 83{ 84 #ifndef ONLINE_JUDGE 85 freopen("code/in.txt","r",stdin); 86 #endif 87 88 scanf("%d",&n); 89 for ( int i = 0 ; i < n ;i++) 90 { 91 scanf("%d",&a[i]); 92 pos[i] = i/siz; 93 } 94 95 for ( int i = n - 1 ; i >= 0 ; i--) update(i,i+a[i]); 96 97 scanf("%d",&m); 98 while (m--) 99 { 100 int opt; 101 scanf("%d",&opt); 102 if (opt==1) 103 { 104 int x; 105 scanf("%d",&x); 106 go(x); 107 } 108 else 109 { 110 int x,y; 111 scanf("%d %d",&x,&y); 112 a[x]=y; 113 update(x,x+a[x]); 114 int p = pos[x]*siz; 115 for ( int i = x-1 ; i >= p ; i--) update(i,i+a[i]); 116 } 117 } 118 119 #ifndef ONLINE_JUDGE 120 fclose(stdin); 121 #endif 122 return 0; 123}

codeforces 13 E. Holes (分块)

·2 分钟
http://codeforces.com/problemset/problem/13/E 题意:给你n个洞,进入某个洞后会跑到另一个洞,到了另一个洞之后又可能会继续到下一个洞,问你从一个洞进去,钻了几个洞才会出来,在哪个洞出来