当前位置: 首页 > news >正文

小奇的糖果(candy)


【题目背景】
小奇不小心让糖果散落到了地上,它对着满地的彩色糖果胡思乱想。
【问题描述】
有 N 个彩色糖果在平面上。 小奇想在平面上取一条水平的线段,并拾起它上方或
下方的所有糖果。求出最多能够拾起多少糖果,使得获得的糖果并不包含所有的
颜色。
【输入格式】
包含多组测试数据,第一行输入一个正整数 T 表示测试数据组数。
接下来 T 组测试数据,对于每组测试数据,第一行输入两个正整数 N、K,分别表
示点数和颜色数。
接下来 N 行,每行描述一个点,前两个数 x, y (|x|, |y| ≤ 2^30 - 1) 描述点
的位置,最后一个数 z (1 ≤ z ≤ k) 描述点的颜色。
【输出格式】
对于每组数据在一行内输出一个非负整数 ans,表示答案。
【样例输入】
1
10 3
1 2 3
2 1 1
2 4 2
3 5 3
4 4 2
5 1 2
6 3 1
6 7 1
7 2 3
9 4 2
【样例输出】
5
【数据范围】
对于 30% 的数据,N ≤ 100;
对于 60% 的数据,N ≤ 5000;
对于 100% 的数据,N ≤ 100000,K ≤ 100000,T ≤ 3

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<set>
 10 using namespace std;
 11 typedef long long LL;
 12 const int maxn=100010;
 13 int T,ANS,N,K;
 14 int last[maxn],l[maxn],r[maxn];
 15 int disc[maxn],x[maxn],b[maxn];
 16 struct Candy{
 17     int x,y,k;
 18     int id;
 19 }a[maxn];
 20 inline int cmpbyy(const Candy & w,const Candy & e){//´Óϵ½ÉÏ ´Ó×óµ½ÓÒ 
 21     return w.y<e.y||(w.y==e.y&&w.x<e.x);
 22 }
 23 inline int cmpbyx(const Candy & w,const Candy & e){//´Ó×óµ½ÓÒ ´Óϵ½ÉÏ 
 24     return w.x<e.x||(w.x==e.x&&w.y>e.y);
 25 }
 26 struct Tree{
 27     int l,r,sum;
 28 }tr[maxn*8];
 29 inline void Build(int rt,int l,int r){
 30     tr[rt].l=l; tr[rt].r=r;
 31     if(l==r){
 32         tr[rt].sum=b[l];
 33         return ;
 34     }
 35     int mid=(l+r)>>1;
 36     Build(rt<<1,l,mid); Build(rt<<1|1,mid+1,r);
 37     tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
 38 }
 39 inline void change(int rt,int pos,int delta){
 40     if(tr[rt].l==tr[rt].r){
 41         tr[rt].sum+=delta;
 42         return ;
 43     }
 44     int mid=(tr[rt].l+tr[rt].r)>>1;
 45     if(pos<=mid) change(rt<<1,pos,delta);
 46     else change(rt<<1|1,pos,delta);
 47     tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
 48 }
 49 inline int query(int rt,int l,int r){
 50     if(l<=tr[rt].l&&tr[rt].r<=r){
 51         return tr[rt].sum;
 52     }
 53     int mid=(tr[rt].l+tr[rt].r)>>1;
 54     int ans=0;
 55     if(l<=mid) ans+=query(rt<<1,l,r);
 56     if(mid+1<=r) ans+=query(rt<<1|1,l,r);
 57     return ans;
 58 }
 59 inline void update(int l,int r){
 60     if(l>r) return ;
 61     ANS=max(ANS,query(1,l,r));
 62 }
 63 inline void work(){
 64     x[0]=0; x[N+1]=N+1;
 65     memset(last,0,sizeof(last)); memset(b,0,sizeof(b));
 66     for(int i=1;i<=800000;i++) tr[i].l=tr[i].r=tr[i].sum=0;
 67     
 68     sort(a+1,a+N+1,cmpbyx);
 69     for(int i=1;i<=N;i++) b[x[i]]++;
 70     Build(1,1,N+1);
 71     for(int i=1;i<=N;i++){
 72         int tmp=a[i].id,L=last[a[i].k];
 73         l[tmp]=L; r[tmp]=N+1;
 74         if(L) r[L]=tmp;
 75         update(x[L]+1,x[tmp]-1);
 76         last[a[i].k]=tmp;
 77     }
 78     for(int i=1;i<=K;i++){
 79         update(x[last[i]]+1,N+1);
 80     }
 81     sort(a+1,a+N+1,cmpbyy);
 82     for(int i=1,j=1;i<=N;i++){
 83         int tmp=a[i].id;
 84         while(j<=N&&a[j].y==a[i].y){
 85             change(1,a[j].x,-1);
 86             j++;
 87         }
 88         l[r[tmp]]=l[tmp]; r[l[tmp]]=r[tmp];
 89         update(x[l[tmp]]+1,x[r[tmp]]-1);
 90     }
 91 }
 92 int main(){
 93 //    freopen("candy.in","r",stdin);
 94 //    freopen("candy.out","w",stdout);
 95     scanf("%d",&T);
 96     while(T--){
 97         ANS=0;
 98         scanf("%d%d",&N,&K);
 99         for(int i=1;i<=N;i++){
100             scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].k);
101             a[i].id=i;
102         }
103         for(int i=1;i<=N;i++){
104             disc[i]=a[i].x;
105         }
106         sort(disc+1,disc+N+1);
107         int *end=unique(disc+1,disc+N+1);
108         for(int i=1;i<=N;i++){
109             a[i].x=lower_bound(disc+1,end,a[i].x)-disc;
110             x[i]=a[i].x;
111         }
112         work();
113         for(int i=1;i<=N;i++) a[i].y*=-1;
114         work();
115         printf("%d\n",ANS);
116     }
117     return 0; 
118 }

 

