标签:cto 直线 NPU linker .com scanf cond stdin logs
for(int dir = 1; dir <= 4; dir++){ if(dir==2 || dir==4) for(int i=1; i<=n; i++)swap(p[i].x,p[i].y); else if(dir == 3) for(int i=1; i<=n; i++)p[i].x = -p[i].x;
...
}
for(int i = n; i >= 1; i--){ int pos = query(a[i]); if(pos!=-1){ addedge(p[i], p[pos]); } update(a[i], p[i].x + p[i].y, i); }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <list> #include <cstdlib> #include <iterator> #include <cmath> #include <iomanip> #include <bitset> #include <cctype> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int ,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //2147483647 const ll nmos = 0x80000000LL; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar(); while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); return x=f?-x:x; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------show time----------------------*/ const int maxn = 1e5+9; struct Point{ int x,y,id; bool operator < (const Point &q)const{ if(x == q.x)return y < q.y; return x < q.x; } }p[maxn]; struct BITnode{ int w,p; }BITsum[maxn]; struct Edge{ int u,v,dis;//曼哈顿距离 bool operator < (const Edge & e)const { return dis < e.dis; } } e[maxn << 3 | 1]; int tot = 0,fa[maxn]; int find(int x){ if(fa[x] == x)return x; else return fa[x] = find(fa[x]); } void addedge(Point a,Point b){ tot ++; e[tot].u = a.id; e[tot].v = b.id; e[tot].dis = abs(a.x - b.x) + abs(a.y-b.y); } int lowbit(int x){ return x & (-x); } int a[maxn],n,k,*ix[maxn]; bool cmp(int * x,int * y){ return (*x) < (*y); } int query(int x){ int mx = inf,p = -1; while(x <= n){ if(BITsum[x].w < mx){ mx = BITsum[x].w; p = BITsum[x].p; } x += lowbit(x); } return p; } void update(int x,int w,int p){ while(x > 0){ if(BITsum[x].w > w){ BITsum[x].w = w; BITsum[x].p = p; } x -= lowbit(x); } } int main(){ scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++){ scanf("%d%d", &p[i].x, &p[i].y); p[i].id = i; } // 建立边 tot = 0; for(int dir = 1; dir <= 4; dir++){ if(dir==2 || dir==4) for(int i=1; i<=n; i++)swap(p[i].x,p[i].y); else if(dir == 3) for(int i=1; i<=n; i++)p[i].x = -p[i].x; sort(p + 1, p + n + 1); //这里离散的操作我觉得也很厉害。 for(int i=1; i<=n; i++)a[i] = p[i].y - p[i].x, ix[i] = &a[i]; sort(ix + 1, ix + n + 1, cmp); for(int i=1; i<=n; i++) *ix[i] = i; for(int i = 1; i <= n; i++) BITsum[i].w = inf,BITsum[i].p = -1; for(int i = n; i >= 1; i--){ int pos = query(a[i]); if(pos!=-1){ addedge(p[i], p[pos]); } update(a[i], p[i].x + p[i].y, i); } } //库鲁斯卡尔 算法 开始 sort(e + 1,e + tot + 1); for(int i=1; i<=n; i++)fa[i] = i; int cnt = n - k,c1 = 0,ans = 0; for(int i=1; i<= tot; i++){ if( find(e[i].u) != find(e[i].v) ){ int px = find(e[i].u); int py = find(e[i].v); fa[px] = py; c1++; if(c1 == cnt){ ans = e[i].dis;break;} } } printf("%d\n", ans); return 0; }
标签:cto 直线 NPU linker .com scanf cond stdin logs
原文地址:https://www.cnblogs.com/ckxkexing/p/9524298.html