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

ABC318 F - Octopus

解题思路

  • 对于每个宝藏维护n个区间(x-l_i-1,x+l_i],答案一定在这些区间中
  • 对于每个区间的端点由小到大排序
  • 对于每个点进行判断,若当前位置合法,则该点一定为一个右端点
  • 则该点到前一个端点之间均为合法点
  • 若前一个点不合法,则一定是某一个区间限制的左端点,所以该点到这个端点之间均未超出范围,使某一宝藏取不到
  • 若前一个点合法,则在满足的前提下,还避免了重复
import java.io.*;
import java.math.BigInteger;
import java.util.*;//implements Runnable
public class Main {static long md=(long)998244353;static long Linf=Long.MAX_VALUE/2;static int inf=Integer.MAX_VALUE/2;static int N=200010;static int n=0;static int m=0;static long ans=0;static long[] a;static long[] b;static boolean check(long x){PriorityQueue<Long> q=new PriorityQueue<>((o1,o2)->{if(o1-o2>0)return 1;else if(o1-o2<0)return -1;else return 0;});for(int i=1;i<=n;++i){q.add(Math.abs(x-a[i]));}for(int i=1;i<=n;++i){if(q.poll()>b[i])return false;}return true;}static void solve() throws Exception{AReader input=new AReader();
//        String fileName="C:\\Users\\Lenovo\\Downloads\\055.txt";
//		Scanner input=new Scanner(new FileReader(fileName));//        BufferedReader input = new BufferedReader(new FileReader(fileName));PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));String al="abcdefghijklmnopqrstuvwxyz";char[] ac=al.toCharArray();n=input.nextInt();a=new long[n+1];for(int i=1;i<=n;++i)a[i]=input.nextLong();b=new long[n+1];for(int i=1;i<=n;++i)b[i]=input.nextLong();Arrays.sort(b,1,n+1);TreeSet<Long> hs=new TreeSet<>();for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){hs.add(a[i]-b[j]-1);//左端点,左开右闭,区分左端点和右端点hs.add(a[i]+b[j]);//右端点}}long l=0;for(long x:hs){if(check(x))ans+=x-l;l=x;//左端点,要么是右端点区间去重叠}out.println(ans);out.flush();out.close();}public static void main(String[] args) throws Exception{solve();}//	public static final void main(String[] args) throws Exception {
//		  new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
//	}
//		@Override
//		public void run() {
//			try {
//				//原本main函数的内容
//				solve();
//
//			} catch (Exception e) {
//			}
//		}staticclass AReader{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public AReader(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//确定下一个token只有一个字符的时候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public void println() throws IOException {bw.newLine();}public void println(int[] arr) throws IOException{for (int value : arr) {bw.write(value + " ");}println();}public void println(int l, int r, int[] arr) throws IOException{for (int i = l; i <= r; i ++) {bw.write(arr[i] + " ");}println();}public void println(int a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{bw.write(a);bw.newLine();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}}
}

 

http://www.lryc.cn/news/331398.html

相关文章:

  • Docker实战教程 第3章 Dockerfile
  • JSON在量化交易系统中的应用
  • x-cmd-pkg | broot 是基于 Rust 开发的一个终端文件管理器
  • 设置asp.net core WebApi函数请求参数可空的两种方式
  • Vue.js组件精讲 开篇:Vue.js的精髓——组件
  • R语言中的常用数据结构
  • 基于Python的微博旅游情感分析、微博舆论可视化系统
  • 机器学习的模型校准
  • 0.17元的4位数码管驱动芯片AiP650,支持键盘,还是无锡国家集成电路设计中心某公司的
  • 【C++】编程规范之内存规则
  • 并发编程之线程池的应用以及一些小细节的详细解析
  • 基于JSP的农产品供销服务系统
  • redis之主从复制、哨兵模式
  • 【随笔】Git 基础篇 -- 分支与合并 git rebase(十)
  • 图像识别技术在体育领域的应用
  • 【项目新功能开发篇】开发编码
  • 软件设计原则:开闭原则
  • Python如何下载视频
  • 使用虚拟引擎为AR体验提供动力
  • Kafka入门到实战-第五弹
  • Ideal Holidays
  • Raven:一款功能强大的CICD安全分析工具
  • 【苹果MAC】苹果电脑 LOGI罗技鼠标设置左右切换全屏页面快捷键
  • IDE/VS2015和VS2017帮助文档MSDN安装和使用
  • 开启 Keep-Alive 可能会导致http 请求偶发失败
  • 【leetcode面试经典150题】4.删除有序数组中的重复项 II(C++)
  • 【LeetCode热题100】【普通数组】合并区间
  • 自我认识的方法模型图
  • dhcp和dhcp中继代理
  • 【fastadmin】脚本模式下,日志钩子函数执行出现死循环,导致内存溢出奔溃