双指针带刷

一.日志统计 

 日志统计

双指针带刷_第1张图片

双指针带刷_第2张图片 双指针带刷_第3张图片


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=d) {
				cnt[pii[j].id]--;
				j++;
			}
			if(cnt[id]>=k) st[id]=true;
		}
		//输出
		for(int i=0;i{
	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;imax) {
				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);
	}
}

 

你可能感兴趣的:(蓝桥杯C/C++,算法,java)