双指针带刷

一.日志统计 

 日志统计

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


public class Main {
	static int N=100010;
	static int cnt[]=new int[N];
	static boolean st[]=new boolean[N];
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws IOException {
		String[] s = br.readLine().split(" ");
		int n=Integer.parseInt(s[0]);
		int d=Integer.parseInt(s[1]);
		int k=Integer.parseInt(s[2]);
		PII3 pii[]=new PII3[n];
		for(int i=0;i<n;i++) {
			s = br.readLine().split(" ");
			pii[i]=new PII3(Integer.parseInt(s[0]),Integer.parseInt(s[1]));	
		}
		//排序
		Arrays.sort(pii);
		//双指针
		for(int i=0,j=0;i<n;i++) {
			int id=pii[i].id;
			cnt[id]++;
			while(pii[i].ts-pii[j].ts>=d) {
				cnt[pii[j].id]--;
				j++;
			}
			if(cnt[id]>=k) st[id]=true;
		}
		//输出
		for(int i=0;i<N;i++) {
			if(st[i]==true)
				System.out.println(i);
		}
	}
}
class PII3 implements Comparable<PII3>{
	int ts=0;
	int id=0;
	public PII3(int ts, int id) {
		super();
		this.ts = ts;
		this.id = id;
	}
	@Override
	public int compareTo(PII3 o) {
		if(this.ts==o.ts) return this.id-o.id;
		return this.ts-o.ts;
	}
	
}

 二.完全二叉树的权值

完全二叉树的权值

 撒撒水啦,直接前缀和,要注意的是完全二叉树不是满二叉树

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static int N=100010;
	static long a[]=new long[N];
	static long s[]=new long[N];
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n=Integer.parseInt(br.readLine());
		String[] s1 = br.readLine().split(" ");
		for(int i=1;i<=n;i++) {		
			a[i]=Integer.parseInt(s1[i-1]);
			s[i]=s[i-1]+a[i];
		}
		int num=(int)(Math.log(n+1)/Math.log(2));
		if(n>Math.pow(2, num)) num++;
		long cen[]=new long [num];
		for(int i=0;i<num;i++) {
			int left=(int)Math.pow(2, i);
			int right=(int)Math.pow(2,i+1)-1;
			if(i==0) cen[i]=a[1];
			else if(i==num-1) cen[i]=s[n]-s[left-1];
			else cen[i]=s[right]-s[left-1];
			//System.out.println(left+" "+right+" "+cen[i]);
		}
		long max=Long.MIN_VALUE;
		int res=0;
		for(int i=0;i<num;i++) {
			if(cen[i]>max) {
				max=cen[i];
				res=i+1;
			}
		}
		System.out.println(res);
	}
}

前缀和做法


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static int N=100010;
	static int a[]=new int[N];
	static long res;
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n=Integer.parseInt(br.readLine());
		String[] s = br.readLine().split(" ");
		for(int i=1;i<=n;i++) {
			a[i]=Integer.parseInt(s[i-1]);
		}
		long max=Long.MIN_VALUE;
		for(int d=1,i=1;i<=n;i*=2,d++) {//d是层 i是当前层起始
			long sum=0;
		    for (int j = i; j < i + (1 << d - 1) && j <= n; j ++ ){//i + (1 << d - 1)是右端点
				sum+=a[j];
			}
			//System.out.println(d+" "+i+" "+(i+Math.pow(2, d-1))+" "+sum);
			if(sum>max) {
				max=sum;
				res=d;
			}
		}
		System.out.println(res);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪八戒1.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值