2025杭电多校第七场 矩形框选、伤害冷却比 个人题解

伤害冷却比

#数学

题目

image

思路

a = K N a=\frac{K}{N} a=NK,则有 f ( x ) = x ( ⌊ a x ⌋ + 1 ) f(x)=x\left( \left\lfloor \frac{a}{x} \right\rfloor +1\right) f(x)=x(xa+1)
大致画出图像,可得下图
image

若要求区间 [ L , R ] [L,R] [L,R]上的最大值,则需要求出 f ( R ) f(R) f(R)与红线蓝线交点值之间的最大值

为了求出交点,联立两个方程:
x + a = x ( ⌊ a x ⌋ + 1 ) x + a = x ⌊ a x ⌋ + x a x = ⌊ a x ⌋ ∃   n ∈ Z   , a x = n ∴ x = a n , n = 1 , 2 , 3 , … \begin{align} x+a&=x\left( \left\lfloor \frac{a}{x} \right\rfloor +1 \right)\\ \\ x+a&=x\left\lfloor \frac{a}{x} \right\rfloor +x\\ \\ \frac{a}{x}&=\left\lfloor \frac{a}{x} \right\rfloor \\ \\ \exists \ n\in& Z\ , \frac{a}{x}=n\\ \\ \therefore x=\frac{a}{n}&,n=1,2,3,\dots \end{align} x+ax+axa nx=na=x(xa+1)=xxa+x=xaZ ,xa=n,n=1,2,3,
因此可以找到距离 R R R最近的 x 0 = a n x_{0}=\frac{a}{n} x0=na,其中 n = ⌈ a R ⌉ n=\left\lceil \frac{a}{R} \right\rceil n=Ra
再将此 x 0 x_{0} x0带入 y = x + a y=x+a y=x+a中,即可得到区间 [ L , R ] [L,R] [L,R]上交点的最大值啦

随后将 R R R带入 f ( x ) f(x) f(x)中,比较二者大小约分输出即可

代码实现

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
#include<string.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = (a); i <= (b); i ++)
#define per(i, a, b) for(ll i = (a); i >= (b); i --)
#define see(stl) for(auto&ele:stl)cout<<ele<<" "; cout<<'\n';
#define mid ((l+r)>>1)
#define double long double
#define int ll
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}

void eachT(){
    int K,N,A,B,C,D;cin>>K>>N>>A>>B>>C>>D;
    int n=ceil(1.0*(K*D)/(N*C));
    double x=1.0*K/(N*n);
    double ans1=x+(1.0*K/N);
    double L=1.0*A/B;
    if(L>x)ans1=-1;
    double ans2=1.0*C/D*(K*D/(N*C)+1);
    if(ans1>=ans2){
        int g=gcd(K*(1+n),N*n);
        cout<<K*(1+n)/g<<"/"<<N*n/g<<'\n';
    }else{
        int g=gcd(C*(K*D/(N*C)+1),D);
        cout<<C*(K*D/(N*C)+1)/g<<"/"<<D/g<<'\n';
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)eachT();
}

矩形框选

#线段树 #扫描线 #二维数点 #差分 #区间最值

题目

image

思路

先讲一讲二维数点的一个常用技巧:
image

假设红框是当前的矩形,长宽固定; a , b , c a,b,c a,b,c为平面内的三个点,现在考虑能够将三个点都选中的区域在哪

过三个点都向左下作与红框大小一致的矩形,三个矩形的交集(蓝色区域)记为 M M M,点 m ∈ M m\in M mM,则以 m m m点为左下角的红框都必定可以框住 a , b , c a,b,c a,b,c三个点

因此数点问题转换为了染色问题!

在确定了一个矩形选取的形状之后,可以让每个平面内的点都给其左下角的矩形区域进行 + 1 +1 +1操作,最后整个平面内哪个点的值最大,其值就是能够框住的点的数量的最大值

