CodeForces 1409E : Two Platforms 二分

传送门

题目描述

有两块长k的板子,可以在任意整数座标水平放置,问最多有多少小球的投影在板子上

分析

最后的答案应该只和x坐标有关,和y无关,应该是计算投影
我们可以去把x坐标从小到大进行排序,然后枚举一个坐标,可以通过二分计算出他另一个点的位置,如果另一端在x1处,那么另一个位置的起点在何处最优呢,显然x1 + 1 ,x1 + 2 ,x1 + 3… 等都是可以的,所以我们可以预处理出来一个max数组,一块板的位置通过枚举,另一块直接预处理出来

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
    
    return (b>0)?gcd(b,a%b):a;}
int a[N],b[N];
int n,k;

int main(){
    
    
    int T;
    read(T);
    while(T--){
    
    
        read(n),read(k);
        for(int i = 1;i <= n;i++) read(a[i]);
        for(int i = 1;i <= n;i++) read(b[i]);
        memset(b,0,sizeof b);
        sort(a + 1,a + 1 + n);
        for(int i = n;i;i--){
    
    
            int x = a[i] + k;
            int p = upper_bound(a + 1,a + 1 + n,x) - a;
            b[i] = max(b[i + 1],p - i);
        }
        int res = 0;
        for(int i = 1;i <= n;i++){
    
    
            int x = a[i] + k;
            int p = upper_bound(a + 1,a + 1 + n,x) - a;
            res = max(res,p + b[p] - i);
        }
        di(res);
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/



猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/113883496