美丽方格

题目描述

你手里拿着很多字母牌,在一个方格棋盘上下棋,棋盘的中心是坐标(0,0),你把字母牌在棋盘上摆放,你规定一个美丽的方格是指中心也是坐标(0,0),且方格中不存在相同字母的牌,给你一个摆放完的棋盘,问是美丽方格中最多多少张牌?

思路

  • 这题第一反应是枚举正方形边长,为了方便,枚举的数是正方形边长一半(假设这个数是k),这样判断一个点是否在正方形内时,就可以写abs(x)<=k && abs(y)<=k
  • 但是枚举会T,我们就可以用二分答案代替枚举,就是正解

AC啦

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct nd{
    int x,y;
    char ch;
    nd(int _x,int _y,char _ch){
        x=_x;
        y=_y;
        ch=_ch;
    }
    nd(){
        x=y=0;
        ch='\0';
    }
};
nd a[10000005];
int n;
int cnt[26]={};
bool check(int x){
    memset(cnt,0,sizeof cnt);
    for(int i=1;i<=n;i++)
        if(abs(a[i].x)<=x && abs(a[i].y)<=x){
            if(cnt[a[i].ch-'a']) return false;
            cnt[a[i].ch-'a']++;
        }
    return true;
}
int Find(){
    int l=0,r=10000000;
    int ans=-1;
    while(l<=r){
        int mid=(l+r)/2;
        if(check(mid)){
            l=mid+1;
           	ans=max(ans,mid);
        }else
            r=mid-1;
    }
    return ans;
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,b;
        char ch;
        cin>>x>>b;
        cin>>ch;
        while(ch==' ')
            cin>>ch;
        a[i]=nd(x,b,ch);
    }
    int sq=Find();
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(abs(a[i].x)<=sq && abs(a[i].y)<=sq)
            cnt++;
    cout<<cnt;
    return 0;
}

posted on 2024-07-05 18:01  可爱楷玩算法  阅读(67)  评论(0)    收藏  举报

导航