~~~~~java题目

P8661 [蓝桥杯 2018 省 B] 日志统计

尺取法。

import java.util.*;
import java.io.*;

public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int N=700005;
	static int n,d,k,cnt[]=new int[N];
	static boolean vis[]=new boolean[N];
	static List<int[]> e=new ArrayList<>();
	
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();d=(int)in.nval;
		in.nextToken();k=(int)in.nval;
		for(int i=1;i<=n;i++)
		{
			in.nextToken();int ts=(int)in.nval;
			in.nextToken();int id=(int)in.nval;
			e.add(new int[] {ts,id});
		}
		Collections.sort(e,new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[0]-o2[0];
			}
		});
//		for(int i=0;i
//			out.println(e.get(i)[0]+" "+e.get(i)[1]);
		for(int i=0,j=0;i<n;i++)
		{
			int ts=e.get(i)[0],id=e.get(i)[1];
			cnt[id]++;
			while(ts-e.get(j)[0]>=d)
			{
				cnt[e.get(j)[1]]--;j++;
			}
			if(cnt[id]>=k) vis[id]=true;
		}
		for(int i=0;i<=100000;i++)
			if(vis[i]) 
				out.println(i);
		out.close();
	}
}

暴力做法:(避免使用HashSet,因为没有自动排序的功能!!!!)

import java.util.*;
import java.io.*;

public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int N=700005;
	static int n,d,k,cnt[]=new int[N];
	static boolean vis[]=new boolean[N];
	static List<int[]> e=new ArrayList<>();
	
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();d=(int)in.nval;
		in.nextToken();k=(int)in.nval;
		for(int i=1;i<=n;i++)
		{
			in.nextToken();int ts=(int)in.nval;
			in.nextToken();int id=(int)in.nval;
			e.add(new int[] {ts,id});
		}
		Collections.sort(e,new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if(o1[0]==o2[0])
					return o1[1]-o2[1];
				return o1[0]-o2[0];
			}
		});
//		for(int i=0;i
//			out.println(e.get(i)[0]+" "+e.get(i)[1]);
		for(int i=0;i<n;i++)
		{
			int ts=e.get(i)[0],id=e.get(i)[1],num=1;
			for(int j=i+1;j<n;j++)
			{
				int p=e.get(j)[0],q=e.get(j)[1];
				if(p>=ts+d) break;
				if(q==id) num++;
			}
			if(num>=k) vis[id]=true;
			
		}
		for(int i=0;i<=100000;i++)
			if(vis[i]) out.println(i);
		out.close();
	}
}

P8663 [蓝桥杯 2018 省 A] 倍数问题

余数问题

import java.util.*;
import java.io.*;

public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int N=700005;static long mod=1000000009;
	
	static int n,k,a[]=new int[N];
	static List<Integer> e[]=new List[1005];
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();k=(int)in.nval;
		for(int i=0;i<k;i++) e[i]=new ArrayList<>();
		for(int i=1;i<=n;i++)
		{
			in.nextToken();a[i]=(int)in.nval;
			e[a[i]%k].add(a[i]);
		}
		for(int i=0;i<k;i++)
			Collections.sort(e[i],Collections.reverseOrder());
		long ans=0;
		for(int i=0;i<k;i++)
		{
			for(int j=0;j<k;j++)
			{
				int g=(k-(i+j)%k)%k;
				if(i==j&&i==g&&e[i].size()>=3)
					ans=Math.max(ans, e[i].get(0)+e[j].get(1)+e[g].get(2));
				else if(i==j&&i!=g&&e[i].size()>=2&&e[g].size()>=1)
					ans=Math.max(ans, e[i].get(0)+e[j].get(1)+e[g].get(0));
				else if(i!=j&&j==g&&e[i].size()>=1&&e[j].size()>=2)
					ans=Math.max(ans, e[i].get(0)+e[j].get(0)+e[g].get(1));
				else if(i==g&&i!=j&&e[i].size()>=2&&e[j].size()>=1)
					ans=Math.max(ans, e[i].get(0)+e[j].get(0)+e[i].get(1));
				else if(i!=j&&j!=g&&e[i].size()>=1&&e[j].size()>=1&&e[g].size()>=1)
					ans=Math.max(ans, e[i].get(0)+e[j].get(0)+e[g].get(0));
			}
		}
		out.println(ans);
		out.close();
	}
}

P8664 [蓝桥杯 2018 省 A] 付账问题

从每个人所付价钱对于S的贡献角度出发。

import java.util.*;
import java.io.*;

public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int N=700005;static long mod=1000000009;static double INF=1e18;
	
	static int n;
	static double a[]=new double[N],s;
	
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();s=in.nval;	
		for(int i=1;i<=n;i++) {
			in.nextToken();a[i]=in.nval;
		}
		Arrays.sort(a,1,n+1);
//		for(int i=1;i<=n;i++) out.print(a[i]+" ");
//		out.println();
		double avg=s/n,sum=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]*(n-i+1)<=s) {
				sum+=(a[i]-avg)*(a[i]-avg);
				s-=a[i];
			}
			else {
				double res=s/(n-i+1);
				sum+=(res-avg)*(res-avg)*(n-i+1);
				break;
			}
		}
		sum=Math.sqrt(sum/n);
		out.println(String.format("%.4f", sum));
		out.close();
	}
}

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