题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6300
类型:结构体sort排序
题解:按x坐标排序就好,相当于从左往右扫,因为保证不会三点共线,所以是满足的
p.s:比赛的时候数组开小了,然后一直tle,切回了java,比完才发现数组开小,die(orz)
C++版
#include<stdio.h>
#include<string.h>
#include <algorithm>
using namespace std;
struct pp{
int i;
int x;
int y;
}u[3005];
bool cmp1(pp a,pp b){
if(a.x != b.x){
return a.x < b.x;
}else{
return a.y < b.y;
}
}
int n,t,x,y;
int main()
{
scanf("%d",&n);
while(n-->0)
{
scanf("%d",&t);
for(int i=1;i<=t*3;i++){
scanf("%d%d",&x,&y);
u[i].i=i;
u[i].x=x;
u[i].y=y;
}
sort(u+1,u+3*t+1,cmp1);
for(int i=1;i<=t;i++){
printf("%d %d %d\n",u[3*i-2].i,u[i*3-1].i,u[i*3].i);
}
}
return 0;
}
java版
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class a1003 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int t = nextInt();
while (t-- > 0) {
int n = nextInt();
ArrayList<pp> p = new ArrayList<pp>();
for (int i = 1; i <= 3 * n; i++) {
p.add(new pp(i, nextInt(), nextInt()));
}
Collections.sort(p, new pp.g());
for(int i=0;i<n*3;i+=3) {
System.out.println(p.get(i).i+" "+p.get(i+1).i+" "+p.get(i+2).i);
}
}
}
}
class pp {
int i;
int x;
int y;
public pp(int i, int x, int y) {
super();
this.i = i;
this.x = x;
this.y = y;
}
public pp() {
super();
}
static class g implements Comparator {
@Override
public int compare(Object o1, Object o2) {
pp p1 = (pp) o1;
pp p2 = (pp) o2;
if (p1.x != p2.x) {
return p1.x > p2.x ? 1 : -1;
} else {
if (p1.y != p2.y) {
return p1.y > p2.y ? 1 : -1;
}
}
return 0;
}
}
}