LA 4253 Archery(二分+枚举)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2254

  这题是一道没有人通过的题。

  对于这题我想到一个二分的方法,可是无论怎么调精度都通过不了。个人感觉,是精度要求的比较高,所以不容易做出这样的题。

分享一下代码:

View Code
  1 /***************** Written By Lyon, From SCAU, Beta 1.4.0 **************/
  2 /**************** headers && constants && definitions ****************/
  3 #pragma comment(linker, "/STACK:102400000,102400000")
  4 
  5 #include 
  6 #include 
  7 #include 
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include 
 14 #include <string>
 15 #include 
 16 #include 
 17 #include 
 18 #include 
 19 #include <set>
 20 #include 
 21 
 22 using namespace std;
 23 
 24 #define PB push_back
 25 #define FI first
 26 #define SE second
 27 #define MPR make_pair
 28 #define REP(i, n) for (int i = 0; i < (n); i++)
 29 #define REP_1(i, n) for (int i = 1; i <= (n); i++)
 30 #define INC(i, a, b) for (int i = (a); i <= (b); i++)
 31 #define DEC(i, a, b) for (int i = (a); i >= (b); i--)
 32 #define _clr(x) memset(x, 0, sizeof(x))
 33 #define _rst(x) memset(x, -1, sizeof(x))
 34 #define SZ(x) ((int) x.size())
 35 #define PRIQ priority_queue
 36 #define MSET multiset
 37 #define ITOR iterator
 38 #define RITOR reverse_iterator
 39 #define ALL(x) x.begin(), x.end()
 40 
 41 typedef long long LL;
 42 typedef unsigned long long ULL;
 43 typedef pair<int, int> PII;
 44 typedef pair<double, double> PDBDB;
 45 typedef pair<double, int> PDBI;
 46 typedef pair<int, double> PIDB;
 47 typedef vector VPIDB;
 48 typedef vector VPDBDB;
 49 typedef vector VPDBI;
 50 typedef pairint> PIII;
 51 typedef pairstring> PIIS;
 52 typedef vector<int> VI;
 53 typedef vector VL;
 54 typedef vector VPII;
 55 typedef vector VPIII;
 56 typedef vector<double> VDBL;
 57 typedef vector<string> VSTR;
 58 typedef vector VVSTR;
 59 typedef vector VVI;
 60 typedef vector<char> VCH;
 61 typedef vector VVCH;
 62 typedef vector<bool> VBL;
 63 
 64 const int N = 5e3 + 100;
 65 const int M = 1 << 5;
 66 const int LEN = 105;
 67 const int hashMod = 1e6 + 5;
 68 const int inf = 0x55555555;
 69 const double eps = 1e-8;
 70 const LL linf = 0x5555555555555555ll;
 71 const double finf = 1e50;
 72 const double pi = acos(-1.0);
 73 const int mod = 1e9 + 7;
 74 
 75 template <class T> inline T sqr(T x) {
 76     return x * x;
 77 }
 78 /*********************************************************************/
 79 
 80 struct Target {
 81     double y, l, r;
 82     Target(double _y = 0.0, double _l = 0.0, double _r = 0.0) {
 83         y = _y, l = _l, r = _r;
 84         if (l > r) swap(l, r);
 85     }
 86 } ;
 87 vector rec;
 88 
 89 bool cmp(Target a, Target b) {
 90     return a.y < b.y;
 91 }
 92 
 93 int test(double x) {
 94     double L = (atan2(rec[0].y, rec[0].r - x) - eps), R = (atan2(rec[0].y, rec[0].l - x) + eps);
 95     REP_1(i, SZ(rec) - 1) {
 96         double tmpL = (atan2(rec[i].y, rec[i].r - x) - eps), tmpR = (atan2(rec[i].y, rec[i].l - x) + eps);
 97         if (R < tmpL) return 1;
 98         if (tmpR < L) return -1;
 99         L = max(tmpL, L), R = min(tmpR, R);
100     }
101     return 0;
102 }
103 
104 bool bs(double w) {
105     double l = -1e-7, r = w + 1e-7, m;
106     while (r - l > eps) {
107         m = (l + r) / 2.0;
108         int res = test(m);
109         if (res == 0) {
110 //            cout << m << endl;
111             return true;
112         } else if (res == -1) r = m;
113         else l = m;
114     }
115     return false;
116 }
117 
118 void input(int n) {
119     double d, l, r;
120     rec.clear();
121     REP(i, n) {
122         scanf("%lf%lf%lf", &d, &l, &r);
123         rec.PB(Target(d, l, r));
124     }
125     sort(ALL(rec), cmp);
126 }
127 
128 int main() {
129 //    freopen("in", "r", stdin);
130     int T, n;
131     double w;
132     while (~scanf("%d", &T)) {
133         while (T--) {
134             scanf("%lf%d", &w, &n);
135             input(n);
136             bs(w) ? puts("YES") : puts("NO");
137         }
138     }
139     return 0;
140 }

