zoukankan      html  css  js  c++  java
  • 51nod 1100 斜率最大 计算几何

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
     收藏
     关注
    平面上有N个点,任意2个点确定一条直线,求出所有这些直线中,斜率最大的那条直线所通过的两个点。
     
    (点的编号为1-N,如果有多条直线斜率相等,则输出所有结果,按照点的X轴坐标排序,正序输出。数据中所有点的X轴坐标均不相等,且点坐标为随机。)
    Input
    第1行,一个数N,N为点的数量。(2 <= N <= 10000)
    第2 - N + 1行:具体N个点的坐标,X Y均为整数(-10^9 <= X,Y <= 10^9)
    Output
    每行2个数,中间用空格分隔。分别是起点编号和终点编号(起点的X轴坐标 < 终点的X轴坐标)
    Input示例
    5
    1 2
    6 8
    4 4
    5 4
    2 3
    Output示例
    4 2


    关键是推出在题目条件下 最大的斜率肯定是存在于相邻的两个点之间 然后就容易做了
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<int, int> PII;
    
    const int maxn = 10000 + 5;
    
    struct node {
        int x, y;
        int num;
    } a[maxn];
    
    int cmp(node aa, node bb) {
        return aa.x < bb.x;
    }
    
    int ans1[maxn];
    int ans2[maxn];
    
    int main() {
        //FIN
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i].x, &a[i].y);
            a[i].num = i;
        }
        sort(a + 1,  a + 1 + n, cmp);
        double mx = -INF;
        int x = 0, y = 0;
        int cas = 0;
        for(int i = 1; i < n; i++) {
            double p = (double)(a[i].y - a[i+1].y) / (a[i].x - a[i+1].x);
            if(p > mx) {
                cas = 0;
                ans1[cas] = a[i].num, ans2[cas] = a[i+1].num;
                mx = p;
                cas++;
            } else if(p == mx) {
                ans1[cas] = a[i].num, ans2[cas] = a[i+1].num;
                mx = p;
                cas++;
            }
    
        }
        for(int i = 0; i < cas; i++) {
            //if(a[ans1[i]].x > a[ans2[i]].x) swap(ans1[i], ans2[i]);
            printf("%d %d
    ", ans1[i], ans2[i]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    leftpad填充函数;
    overfllow的解析
    append与after
    数组扁平话的N种解法;
    关于webapi调用wcf并发假死的分析
    C#金额数字转换中文繁体
    关于ios的IDFA
    Windows下为MySQL做定时备份
    [System.OutOfMemoryException] {函数求值已禁用,因为出现内存不足异常。
    mvc 捕获404和500 等
  • 原文地址:https://www.cnblogs.com/Hyouka/p/7278706.html
Copyright © 2011-2022 走看看