解法
对于每个气球(x,y)
,如果它可以被拿到,那么需要有塔,其塔尖(p,h)
在阴影部分:
分析可知需要满足:
h
≥
∣
p
−
x
∣
+
y
h\ge |p-x|+y
h≥∣p−x∣+y
,即
x
+
y
≥
p
+
h
x+y\ge p+h
x+y≥p+h且
y
−
x
≥
h
−
p
y-x\ge h-p
y−x≥h−p
把每个点从(x,y)
进行坐标变换成(x+y,y-x)
所以对于每个气球,只要找有没有横纵坐标都比它大的塔即可。
#include <stdio.h>
#include <string>
#include <iostream>
#include <memory.h>
#include <stdlib.h>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#define MAXN 100010
#define NINF 0x80000000
using namespace std;
typedef long long lld;
struct POINT {
int x,y;
POINT(int a,int b):x(a),y(b) {}
bool operator < (const POINT &b) const {
if (x==b.x) return y<b.y;
return x<b.x;
}
};
vector<POINT> tower,ball;
int solve() {
int ans = 0;
int j = tower.size()-1;
int maxval = NINF;
for(int i = ball.size()-1;i>=0;i--) {
POINT &p = ball[i];
for(;j>=0 && tower[j].x>=p.x;j--) {
maxval = max(maxval,tower[j].y);
}
if(maxval>=p.y) ans++;
}
return ans;
}
int main() {
int t;
scanf("%d",&t);
for(auto round=1;round<=t;++round) {
tower.clear();
ball.clear();
int n,k;
scanf("%d%d",&n,&k);
int x1,x2,y1,y2;
lld a1,b1,c1,m1,a2,b2,c2,m2;
scanf("%d%d%lld%lld%lld%lld",&x1,&x2,&a1,&b1,&c1,&m1);
scanf("%d%d%lld%lld%lld%lld",&y1,&y2,&a2,&b2,&c2,&m2);
tower.emplace_back(POINT(x1+y1,y1-x1));
tower.emplace_back(POINT(x2+y2,y2-x2));
for(int i=3;i<=n;++i) {
int tmp1 = x2, tmp2 = y2;
x2 = (a1*x2+b1*x1+c1)%m1+1;
y2 = (a2*y2+b2*y1+c2)%m2+1;
tower.emplace_back(POINT(x2+y2,y2-x2));
x1 = tmp1;
y1 = tmp2;
}
scanf("%d%d%lld%lld%lld%lld",&x1,&x2,&a1,&b1,&c1,&m1);
scanf("%d%d%lld%lld%lld%lld",&y1,&y2,&a2,&b2,&c2,&m2);
ball.emplace_back(POINT(x1+y1,y1-x1));
ball.emplace_back(POINT(x2+y2,y2-x2));
for(int i=3;i<=k;++i) {
int tmp1 = x2, tmp2 = y2;
x2 = (a1*x2+b1*x1+c1)%m1+1;
y2 = (a2*y2+b2*y1+c2)%m2+1;
ball.emplace_back(POINT(x2+y2,y2-x2));
x1 = tmp1;
y1 = tmp2;
}
sort(tower.begin(),tower.end());
sort(ball.begin(),ball.end());
printf("Case #%d: %d\n",round,solve());
}
}