UPD:

  经过又一轮的调精度,代码变成这样了,数据强度也高了不少,然而还是过不了。

View Code
  1 /***************** Written By Lyon, From SCAU, Beta 1.4.0 **************/
  2 /**************** headers && constants && definitions ****************/
  3 #pragma comment(linker, "/STACK:102400000,102400000")
  4 
  5 #include 
  6 #include 
  7 #include 
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include 
 14 #include <string>
 15 #include 
 16 #include 
 17 #include 
 18 #include 
 19 #include <set>
 20 #include 
 21 
 22 using namespace std;
 23 
 24 #define PB push_back
 25 #define FI first
 26 #define SE second
 27 #define MPR make_pair
 28 #define REP(i, n) for (int i = 0; i < (n); i++)
 29 #define REP_1(i, n) for (int i = 1; i <= (n); i++)
 30 #define INC(i, a, b) for (int i = (a); i <= (b); i++)
 31 #define DEC(i, a, b) for (int i = (a); i >= (b); i--)
 32 #define _clr(x) memset(x, 0, sizeof(x))
 33 #define _rst(x) memset(x, -1, sizeof(x))
 34 #define SZ(x) ((int) x.size())
 35 #define PRIQ priority_queue
 36 #define MSET multiset
 37 #define ITOR iterator
 38 #define RITOR reverse_iterator
 39 #define ALL(x) x.begin(), x.end()
 40 
 41 typedef long long LL;
 42 typedef unsigned long long ULL;
 43 typedef pair<int, int> PII;
 44 typedef pair<double, double> PDBDB;
 45 typedef pair<double, int> PDBI;
 46 typedef pair<int, double> PIDB;
 47 typedef vector VPIDB;
 48 typedef vector VPDBDB;
 49 typedef vector VPDBI;
 50 typedef pairint> PIII;
 51 typedef pairstring> PIIS;
 52 typedef vector<int> VI;
 53 typedef vector VL;
 54 typedef vector VPII;
 55 typedef vector VPIII;
 56 typedef vector<double> VDBL;
 57 typedef vector<string> VSTR;
 58 typedef vector VVSTR;
 59 typedef vector VVI;
 60 typedef vector<char> VCH;
 61 typedef vector VVCH;
 62 typedef vector<bool> VBL;
 63 
 64 const int N = 5e3 + 100;
 65 const int M = 1 << 5;
 66 const int LEN = 105;
 67 const int hashMod = 1e6 + 5;
 68 const int inf = 0x55555555;
 69 const double eps = 1e-7;
 70 const LL linf = 0x5555555555555555ll;
 71 const double finf = 1e50;
 72 const double pi = acos(-1.0);
 73 const int mod = 1e9 + 7;
 74 
 75 template <class T> inline T sqr(T x) {
 76     return x * x;
 77 }
 78 /*********************************************************************/
 79 
 80 struct Target {
 81     double y, l, r;
 82     Target(double _y = 0.0, double _l = 0.0, double _r = 0.0) {
 83         y = _y, l = _l, r = _r;
 84         if (l > r) swap(l, r);
 85     }
 86 } ;
 87 vector rec;
 88 
 89 bool cmp(Target a, Target b) {
 90     return a.y < b.y;
 91 }
 92 
 93 int test(double x) {
 94     double L = (10 * atan2(rec[0].y, rec[0].r - x) - eps), R = (10 * atan2(rec[0].y, rec[0].l - x) + eps);
 95     REP_1(i, SZ(rec) - 1) {
 96         double tmpL = (10 * atan2(rec[i].y, rec[i].r - x) - eps), tmpR = (10 * atan2(rec[i].y, rec[i].l - x) + eps);
 97         if (R < tmpL) return 1;
 98         if (tmpR < L) return -1;
 99         L = max(tmpL, L), R = min(tmpR, R);
100     }
101     return 0;
102 }
103 
104 bool bs(double w) {
105     double l = 0, r = w, m;
106     while (r - l > eps) {
107         m = (l + r) / 2.0;
108         int res = test(m);
109         if (res == 0) {
110 //            cout << m << endl;
111             return true;
112         } else if (res == -1) r = m;
113         else l = m;
114     }
115     return false;
116 }
117 
118 void input(int n) {
119     double d, l, r;
120     rec.clear();
121     REP(i, n) {
122         scanf("%lf%lf%lf", &d, &l, &r);
123         rec.PB(Target(d, l, r));
124     }
125     sort(ALL(rec), cmp);
126 }
127 
128 int main() {
129 //    freopen("in", "r", stdin);
130     int T, n;
131     double w;
132     while (~scanf("%d", &T)) {
133         while (T--) {
134             scanf("%lf%d", &w, &n);
135             input(n);
136             bs(w) ? puts("YES") : puts("NO");
137         }
138     }
139     return 0;
140 }

