一.日志统计
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);
}
}