题意: 给定一个x的区间(x1, x2),然后给定n条直线,判断这些直线的交点有没有在区间(x1, x2)内;
>> face <<
strategy: 仔细想想 就会发现 针对每两个在区间内相交的直线 都有
( y 1 ∣ x = x 1 − y 2 ∣ x = x 1 ) ( y 1 ∣ x = x 2 − y 2 ∣ x = x 2 ) < 0 (y_1\mid _{x = x_1} - y_2 \mid _{x = x_1}) (y_1\mid _{x = x_2} - y_2 \mid _{x = x_2}) < 0 (y1∣x=x1−y2∣x=x1)(y1∣x=x2−y2∣x=x2)<0
于是很自然就会想到 先对y排序 然后 遍历前面的每一条直线,判断上述条件
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define oo INT_MAX
#define ll long long
#define db double
#define all(a) a.begin(), a.end()
#define met(a, b) memset(a, b, sizeof(a))
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define lowbit(x) x &(-x)
#define pi acos(-1.0)
using namespace std;
using namespace __gnu_pbds;
const int maxn = 100009;
// FILE *o = freopen("C:\\Users\\Jason.Z\\Desktop\\in.txt", "r", stdin);
// assert(o != NULL);
// FILE *e = freopen("C:\\Users\\Jason.Z\\Desktop\\y[i].txt", "w", stdout);
// assert(e != NULL);
vector<pair<ll, ll> > q(maxn);
int main(){
ios::sync_with_stdio(0);
int n;
cin >> n;
int x1, x2;
cin >> x1 >> x2;
_rep(i, 1, n){
int k, b;
cin >> k >> b;
// q.push_back({k * x1 + b, k * x2 + b});
q[i].first = k * x1 + b, q[i].second = k * x2 + b;
}
sort(q.begin() + 1, q.begin() + n + 1, greater<pair<ll, ll> > ());
bool yes = 0;
_rep(i, 1, n){
_rep(j, i + 1, n){
if(q[j].second > q[i].second)yes = 1;
}
if(yes)break;
}
puts(yes ? "YES" : "NO");
}
然而 TLE
strategy2 : 其实优化很简单, 针对当前直线, 不用和前面的所有直线比较, 只用和上一条直线比较, 画个图就知道了
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define oo INT_MAX
#define ll long long
#define db double
#define all(a) a.begin(), a.end()
#define met(a, b) memset(a, b, sizeof(a))
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define lowbit(x) x &(-x)
#define pi acos(-1.0)
using namespace std;
using namespace __gnu_pbds;
const int maxn = 100009;
// FILE *o = freopen("C:\\Users\\Jason.Z\\Desktop\\in.txt", "r", stdin);
// assert(o != NULL);
// FILE *e = freopen("C:\\Users\\Jason.Z\\Desktop\\y[i].txt", "w", stdout);
// assert(e != NULL);
vector<pair<ll, ll> > q(maxn);
int main(){
ios::sync_with_stdio(0);
ll n;
cin >> n;
ll x1, x2;
cin >> x1 >> x2;
_rep(i, 1, n){
ll k, b;
cin >> k >> b;
// q.push_back({k * x1 + b, k * x2 + b});
q[i].first = k * x1 + b, q[i].second = k * x2 + b;
}
sort(q.begin() + 1, q.begin() + n + 1, greater<pair<ll, ll> > ());
bool yes = 0;
_rep(i, 2, n){
if(q[i].second > q[i - 1].second){
yes = 1;
break;
}
}
puts(yes ? "YES" : "NO");
}
注意 long long