51nod - 1100 斜率最大

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100

虽然这题数据有点小,但是做题嘛,总是要有追求的,不能什么都靠暴力。

随便画个图就可以看到,斜率最大的情况它们的横坐标一定是相邻的。于是,最后只要排个序就好了。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e4 + 100;
 4 struct st
 5 {
 6     int x, y, pos;
 7 }a[maxn];
 8 bool cmp(st a, st b) {
 9     return a.x < b.x;
10 }
11 double xielv(st a, st b) {
12     return (a.y-b.y)*1.0/(a.x-b.x);
13 }
14 int main() {
15     int n, x, y;    
16     scanf("%d", &n);
17     for(int i=0; i<n; i++) {
18         scanf("%d%d", &x, &y);
19         a[i] = (st) {x, y, i+1};
20     }
21     sort(a, a+n, cmp);
22     double maxxl = -1e9;
23     int index = -1;
24     for(int i=1; i<n; i++) {
25         if(maxxl < xielv(a[i], a[i-1])) {
26             maxxl = xielv(a[i], a[i-1]);
27             index = i;
28         }
29     }
30     if(a[index].x < a[index-1].x) printf("%d %d\n", a[index].pos, a[index-1].pos);
31     else printf("%d %d\n", a[index-1].pos, a[index].pos);
32     return 0;
33 }

 

posted @ 2016-09-08 19:58  蓝精灵III  阅读(206)  评论(0编辑  收藏  举报