转载于:https://www.cnblogs.com/CXCXCXC/p/5319352.html

相关文章:

  • 如何解决 POST 请求中文乱码问题,GET 的又如何处理呢?
  • 修饰符与关键字
  • bootstrap 入门
  • SpringMVC 和 Struts2 的区别有哪些?
  • 递归经典问题详解
  • 讲下Spring框架
  • 视图间坐标转换
  • Spring与SpringMVC的区别
  • php关于的用法
  • Spring与SpringBoot的关系
  • Java网络编程(模拟浏览器访问Tomcat服务器)
  • Spring 、Spring Boot 和 Spring Cloud 的关系
  • xmpp 环境配置
  • SpringBoot常用注解
  • 二OpenStack 安装 Identity Service - Keystone
  • Angular4 模板式表单用法以及验证
  • If…else
  • Laravel Mix运行时关于es2015报错解决方案
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Python利用正则抓取网页内容保存到本地
  • SQL 难点解决:记录的引用
  • TypeScript迭代器
  • 仿天猫超市收藏抛物线动画工具库
  • 区块链分支循环
  • 思维导图—你不知道的JavaScript中卷
  • 微信小程序设置上一页数据
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 移动端唤起键盘时取消position:fixed定位
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (附源码)计算机毕业设计大学生兼职系统
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .gitignore
  • .NET Core 版本不支持的问题
  • .net core 连接数据库,通过数据库生成Modell
  • .NET Core跨平台微服务学习资源
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 中让 Task 支持带超时的异步等待
  • .netcore如何运行环境安装到Linux服务器
  • .NET导入Excel数据
  • .Net多线程总结
  • .net生成的类,跨工程调用显示注释
  • 。Net下Windows服务程序开发疑惑
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • @RequestParam详解
  • [ 隧道技术 ] cpolar 工具详解之将内网端口映射到公网
  • [20150629]简单的加密连接.txt
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [BUG]vscode插件live server无法自动打开浏览器
  • [C/C++]数据结构 循环队列
  • [C++][基础]1_变量、常量和基本类型
  • [C++]Leetcode17电话号码的字母组合
  • [CQOI 2011]动态逆序对
  • [Django 0-1] Core.Email 模块