結果

問題 No.461 三角形はいくつ?
ユーザー koba-e964koba-e964
提出日時 2016-12-12 12:24:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 208 ms / 5,000 ms
コード長 1,866 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 69,460 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-07 03:15:23
合計ジャッジ時間 5,052 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 180 ms
5,376 KB
testcase_06 AC 98 ms
5,376 KB
testcase_07 AC 118 ms
5,376 KB
testcase_08 AC 77 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 22 ms
5,376 KB
testcase_11 AC 134 ms
5,376 KB
testcase_12 AC 57 ms
5,376 KB
testcase_13 AC 205 ms
5,376 KB
testcase_14 AC 193 ms
5,376 KB
testcase_15 AC 206 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 129 ms
5,376 KB
testcase_19 AC 130 ms
5,376 KB
testcase_20 AC 201 ms
5,376 KB
testcase_21 AC 208 ms
5,376 KB
testcase_22 AC 202 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 22 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 32 ms
5,376 KB
testcase_30 AC 18 ms
5,376 KB
testcase_31 AC 55 ms
5,376 KB
testcase_32 AC 72 ms
5,376 KB
testcase_33 AC 5 ms
5,376 KB
testcase_34 AC 6 ms
5,376 KB
testcase_35 AC 5 ms
5,376 KB
testcase_36 AC 41 ms
5,376 KB
testcase_37 AC 39 ms
5,376 KB
testcase_38 AC 41 ms
5,376 KB
testcase_39 AC 36 ms
5,376 KB
testcase_40 AC 39 ms
5,376 KB
testcase_41 AC 43 ms
5,376 KB
testcase_42 AC 137 ms
5,376 KB
testcase_43 AC 136 ms
5,376 KB
testcase_44 AC 139 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;

ll gcd(ll x, ll y) {
  return y == 0 ? x : gcd(y, x % y);
}

PL add(PL x, PL y) {
  PL w = PL(x.first * y.second + x.second * y.first,
		x.second * y.second);
  return w;
}

PL neg(PL x) { return PL(- x.first, x.second); }

bool fr_le(PL x, PL y) {
  return x.first * y.second <= y.first * x.second;
}
bool fr_lt(PL x, PL y) {
  return x.first * y.second < y.first * x.second;
}
int main(void){
  int n;
  cin >> n;
  vector<PL> pool[3];
  REP(i, 0, 3) {
    pool[i].push_back(PL(0, 1));
  }
  REP(i, 0, n) {
    int p, a, b;
    cin >> p >> a >> b;
    int g = __gcd(a, b);
    a /= g;
    b /= g;
    pool[p].push_back(PL(b, a + b));
  }
  if (pool[0].size() > pool[2].size()) {
    swap(pool[0], pool[2]);
  }
  if (pool[1].size() > pool[2].size()) {
    swap(pool[1], pool[2]);
  }
  ll tot = 0;
  sort(pool[2].begin(), pool[2].end(), fr_lt);
  REP(i, 0, pool[0].size()) {
    PL u = pool[0][i];
    REP(j, 0, pool[1].size()) {
      PL v = pool[1][j];
      if (not fr_le(add(u, v), PL(1, 1))) {
	continue;
      }
      PL ma = u;
      if (fr_le(u, v)) { ma = v; }
      // find #{x | x + u <= 1 && x + v <= 1 && x + u + v != 1}
      PL x_ma = add(PL(1, 1), neg(ma));
      int idx = upper_bound(pool[2].begin(), pool[2].end(), x_ma, fr_lt)
	- pool[2].begin();
      tot += idx;
      PL excl = add(PL(1, 1), neg(add(u, v)));
      {
	int idx1 = upper_bound(pool[2].begin(), pool[2].end(), excl, fr_lt)
	- pool[2].begin();
	int idx2 = lower_bound(pool[2].begin(), pool[2].end(), excl, fr_lt)
	- pool[2].begin();
	assert (idx1 - idx2 <= 1);
	assert (idx1 >= idx2);
	tot -= idx1 - idx2;
      }
    }
  }
  cout << tot << endl;
}
0