1、快速排序
public class KuaiSort {
public static int quickSelect(int a[],int l,int r,int k) {
Random rand=new Random();
int p=rand.nextInt(r-l+1)+l;
int x=a[p];
int tmp=a[p];
a[p]=a[r];
a[r]=tmp;
int i=l,j=r;
while(i<j) {
while(i<j&&a[i]<x) i++;
if(i<j) {
a[j]=a[i];
j--;
}
while(i<j && a[j]>x) j--;
if(i<j) {
a[i]=a[j];
i++;
}
}
a[i]=x;
p=i;
if(i-l+1==k) return a[i];
if(i-l+1<k) return quickSelect(a,i+1,r,k-i+l-1);
else return quickSelect(a, l, i-1, k);
}
public static void main(String[] args) {
int[] a= {1,4,2,8,5,7};
System.out.println(quickSelect(a, 0, 5, 2));
}
}
2、螺旋折线
如图p1.pgn所示的螺旋折线经过平面上所有整点恰好一次。
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。
例如dis(0, 1)=3, dis(-2, -1)=9
给出整点坐标(X, Y),你能计算出dis(X, Y)吗?
【输入格式】
X和Y
对于40%的数据,-1000 <= X, Y <= 1000
对于70%的数据,-100000 <= X, Y <= 100000
对于100%的数据, -1000000000 <= X, Y <= 1000000000
【输出格式】
输出dis(X, Y)
【输入样例】
0 1
【输出样例】
3
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。
public class LuoXuan {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int x=input.nextInt();
int y=input.nextInt();
System.out.println(dis(x,y));
}
public static int dis(int x,int y){
if(Math.abs(x)==Math.abs(y))
{
if(x>0&&y>0)
return 4*x*x;
else if(x>0&&y<0)
return 4*x*x+2*x;
else if(x<0&&y>0)
return 4*x*x-2*y;
else
return 4*x*x-4*x;
}
else
{
if(x>0)
{
if(y>0)
{
int max=Math.max(x, y);
if(x<y)
return 4*max*max-Math.abs(x-y);
else
return 4*max*max+Math.abs(x-y);
}
else
{
y=-y;
int max=Math.max(x, y);
if(x<y)
return 4*max*max+2*max+Math.abs(x-y);
else
return 4*max*max+2*max-Math.abs(x-y);
}
}
else
{
if(y>0)
{
x=-x;
int max=Math.max(x,y);
if(x<y)
return 4*max*max-2*max+Math.abs(x-y);
else
return 4*max*max-2*max-Math.abs(x-y);
}
else
{
x=-x;
y=-y;
int max=Math.max(x,y);
if(x<y)
return 4*max*max+4*max-Math.abs(x-y);
else
return 4*max*max+4*max+Math.abs(x-y)-8*max;
}
}
}
}
}
3.日志统计
小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:
ts id
表示在ts时刻编号id的帖子收到一个"赞"。
现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。
具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。
给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。
【输入格式】
第一行包含三个整数N、D和K。
以下N行每行一条日志,包含两个整数ts和id。
对于50%的数据,1 <= K <= N <= 1000
对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000
【输出格式】
按从小到大的顺序输出热帖id。每个id一行。
【输入样例】
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
【输出样例】
1
3
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.Scanner;
public class Login {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int D=input.nextInt();
int K=input.nextInt();
final int MAX=100001;
ArrayList<Obj>[] tsList=new ArrayList[MAX];
for(int i=0;i<N;i++)
{
int ts=input.nextInt();
int id=input.nextInt();
Obj obj=new Obj();
obj.ts=ts;
if(tsList[id]==null)
tsList[id]=new ArrayList<Obj>();
tsList[id].add(obj);
}
for(int i=0;i<MAX;i++)
{
if(tsList[i]==null) continue;
ArrayList<Obj>list=(ArrayList<Obj>)tsList[i].clone();
sort(list);
boolean key=false;
for(int j=0;j<list.size();j++)
{
for(int m=list.size()-1;m>=j;m--)
{
if(list.get(m).ts-list.get(j).ts<D&&m-j+1>=K)
{
key=true;
break;
}
}
if(key)
{
System.out.println(i);
break;
}
}
}
}
public static class Obj implements Comparable<Obj>{
public int ts;
Obj()
{
ts=0;
}
@Override
public int compareTo(Obj o)
{
if(ts>o.ts)
return 1;
else if(ts<o.ts)
return -1;
else
return 0;
}
}
}