如何快速实现二维染色呢 ?
可以通过差分标记+线段树维护扫描线来实现:

  • 线段树维护扫描线上的区间最大值,同时需要区间修改功能
  • 假设扫描线为平行于 y y y轴的直线,从左到右扫描
  • 遇到一个矩形的入边,在扫描线上区间加
  • 遇到一个矩形的出边,在扫描线上区间减
  • 每处理完一个横坐标,就调用 s u m [ r o o t ] sum[root] sum[root]获取扫描线的线段树根节点所维护的最大值,更新全局最大值

但刚刚说的全都是建立在矩形形状确定的条件下
因此我们还需要解决矩形形状的问题:

n n n最大为 1 e 5 1e 5 1e5,那么可以枚举矩形长 x x x,则宽 y = ⌊ k x ⌋ y=\left\lfloor \frac{k}{x} \right\rfloor y=xk,在保证矩形面积尽可能大的同时不遗漏情况
x × x ≤ k x\times x\leq k x×xk作为循环条件,则 x ≤ k x\leq \sqrt{ k } xk k k k n n n为同一量级,所以枚举次数为 1 e 2.5 = 1 e 2 × 10 = 3 e 2 1e 2.5=1e 2\times \sqrt{ 10 }=3e 2 1e2.5=1e2×10 =3e2
对于一个确定的矩形形状,扫描线+线段树的复杂度为 n log ⁡ n = 1 e 6 n\log n=1e 6 nlogn=1e6 T T T组数为 1 e 2 1e 2 1e2,总复杂度为 3 e 10 3e 10 3e10,在 8 s 8s 8s的限制下可以通过

代码实现

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
#include<string.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, a, b) for(int i = (a); i <= (b); i ++)
#define per(i, a, b) for(ll i = (a); i >= (b); i --)
#define see(stl) for(auto&ele:stl)cout<<ele<<" "; cout<<'\n';
#define ls p<<1
#define rs p<<1|1
#define mid ((l+r)>>1)
const int N = 1e4 + 5;
// int a[N][N];

struct no {
    int x, y, w;
};
struct q {
    int down, up, val;
};
struct L {
    vector<q>query;
}line[N];

int ma[N << 2], tag[N << 2];

void pushup(int p) {
    ma[p] = max(ma[ls], ma[rs]);
}

void pushdown(int p) {
    if (!tag[p]) return;
    tag[ls] += tag[p]; ma[ls] += tag[p];
    tag[rs] += tag[p]; ma[rs] += tag[p];
    tag[p] = 0;
}

void modify(int p, int l, int r, int x, int y, int val) {
    if (x <= l && r <= y) { ma[p] += val; tag[p] += val; return; }
    pushdown(p);
    if (x <= mid)modify(ls, l, mid, x, y, val);
    if (y > mid)modify(rs, mid + 1, r, x, y, val);
    pushup(p);
}

void build(int p, int l, int r) {
    ma[p] = tag[p] = 0;
    if (l == r)return;
    build(ls, l, mid), build(rs, mid + 1, r);
}

void init(int X, int Y) {
    rep(i, 0, X + 1) {
        line[i].query.clear();
    }
    build(1, 1, Y);
}

void eachT() {
    int n, k; cin >> n >> k;
    vector<no>node(n + 1);
    int X = 0, Y = 0;
    rep(i, 1, n) {
        int x, y, v; cin >> x >> y >> v;
        node[i] = { x,y,v };
        X = max(X, x), Y = max(Y, y);
    }
    int ans = 0;
    for (int p = 1; p * p <= k; p++) {
        int y = k / p, x = p;
        rep(t, 1, 2) {
            swap(x, y);
            init(X, Y);
            rep(i, 1, n) {
                int x0 = node[i].x, y0 = node[i].y, w = node[i].w;
                line[max(x0 - x + 1, 1ll)].query.push_back({ max(y0 - y + 1,1ll),y0,w });
                line[x0 + 1].query.push_back({ max(y0 - y + 1,1ll),y0,-w });
            }
            rep(i, 1, X) {
                for (auto& ele : line[i].query) {
                    int down = ele.down, up = ele.up, val = ele.val;
                    modify(1, 1, Y, down, up, val);
                }
                int cmp = ma[1];
                ans = max(ans, cmp);
            }
        }
    }
    cout << ans << '\n';
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)eachT();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值