UVA 12123 - Magnetic Train Tracks(計數問題)

來源:互聯網
上載者:User

標籤:style   blog   class   c   code   tar   

題目連結:12123 - Magnetic Train Tracks題意:給定n個點,求有幾個銳角三角形。思路:和UVA 11529是同類的題,枚舉一個做原點,然後剩下點根據這個原點進行極角排序,然後利用two pointer去遍曆一遍,找出角度小於90度的銳角,然後扣掉這些得到鈍角三角形的個數,然後在用總情況去扣掉鈍角就是銳角或直角代碼:
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const double eps = 1e-9;const double pi = acos(-1.0);const int N = 1205;int n;struct Point {double x, y;void read() {scanf("%lf%lf", &x, &y); }} p[N];double r[N * 2];int C(int n, int m) {if (n < m) return 0;int ans = 1;for (int i = 0; i < m; i++)ans = ans * (n - i) / (i + 1);return ans;}double cal(Point a, Point b) {return atan2(b.y - a.y, b.x - a.x);}int solve(int num) {int tn = 0;for (int i = 0; i < n; i++) {if (i == num) continue;r[tn++] = cal(p[num], p[i]); } sort(r, r + tn); for (int i = 0; i < tn; i++) r[tn + i] = r[i] + 2 * pi;int j = 1;int ans = 0;for (int i = 0; i < tn; i++) {while (fabs(r[j] - r[i]) - pi / 2 <= -eps) j++;ans += j - i - 1; } return C(tn, 2) - ans;}int main() {int cas = 0;while (~scanf("%d", &n) && n) {for (int i = 0; i < n; i++)p[i].read();int ans = 0;for (int i = 0; i < n; i++)ans += solve(i);printf("Scenario %d:\n", ++cas);printf("There are %d sites for making valid tracks\n", C(n, 3) - ans); }return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.