自己搞的一些测试数据:

View Code
 1 10
 2 15
 3 4
 4 10 2 7
 5 7 5 12
 6 2 7 12
 7 4 9 13
 8 6
 9 3
10 2 1 3
11 4 0 2
12 5 4 6
13 10
14 4
15 8 2 5
16 4 2 5
17 6 5 8
18 2 5 8
19 
20 10000000
21 5
22 2000000 5000000 10000000
23 4000000 1 5000000
24 6000000 4999999 10000000
25 8000000 1 4999999
26 10000000 4999998 10000000
27 
28 10000000
29 2
30 3 0 4
31 7499997 9999996 10000000
32 10000000
33 2
34 3 0 4
35 7499999 9999999 10000000
36 
37 10000000
38 2
39 5 0 12
40 4166666 9999999 10000000
41 10000000
42 2
43 5 0 12
44 4166666 9999998 10000000
45 
46 10000000
47 2
48 5 0 12
49 4166665 9999997 10000000
50 10000000
51 2
52 5 0 12
53 4166665 9999996 10000000

UPD:

View Code
  1 /***************** Written By Lyon, From SCAU, Beta 1.4.0 **************/
  2 /**************** headers && constants && definitions ****************/
  3 #pragma comment(linker, "/STACK:102400000,102400000")
  4 
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <cstdlib>
  8 #include <cstring>
  9 #include <iomanip>
 10 #include <iostream>
 11 #include <algorithm>
 12 #include <numeric>
 13 #include <vector>
 14 #include <string>
 15 #include <bitset>
 16 #include <queue>
 17 #include <ctime>
 18 #include <stack>
 19 #include <set>
 20 #include <map>
 21 
 22 using namespace std;
 23 
 24 #define PB push_back
 25 #define FI first
 26 #define SE second
 27 #define MPR make_pair
 28 #define REP(i, n) for (int i = 0; i < (n); i++)
 29 #define REP_1(i, n) for (int i = 1; i <= (n); i++)
 30 #define INC(i, a, b) for (int i = (a); i <= (b); i++)
 31 #define DEC(i, a, b) for (int i = (a); i >= (b); i--)
 32 #define _clr(x) memset(x, 0, sizeof(x))
 33 #define _rst(x) memset(x, -1, sizeof(x))
 34 #define SZ(x) ((int) x.size())
 35 #define PRIQ priority_queue
 36 #define MSET multiset
 37 #define ITOR iterator
 38 #define RITOR reverse_iterator
 39 #define ALL(x) x.begin(), x.end()
 40 
 41 typedef long long LL;
 42 typedef unsigned long long ULL;
 43 typedef pair<int, int> PII;
 44 typedef pair<double, double> PDBDB;
 45 typedef pair<double, int> PDBI;
 46 typedef pair<int, double> PIDB;
 47 typedef vector<PIDB> VPIDB;
 48 typedef vector<PDBDB> VPDBDB;
 49 typedef vector<PDBI> VPDBI;
 50 typedef pair<PII, int> PIII;
 51 typedef pair<PII, string> PIIS;
 52 typedef vector<int> VI;
 53 typedef vector<LL> VL;
 54 typedef vector<PII> VPII;
 55 typedef vector<PIII> VPIII;
 56 typedef vector<double> VDBL;
 57 typedef vector<string> VSTR;
 58 typedef vector<VSTR> VVSTR;
 59 typedef vector<VI> VVI;
 60 typedef vector<char> VCH;
 61 typedef vector<VCH> VVCH;
 62 typedef vector<bool> VBL;
 63 typedef long double LDB;
 64 
 65 const int N = 5e3 + 100;
 66 const int M = 1 << 5;
 67 const int LEN = 105;
 68 const int hashMod = 1e6 + 5;
 69 const int inf = 0x55555555;
 70 const double eps = 1e-8;
 71 const LDB leps = 1e-10;
 72 const LL linf = 0x5555555555555555ll;
 73 const double finf = 1e50;
 74 const double pi = acos(-1.0);
 75 const int mod = 1e9 + 7;
 76 
 77 template > inline T sqr(T x) {
 78     return x * x;
 79 }
 80 /*********************************************************************/
 81 
 82 struct Target {
 83     LDB y, l, r;
 84     Target(LDB _y = 0.0, LDB _l = 0.0, LDB _r = 0.0) {
 85         y = _y, l = _l, r = _r;
 86         if (l > r) swap(l, r);
 87     }
 88 } ;
 89 vector<Target> rec;
 90 
 91 bool cmp(Target a, Target b) {
 92     return a.y < b.y;
 93 }
 94 
 95 int test(LDB x) {
 96     LDB L = (rec[0].l - x - eps) / rec[0].y, R = (rec[0].r - x + eps) / rec[0].y;
 97     REP_1(i, SZ(rec) - 1) {
 98         LDB tmpL = (rec[i].l - x - eps) / rec[i].y, tmpR = (rec[i].r - x + eps) / rec[i].y;
 99         if (R < tmpL) return 1;
100         if (tmpR < L) return -1;
101         L = max(tmpL, L), R = min(tmpR, R);
102     }
103     return 0;
104 }
105 
106 bool bs(LDB w) {
107     LDB l = 0, r = w, m;
108     while (r - l > eps) {
109         m = (l + r) / 2.0;
110         int res = test(m);
111         if (res == 0) {
112 //            cout << m << endl;
113             return true;
114         } else if (res == -1) l = m;
115         else r = m;
116     }
117     return false;
118 }
119 
120 void input(int n) {
121     LDB d, l, r;
122     rec.clear();
123     REP(i, n) {
124         cin >> d >> l >> r;
125         rec.PB(Target(d, l, r));
126     }
127     sort(ALL(rec), cmp);
128 }
129 
130 int main() {
131 //    freopen("in", "r", stdin);
132     int T, n;
133     LDB w;
134     while (~scanf("%d", &T)) {
135         while (T--) {
136             cin >> w >> n;
137             input(n);
138             bs(w) ? puts("YES") : puts("NO");
139         }
140     }
141     return 0;
142 }

  上面这个代码过了更多精度要求更高的数据,可惜还是WA了。

 

UPD:

  协助管理员把数据改正过来了,然后我的代码AC了!~~~

 

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/archive/2013/03/04/LA_4253_Lyon.html

你可能感兴趣的:(LA 4253 Archery(二分+枚举))