sweep line 扫描线略知略会

sweep line 扫描线略知略会

做了一些有相同思路的题,先留个坑。

思路

🌎 2019/8/16 21:48:22
我的理解是 对一些事件根据一定顺序排序然后for

​ ——辰巨如是说

待阅读

https://codeforces.com/blog/entry/20377

https://blog.csdn.net/xianpingping/article/details/83032798

https://blog.csdn.net/sslz_fsy/article/details/82902644

例题与例程

ZJNU 1426

(弊校OJ,可能打不开)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;

int n,cur,ans;
map<int, int> cnt;

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> n;
for (int a, b, i = 1; i <= n; i++) {
cin >> a >> b;
cnt[a - 1]++;
cnt[b]--;
}

for (auto pi:cnt) {
ans = max(ans, cur);
cur += pi.second;
}
cout << ans << endl;

return 0;
}

NCOJ 889D Symmetrical Painting

2019牛客暑期多校训练营(第九场)J

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>

#define get0(x) get<0>(x)
#define get1(x) get<1>(x)
#define _debug(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 59;
const ll MOD = 1e9 + 7;

ll n, ans;
vector<tuple<int, int> > pos;


int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);


cin >> n;

pos.emplace_back(0, 0);
for (ll l, r, i = 1; i <= n; i++) {
cin >> l >> r;
pos.emplace_back(l + l, 1);
pos.emplace_back(l + r, -2);
pos.emplace_back(r + r, 1);
}
sort(pos.begin(), pos.end());

ll cur = 0;
ll del = 0;
ll las = 0;

for (auto &p:pos) {
cur += (get0(p) - las) * del;
del += get1(p);
las = get0(p);
ans = max(ans, cur);
}
cout << ans << endl;
return 0;
}

HDU 6627

HDU 2019 Multi-University Training Contest 5

这题main函数里其实不复杂,但是分数可真是搞死我了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <bits/stdc++.h>

#define get0(x) get<0>(x)
#define get1(x) get<1>(x)
#define get2(x) get<2>(x)
#define _debug(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 59;
const ll MOD = 1e9 + 7;
const ll INF = 1001;


typedef struct frac {
ll a, b;

frac() : a(0), b(1) {}

frac(ll _a) : a(_a), b(1) {}

frac(ll _a, ll _b) : a(_a), b(_b) {
if (a == 0) {
this->b = 1;
}
if (b < 0) {
this->a = -_a;
this->b = -_b;
}
ll g = __gcd(abs(a), abs(b));
this->a /= g;
this->b /= g;
}

frac operator+(const frac &rht) const {
return frac(a * rht.b + b * rht.a, b * rht.b);
}

frac operator-(const frac &rht) const {
return frac(a * rht.b - b * rht.a, b * rht.b);
}

frac operator*(const frac &rht) const {
return frac(a * rht.a, b * rht.b);
}

frac operator/(const frac &rht) const {
return frac(a * rht.b, b * rht.a);
}

bool operator<(const frac &rht) const {
return a * rht.b < b * rht.a;
}

bool operator<=(const frac &rht) const {
return a * rht.b <= b * rht.a;
}

bool operator>(const frac &rht) const {
return a * rht.b > b * rht.a;
}

bool operator>=(const frac &rht) const {
return a * rht.b >= b * rht.a;
}

bool operator==(const frac &rht) const {
return a * rht.b == b * rht.a;
}

friend ostream &operator<<(ostream &out, frac &T);

void operator()(ll _a, ll _b) {
*this = frac(_a, _b);
}
} fc;

ostream &operator<<(ostream &out, fc &T) {
out << T.a << "/" << T.b;
return out;
}

// y1<y2
fc calc1(fc x1, fc y1, fc x2, fc y2, fc c) {
fc dx = x2 - x1;
fc dy = y2 - y1;
fc dc = c - y1;
return x1 + dc * dx / dy;
}

// y1>y2
fc calc2(fc x1, fc y1, fc x2, fc y2, fc c) {
fc dx = x2 - x1;
fc dy = y1 - y2;
fc dc = c - y2;
return x2 - dc * dx / dy;
}

ll n, kase, c;
vector<tuple<fc, ll, ll> > pos;
vector<fc> ans;


int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> kase;
while (kase--) {

ans.clear();
pos.clear();
cin >> n >> c;

fc prek(0);
fc preb(0);
fc lasx(-INF);
fc lasy;
fc v(c);

//pos.emplace_back(-INF, 0, 0);
for (ll _a, _b, i = 1; i <= n; i++) {
cin >> _a >> _b;
prek = prek + fc(-_a);
preb = preb + fc(-_b);
pos.emplace_back(frac(-_b, _a), 2 * _a, 2 * _b);// +inf ,+k
}

pos.emplace_back(INF, 0, 0);
lasy = prek * lasx + preb;

sort(pos.begin(), pos.end());

bool infi = false;
for (auto &p:pos) {

fc x, y;
ll k, b;
tie(x, k, b) = p;
y = (prek + k) * x + (preb + b);

if (lasy == v && y == v) {
ans.clear();
infi = true;
break;
}

if (lasy <= v && y > v) {
ans.push_back(calc1(lasx, lasy, x, y, v));
} else if (lasy >= v && y < v) {
ans.push_back(calc2(lasx, lasy, x, y, v));
}

lasy = y;
lasx = x;
prek = prek + k;
preb = preb + b;
}

if (infi) {
cout << "-1\n";
} else if (ans.empty()) {
cout << "0\n";
} else {
cout << ans.size() << " ";
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " \n"[i == ans.size() - 1];
}
ans.clear();
pos.clear();
}
return 0;
}

/*

4
2 3
1 2
1 -1
3 3
2 1
2 2
2 3
2 1
3 5
4 -1
3 2
1 -1
1 -2
1 -3


*/
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×