• 【HDOJ】2295 Radar


    DLX+二分。

      1 /* 2295 */
      2 #include <iostream>
      3 #include <string>
      4 #include <map>
      5 #include <queue>
      6 #include <set>
      7 #include <stack>
      8 #include <vector>
      9 #include <deque>
     10 #include <algorithm>
     11 #include <cstdio>
     12 #include <cmath>
     13 #include <ctime>
     14 #include <cstring>
     15 #include <climits>
     16 #include <cctype>
     17 #include <cassert>
     18 #include <functional>
     19 #include <iterator>
     20 #include <iomanip>
     21 using namespace std;
     22 //#pragma comment(linker,"/STACK:102400000,1024000")
     23 
     24 #define sti                set<int>
     25 #define stpii            set<pair<int, int> >
     26 #define mpii            map<int,int>
     27 #define vi                vector<int>
     28 #define pii                pair<int,int>
     29 #define vpii            vector<pair<int,int> >
     30 #define rep(i, a, n)     for (int i=a;i<n;++i)
     31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
     32 #define clr                clear
     33 #define pb                 push_back
     34 #define mp                 make_pair
     35 #define fir                first
     36 #define sec                second
     37 #define all(x)             (x).begin(),(x).end()
     38 #define SZ(x)             ((int)(x).size())
     39 #define lson            l, mid, rt<<1
     40 #define rson            mid+1, r, rt<<1|1
     41 
     42 const double eps = 1e-8;
     43 const int maxn = 60;
     44 int N, M, K;
     45 double cx[maxn], cy[maxn];
     46 double rx[maxn], ry[maxn];
     47 bool visit[maxn];
     48 
     49 typedef struct {
     50     static const int maxc = 65;
     51     static const int maxr = 65;
     52     static const int maxn = maxr * maxc;
     53 
     54     int n, sz;
     55     int S[maxc];
     56 
     57     int row[maxn], col[maxn];
     58     int L[maxn], R[maxn], U[maxn], D[maxn];
     59     
     60     int ansd;
     61     
     62     void init(int n_) {
     63         n = n_;
     64         
     65         rep(i, 0, n+1) {
     66             L[i] = i - 1;
     67             R[i] = i + 1;
     68             U[i] = i;
     69             D[i] = i;
     70             col[i] = i;
     71         }
     72         
     73         L[0] = n;
     74         R[n] = 0;
     75         
     76         ansd = INT_MAX;
     77         sz = n+1;
     78         memset(S, 0, sizeof(S));
     79     }
     80     
     81     void addRow(int r, vi columns) {
     82         int first = sz;
     83         int size = SZ(columns);
     84         
     85         rep(i, 0, size) {
     86             int c = columns[i];
     87             
     88             L[sz] = sz - 1;
     89             R[sz] = sz + 1;
     90             
     91             D[sz] = c;
     92             U[sz] = U[c];
     93             D[U[c]] = sz;
     94             U[c] = sz;
     95             
     96             row[sz] = r;
     97             col[sz] = c;
     98             
     99             ++S[c];
    100             ++sz;
    101         }
    102         
    103         L[first] = sz - 1;
    104         R[sz - 1] = first;
    105     }
    106     
    107     void remove(int c) {
    108         for (int i=D[c]; i!=c; i=D[i]) {
    109             L[R[i]] = L[i];
    110             R[L[i]] = R[i];
    111             --S[col[i]];
    112         }
    113     }
    114     
    115     void restore(int c) {
    116         for (int i=D[c]; i!=c; i=D[i]) {
    117             L[R[i]] = i;
    118             R[L[i]] = i;
    119             ++S[col[i]];
    120         }
    121     }
    122     
    123     int H() {
    124         int ret = 0;
    125         
    126         memset(visit, false, sizeof(visit));
    127         for (int i=R[0]; i!=0; i=R[i]) {
    128             if (visit[col[i]])
    129                 continue;
    130             ++ret;
    131             visit[col[i]] = true;
    132             for (int j=D[i]; j!=i; j=D[j]) {
    133                 for (int k=R[j]; k!=j; k=R[k]) {
    134                     visit[col[k]] = true;
    135                 }
    136             }
    137         }
    138         
    139         return ret;
    140     }
    141     
    142     void dfs(int d) {
    143         int delta = H();
    144         
    145         if (delta+d>=ansd || d+delta>K)
    146             return ;
    147         
    148         if (R[0] == 0) {
    149             ansd = min(ansd, d);
    150             return ;
    151         }
    152         
    153         int c = R[0];
    154         for (int i=R[0]; i!=0; i=R[i]) {
    155             if (S[i] < S[c])
    156                 c = i;
    157         }
    158         
    159         for (int i=D[c]; i!=c; i=D[i]) {
    160             remove(i);
    161             for (int j=R[i]; j!=i; j=R[j]) {
    162                 remove(j);
    163             }
    164             dfs(d + 1);
    165             if (ansd <= K)
    166                 return ;
    167             for (int j=L[i]; j!=i; j=L[j]) {
    168                 restore(j);
    169             }
    170             restore(i);
    171         }
    172     }
    173     
    174 } DLX;
    175 
    176 DLX solver;
    177 
    178 double Length(int j, int i) {
    179     return sqrt((cx[i]-rx[j])*(cx[i]-rx[j]) + (cy[i]-ry[j])*(cy[i]-ry[j]));
    180 }
    181 
    182 bool judge(double bound) {
    183     memset(visit, false, sizeof(visit));
    184     solver.init(N);
    185     
    186     rep(i, 1, M+1) {
    187         vi columns;
    188         int cnt = 0;
    189         rep(j, 1, N+1) {
    190             if (Length(i, j) <= bound) {
    191                 columns.pb(j);
    192                 visit[j] = true;
    193                 ++cnt;
    194             }
    195         }
    196 
    197         if (SZ(columns) > 0) {
    198             solver.addRow(i, columns);
    199         }
    200     }
    201     
    202     rep(j, 1, N+1) {
    203         if (!visit[j]) {
    204     #ifndef ONLINE_JUDGE
    205     // printf("ansd = %d
    ", solver.ansd);
    206     #endif
    207             return false;
    208         }
    209     }
    210     
    211     solver.dfs(0);
    212     
    213     #ifndef ONLINE_JUDGE
    214     // printf("ansd = %d
    ", solver.ansd);
    215     #endif
    216     return solver.ansd<=K;
    217 }
    218 
    219 int main() {
    220     ios::sync_with_stdio(false);
    221     #ifndef ONLINE_JUDGE
    222         freopen("data.in", "r", stdin);
    223         freopen("data.out", "w", stdout);
    224     #endif
    225 
    226     double ans;
    227     double l, r, mid;
    228     int t;
    229     
    230     scanf("%d", &t);
    231     while (t--) {
    232         scanf("%d %d %d", &N, &M, &K);
    233         rep(i, 1, N+1)
    234             scanf("%lf %lf", &cx[i], &cy[i]);
    235         rep(i, 1, M+1)
    236             scanf("%lf %lf", &rx[i], &ry[i]);
    237         l = 0;
    238         r = ans = 2000.0;
    239         while (r >= l) {
    240             mid = (r + l) / 2.0;
    241             if (judge(mid)) {
    242                 ans = min(ans, mid);
    243                 r = mid - eps;
    244             } else {
    245                 l = mid + eps;
    246             }
    247         }
    248         printf("%.06lf
    ", ans);
    249     }
    250 
    251     #ifndef ONLINE_JUDGE
    252         printf("time = %d.
    ", (int)clock());
    253     #endif
    254 
    255     return 0;
    256 }
  • 相关阅读:
    数据处理之PostgreSQL过程语言学习
    Thinkphp中的assign() 和 display()
    JS截取与分割字符串常用技巧总结
    三种JS截取字符串方法
    十大经典排序算法的JS版
    js时间与毫秒互相转换
    javascript--清除表单缓存
    JS join()和split()方法、reverse() 方法、sort()方法
    JS数组去重的几种常见方法
    CSS样式大全
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4976400.html
Copyright © 2020-2023  润新知