zoukankan      html  css  js  c++  java
  • BZOJ1914 [Usaco2010 OPen]Triangle Counting 数三角形

    hzwer已经说的很好了,在此只能跪烂了

     1 /**************************************************************
     2     Problem: 1914
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:88 ms
     7     Memory:3160 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <cmath>
    12 #include <algorithm>
    13  
    14 using namespace std;
    15 typedef long long ll;
    16 typedef double lf;
    17 const int N = 100005;
    18  
    19 int n;
    20 ll cnt = 0;
    21  
    22 struct P {
    23     ll x, y;
    24     lf an;
    25     P() {}
    26     P(ll _x, ll _y, lf _an) : x(_x), y(_y), an(_an) {}
    27 }a[N];
    28 inline bool operator < (const P &a, const P &b) {
    29     return a.an < b.an;
    30 }
    31 inline ll operator * (const P &a, const P &b) {
    32     return (ll) a.x * b.y - a.y * b.x;
    33 }
    34  
    35 inline int read() {
    36     int x = 0, sgn = 1;
    37     char ch = getchar();
    38     while (ch < '0' || '9' < ch) {
    39         if (ch == '-') sgn = -1;
    40         ch = getchar();
    41     }
    42     while ('0' <= ch && ch <= '9') {
    43         x = x * 10 + ch - '0';
    44         ch = getchar();
    45     }
    46     return sgn * x;
    47 }
    48  
    49 void work() {
    50     int r = 1, t = 0, i;
    51     for (i = 1; i <= n; ++i) {
    52         while ((r % n + 1) != i && a[i] * a[r % n + 1] >= 0) ++t, ++r;
    53         cnt += (ll) t * (t - 1) / 2;
    54         --t;
    55     }
    56 }
    57  
    58 int main() {
    59     n = read();
    60     int i, X, Y;
    61     for (i = 1; i <= n; ++i) {
    62         X = read(), Y = read();
    63         a[i] = P(X, Y, atan2(Y, X));
    64     }
    65     sort(a + 1, a + n + 1);
    66     work();
    67     printf("%lld
    ", (ll) n * (n - 1) * (n - 2) / 6 - cnt);
    68     return 0;
    69 }
    View Code

     话说为了Rank 1我又丧病的使用了fread...还正是神器啊Orz

     1 /**************************************************************
     2     Problem: 1914
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:60 ms
     7     Memory:4728 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <cmath>
    12 #include <algorithm>
    13  
    14 using namespace std;
    15 typedef long long ll;
    16 typedef double lf;
    17 const int N = 100005;
    18 const int Maxbuf = 1600005;
    19 int n;
    20 ll cnt = 0, Left = 0, len;
    21 char buf[Maxbuf];
    22  
    23 struct P {
    24     ll x, y;
    25     lf an;
    26     P() {}
    27     P(ll _x, ll _y, lf _an) : x(_x), y(_y), an(_an) {}
    28 }a[N];
    29 inline bool operator < (const P &a, const P &b) {
    30     return a.an < b.an;
    31 }
    32 inline ll operator * (const P &a, const P &b) {
    33     return (ll) a.x * b.y - a.y * b.x;
    34 }
    35  
    36 inline int read() {
    37     int x = 0, sgn = 1;
    38     while (buf[Left] < '0' || '9' < buf[Left]) {
    39         if (buf[Left] == '-') sgn = -1;
    40         ++Left;
    41     }
    42     while ('0' <= buf[Left] && buf[Left] <= '9') {
    43         x = x * 10 + buf[Left] - '0';
    44         ++Left;
    45     }
    46     return sgn * x;
    47 }
    48  
    49 void work() {
    50     int r = 1, t = 0, i;
    51     for (i = 1; i <= n; ++i) {
    52         while ((r % n + 1) != i && a[i] * a[r % n + 1] >= 0) ++t, ++r;
    53         cnt += (ll) t * (t - 1) / 2;
    54         --t;
    55     }
    56 }
    57  
    58 int main() {
    59     len = fread(buf, 1, Maxbuf, stdin);
    60     buf[len] = ' ';
    61     n = read();
    62     int i, X, Y;
    63     for (i = 1; i <= n; ++i) {
    64         X = read(), Y = read();
    65         a[i] = P(X, Y, atan2(Y, X));
    66     }
    67     sort(a + 1, a + n + 1);
    68     work();
    69     printf("%lld
    ", (ll) n * (n - 1) * (n - 2) / 6 - cnt);
    70     return 0;
    71 }
    View Code

    (p.s. 比Rank 2快了2ms 23333)

    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    清除浮动的三种方式
    js控制滚动条默认在底部
    npm 基础命令
    package.json中^,~的区别
    mac 使用iTerm2快捷登录远程服务器
    iterm 分屏切换快捷键与配色设置
    git tag — 标签相关操作
    gulp iconfont
    webpack 3 升级 webpack4 个人笔记
    import * as x from 'xx' 和 import x from 'xx'
  • 原文地址:https://www.cnblogs.com/rausen/p/4103905.html
Copyright © 2011-2022 走看看