传送门: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100
由于2 <= N <= 10000, 所以不难想出一个O(n^2)的枚举算法,枚举两个点的坐标。不断更新最大斜率的值,用一个结构体数组来记录两个点,每次更新的时候将数组的下标重置为1。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; int n, cnt, maxn = -1; int x[10005], y[10005]; struct node { int c, b; }a[10005]; bool cmp(node f, node g) { if(x[f.c] > x[g.c])return 1; else return 0; } int main() { scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d%d", &x[i], &y[i]); } for(int i=1; i<=n; i++) { for(int j=i+1; j<=n; j++) { if(maxn < (y[i]-y[j])/(x[i]-x[j])) { maxn = (y[i]-y[j])/(x[i]-x[j]); cnt = 1; a[cnt].c = i, a[cnt].b = j; } else { if(maxn == (y[i]-y[j])/(x[i]-x[j])) { a[++cnt].c = i, a[cnt].b = j; } } } } sort(a+1, a+1+cnt, cmp); for(int i=1; i<=cnt; i++) { if(x[a[i].c] > x[a[i].b]) printf("%d %d\n", a[i].b, a[i].c); else printf("%d %d\n", a[i].c, a[i].b); } }