HDU 6300 Triangle Partition (C++、java)

题目链接: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;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值