problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02289
C++
Time Limit Exceeded
#include <iostream> #include <set> int main() { std::multiset<int> S; while (1) { std::string Command; std::cin >> Command; if (Command[0] == 'i') { int Value; std::cin >> Value; S.insert(Value); } else if (Command[1] == 'x') { std::set<int>::reverse_iterator It = S.rbegin(); std::cout << *It << std::endl; S.erase(--(It.base())); } else { break; } } return 0; }
#include <iostream> #include <set> int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::multiset<int> S; while (1) { std::string Command; std::cin >> Command; if (Command[0] == 'i') { int Value; std::cin >> Value; S.insert(Value); } else if (Command[1] == 'x') { std::set<int>::reverse_iterator It = S.rbegin(); std::cout << *It << std::endl; S.erase(--(It.base())); } else { break; } } return 0; }
insert
4
4
4
6
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stdio.h> using namespace std; #define INFTY (1 << 30) #define MAX 500000 int N = 0; int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int parent(int i) { return i / 2; } int maxOf3Node(int data[], int base) { int l = left(base); int r = right(base); int leargest; if (l <= N && data[l] > data[base]) leargest = l; else leargest = base; if (r <= N && data[r] > data[leargest]) leargest = r; return leargest; } void maxHeapify(int data[], int i) { int leargest = maxOf3Node(data, i); if (leargest != i) { swap(data[leargest], data[i]); maxHeapify(data, leargest); } } void buildMaxHeap(int data[]) { for (int i = N / 2; i > 0; i--) maxHeapify(data, i); } void insert(int data[], int key) { N++; data[N] = key; int i = N; while (i > 1 && data[parent(i)] < data[i]) { swap(data[parent(i)], data[i]); i = parent(i); } } int extract(int data[]) { int max = data[1]; data[1] = data[N]; N--; maxHeapify(data, 1); return max; } void trace(int data[]) { for (int i = 1; i <= N; i++) cout << " " << data[i]; cout << endl; } int main() { int data[MAX + 1]; int x; char com[20]; while (true) { scanf("%s", com); if (com[0] == 'i') { // insert scanf("%d", &x); insert(data, x); } else if (com[1] == 'x') { // extract int max = extract(data); cout << max << endl; } else if (com[0] == 'e') { // end break; } } return 0; }
#include <algorithm> #include <iostream> #include <stdio.h> using namespace std; #define INFTY (1 << 30) #define MAX 2000000 int N = 0; int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int parent(int i) { return i / 2; } int maxOf3Node(int data[], int base) { int l = left(base); int r = right(base); int leargest; if (l <= N && data[l] > data[base]) leargest = l; else leargest = base; if (r <= N && data[r] > data[leargest]) leargest = r; return leargest; } void maxHeapify(int data[], int i) { int leargest = maxOf3Node(data, i); if (leargest != i) { swap(data[leargest], data[i]); maxHeapify(data, leargest); } } void buildMaxHeap(int data[]) { for (int i = N / 2; i > 0; i--) maxHeapify(data, i); } void insert(int data[], int key) { N++; data[N] = key; int i = N; while (i > 1 && data[parent(i)] < data[i]) { swap(data[parent(i)], data[i]); i = parent(i); } } int extract(int data[]) { int max = data[1]; data[1] = data[N]; N--; maxHeapify(data, 1); return max; } void trace(int data[]) { for (int i = 1; i <= N; i++) cout << " " << data[i]; cout << endl; } int main() { int data[MAX + 1]; int x; char com[20]; while (true) { scanf("%s", com); if (com[0] == 'i') { // insert scanf("%d", &x); insert(data, x); } else if (com[1] == 'x') { // extract int max = extract(data); cout << max << endl; } else if (com[0] == 'e') { // end break; } } return 0; }
replace
5
6
5
6
0
p02289
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <cassert> #include <cstddef> #include <functional> #include <memory> #include <type_traits> #include <utility> template <class TotallyOrderedSet> class pairing_heap { public: using set_type = TotallyOrderedSet; using value_type = typename set_type::value_type; using const_reference = const value_type &; using size_type = std::size_t; private: struct node_t { value_type value; node_t *left, *right; template <class... Args> node_t(Args &&...args) noexcept( std::is_nothrow_constructible<value_type, Args...>::value) : value(std::forward<Args>(args)...), left(nullptr), right(nullptr) {} }; using pointer = node_t *; pointer root; size_type s; static pointer merge(pointer x, pointer y) noexcept( noexcept(set_type::less_equal(std::declval<value_type>(), std::declval<value_type>()))) { if (!set_type::less_equal(x->value, y->value)) std::swap(x, y); y->right = x->left; x->left = y; return x; } static pointer mergelist(pointer curr) noexcept( noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) { pointer head(nullptr), temp(nullptr), next(nullptr); while (curr) { next = (curr->right); if (next) { temp = (next->right); curr = merge((curr), (next)); } curr->right = (head); head = (curr); curr = (temp); } if (head) { curr = (head); head = (curr->right); } while (head) { next = (head->right); curr = merge((curr), (head)); head = (next); } if (curr) curr->right = nullptr; return (curr); } public: pairing_heap() noexcept : root(), s(0) {} bool empty() const noexcept { return !root; } size_type size() const noexcept { return s; } const_reference top() const noexcept { assert(!empty()); return root->value; } void push(const value_type &x) { if (empty()) root = new node_t(x); else root = merge((root), new node_t(x)); ++s; } void push(value_type &&x) { if (empty()) root = new node_t((x)); else root = merge((root), new node_t((x))); ++s; } template <class... Args> void emplace(Args &&...args) { if (empty()) root = new node_t(std::forward<Args>(args)...); else root = merge((root), new node_t(std::forward<Args>(args)...)); ++s; } void pop() noexcept(noexcept(mergelist(std::declval<pointer>()))) { assert(!empty()); root = mergelist((root->left)); --s; } pairing_heap &meld(pairing_heap &other) noexcept( noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) { if (!other.empty()) { s += other.s; other.s = 0; root = merge((root), (other.root)); } return *this; } pairing_heap &operator+=(pairing_heap &other) noexcept( noexcept(meld(std::declval<pairing_heap &>()))) { return meld(other); } }; #include <algorithm> #include <limits> #include <utility> template <class T> class ordered_set { public: using value_type = T; static bool less_equal(const value_type &x, const value_type &y) noexcept( noexcept(std::declval<value_type>() >= std::declval<value_type>())) { return x >= y; } }; #include <cstdio> #include <string> void scan(std::string &s) { s.clear(); int c = fgetc(stdin); while (c == ' ' || c == '\n') c = fgetc(stdin); while (c != ' ' && c != '\n') { s.push_back(c); c = fgetc(stdin); } } int main() { pairing_heap<ordered_set<int>> Q; std::string s; int k; while (scan(s), s[2] != 'd') { if (s[0] == 'i') { scanf("%d", &k); Q.push(k); } else { printf("%d\n", Q.top()); Q.pop(); } } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <cassert> #include <cstddef> #include <functional> #include <memory> #include <type_traits> #include <utility> template <class TotallyOrderedSet> class pairing_heap { public: using set_type = TotallyOrderedSet; using value_type = typename set_type::value_type; using const_reference = const value_type &; using size_type = std::size_t; private: struct node_t { value_type value; node_t *left, *right; template <class... Args> node_t(Args &&...args) noexcept( std::is_nothrow_constructible<value_type, Args...>::value) : value(std::forward<Args>(args)...), left(nullptr), right(nullptr) {} }; using pointer = node_t *; pointer root; size_type s; static pointer merge(pointer x, pointer y) noexcept( noexcept(set_type::less_equal(std::declval<value_type>(), std::declval<value_type>()))) { if (!set_type::less_equal(x->value, y->value)) std::swap(x, y); y->right = x->left; x->left = y; return x; } static pointer mergelist(pointer curr) noexcept( noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) { pointer head(nullptr), temp(nullptr), next(nullptr); while (curr) { next = (curr->right); if (next) { temp = (next->right); curr = merge((curr), (next)); } else temp = nullptr; curr->right = (head); head = (curr); curr = (temp); } if (head) { curr = (head); head = (curr->right); } while (head) { next = (head->right); curr = merge((curr), (head)); head = (next); } if (curr) curr->right = nullptr; return (curr); } public: pairing_heap() noexcept : root(), s(0) {} bool empty() const noexcept { return !root; } size_type size() const noexcept { return s; } const_reference top() const noexcept { assert(!empty()); return root->value; } void push(const value_type &x) { if (empty()) root = new node_t(x); else root = merge((root), new node_t(x)); ++s; } void push(value_type &&x) { if (empty()) root = new node_t((x)); else root = merge((root), new node_t((x))); ++s; } template <class... Args> void emplace(Args &&...args) { if (empty()) root = new node_t(std::forward<Args>(args)...); else root = merge((root), new node_t(std::forward<Args>(args)...)); ++s; } void pop() noexcept(noexcept(mergelist(std::declval<pointer>()))) { assert(!empty()); root = mergelist((root->left)); --s; } pairing_heap &meld(pairing_heap &other) noexcept( noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) { if (!other.empty()) { s += other.s; other.s = 0; root = merge((root), (other.root)); } return *this; } pairing_heap &operator+=(pairing_heap &other) noexcept( noexcept(meld(std::declval<pairing_heap &>()))) { return meld(other); } }; #include <algorithm> #include <limits> #include <utility> template <class T> class ordered_set { public: using value_type = T; static bool less_equal(const value_type &x, const value_type &y) noexcept( noexcept(std::declval<value_type>() >= std::declval<value_type>())) { return x >= y; } }; #include <cstdio> #include <string> void scan(std::string &s) { s.clear(); int c = fgetc(stdin); while (c == ' ' || c == '\n') c = fgetc(stdin); while (c != ' ' && c != '\n') { s.push_back(c); c = fgetc(stdin); } } int main() { pairing_heap<ordered_set<int>> Q; std::string s; int k; while (scan(s), s[2] != 'd') { if (s[0] == 'i') { scanf("%d", &k); Q.push(k); } else { printf("%d\n", Q.top()); Q.pop(); } } return 0; }
replace
45
46
45
47
TLE
p02289
C++
Runtime Error
#include <cstdio> #include <queue> #include <string> using namespace std; int main() { priority_queue<int> PQ; char com[10]; int key; while (1) { if (com[0] == 'e' && com[1] == 'n') break; else if (com[0] == 'i') { scanf("%d", &key); PQ.push(key); } else { printf("%d\n", PQ.top()); PQ.pop(); } } return 0; }
#include <cstdio> #include <queue> #include <string> using namespace std; int main() { priority_queue<int> PQ; char com[10]; int key; while (1) { scanf("%s", com); if (com[0] == 'e' && com[1] == 'n') break; else if (com[0] == 'i') { scanf("%d", &key); PQ.push(key); } else { printf("%d\n", PQ.top()); PQ.pop(); } } return 0; }
insert
11
11
11
12
-11
p02289
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int num = 2000000; int a[num]; int h; inline int pa(int i) { return i / 2; } void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int lg; if (l <= h && a[i] < a[l]) lg = l; else lg = i; if (r <= h && a[lg] < a[r]) lg = r; if (lg != i) { swap(a[i], a[lg]); maxHeapify(lg); } } inline void insert(int k) { h++; a[h] = k; int i = h; while (i > 1) { if (a[h] > a[pa(h)]) swap(a[h], a[pa(h)]); i = pa(h); } } inline void extract() { printf("%d", a[1]); printf("\n"); a[1] = a[h]; h--; maxHeapify(1); } int main() { char com[10]; int key; for (int i = 1;; i++) { scanf("%s", &com); if (com[0] == 'i') { scanf("%d", &key); insert(key); } else if (com[1] == 'x') extract(); else break; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int num = 2000000; int a[num]; int h; inline int pa(int i) { return i / 2; } void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int lg; if (l <= h && a[i] < a[l]) lg = l; else lg = i; if (r <= h && a[lg] < a[r]) lg = r; if (lg != i) { swap(a[i], a[lg]); maxHeapify(lg); } } inline void insert(int k) { h++; a[h] = k; int i = h; while (i > 1 && a[i] > a[pa(i)]) { swap(a[i], a[pa(i)]); i = pa(i); } } inline void extract() { printf("%d", a[1]); printf("\n"); a[1] = a[h]; h--; maxHeapify(1); } int main() { char com[10]; int key; for (int i = 1;; i++) { scanf("%s", &com); if (com[0] == 'i') { scanf("%d", &key); insert(key); } else if (com[1] == 'x') extract(); else break; } return 0; }
replace
29
33
29
32
TLE
p02289
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; void maxHeap(vector<ll> &h, int i) { int left = i * 2 + 1; int right = i * 2 + 2; int largest = i; if (left < h.size() && h[left] > h[largest]) largest = left; if (right < h.size() && h[right] > h[largest]) largest = right; if (largest != i) { swap(h[largest], h[i]); maxHeap(h, largest); } } void buildMaxHeap(vector<ll> &heap) { for (int i = heap.size() / 2; i >= 0; i--) { maxHeap(heap, i); } } void input(vector<ll> &heap, int i) { int sub = i + 1; if (sub <= 1) return; if (heap[sub - 1] > heap[sub / 2 - 1]) { swap(heap[sub - 1], heap[sub / 2 - 1]); input(heap, sub / 2 - 1); } } void printheap(const vector<ll> &h) { cout << "size: " << h.size() << endl; ; for (int i = 0; i < h.size(); i++) cout << " " << h[i]; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); vector<ll> heap; string str; ll num; cin >> str; while (str != "end") { if (str == "insert") { cin >> num; heap.push_back(num); input(heap, heap.size() - 1); } else if (str == "print") { printheap(heap); } else { if (!heap.empty()) { cout << heap.front() << endl; heap[0] = heap.back(); heap.pop_back(); buildMaxHeap(heap); } } cin >> str; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; void maxHeap(vector<ll> &h, int i) { int left = i * 2 + 1; int right = i * 2 + 2; int largest = i; if (left < h.size() && h[left] > h[largest]) largest = left; if (right < h.size() && h[right] > h[largest]) largest = right; if (largest != i) { swap(h[largest], h[i]); maxHeap(h, largest); } } void buildMaxHeap(vector<ll> &heap) { for (int i = heap.size() / 2; i >= 0; i--) { maxHeap(heap, i); } } void input(vector<ll> &heap, int i) { int sub = i + 1; if (sub <= 1) return; if (heap[sub - 1] > heap[sub / 2 - 1]) { swap(heap[sub - 1], heap[sub / 2 - 1]); input(heap, sub / 2 - 1); } } void printheap(const vector<ll> &h) { cout << "size: " << h.size() << endl; ; for (int i = 0; i < h.size(); i++) cout << " " << h[i]; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); vector<ll> heap; string str; ll num; cin >> str; while (str != "end") { if (str == "insert") { cin >> num; heap.push_back(num); input(heap, heap.size() - 1); } else if (str == "print") { printheap(heap); } else { if (!heap.empty()) { cout << heap.front() << endl; heap[0] = heap.back(); heap.pop_back(); maxHeap(heap, 0); } } cin >> str; } return 0; }
replace
64
65
64
65
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i, int n) { int l_idx, r_idx, temp; l_idx = left(i + 1) - 1; r_idx = right(i + 1) - 1; temp = i; if (r_idx < n) { if (A[r_idx] > A[i]) { temp = r_idx; } } if (l_idx < n) { if (A[l_idx] > A[temp]) { temp = l_idx; } } if (temp != i) { std::swap(A[i], A[temp]); maxHeapify(A, temp, n); } } void compareParent(int *A, int i, int n) { int p; if (i != 0) { p = parent(i + 1) - 1; if (A[i] > A[p]) { std::swap(A[i], A[p]); compareParent(A, p, n); } } } int main(void) { int n; int A[200000]; n = 0; char op[10]; while (1) { scanf("%s", op); if (op[2] == 'd') { break; } else if (op[2] == 's') { scanf("%d", &A[n]); n++; compareParent(A, n - 1, n); } else { printf("%d\n", A[0]); std::swap(A[n - 1], A[0]); n--; maxHeapify(A, 0, n); } } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i, int n) { int l_idx, r_idx, temp; l_idx = left(i + 1) - 1; r_idx = right(i + 1) - 1; temp = i; if (r_idx < n) { if (A[r_idx] > A[i]) { temp = r_idx; } } if (l_idx < n) { if (A[l_idx] > A[temp]) { temp = l_idx; } } if (temp != i) { std::swap(A[i], A[temp]); maxHeapify(A, temp, n); } } void compareParent(int *A, int i, int n) { int p; if (i != 0) { p = parent(i + 1) - 1; if (A[i] > A[p]) { std::swap(A[i], A[p]); compareParent(A, p, n); } } } int main(void) { int n; int A[2000000]; n = 0; char op[10]; while (1) { scanf("%s", op); if (op[2] == 'd') { break; } else if (op[2] == 's') { scanf("%d", &A[n]); n++; compareParent(A, n - 1, n); } else { printf("%d\n", A[0]); std::swap(A[n - 1], A[0]); n--; maxHeapify(A, 0, n); } } return 0; }
replace
45
46
45
46
0
p02289
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int tree[500000]; int N = 0; void maxHeapify(int i) { int l = i * 2 + 1; int r = i * 2 + 2; int largest = i; if (l < N && tree[l] > tree[largest]) largest = l; if (r < N && tree[r] > tree[largest]) largest = r; if (largest != i) { int temp = tree[i]; tree[i] = tree[largest]; tree[largest] = temp; maxHeapify(largest); } } void insert(int i) { if (i == 0) return; int parent = (i - 1) / 2; if (tree[parent] < tree[i]) { int temp = tree[parent]; tree[parent] = tree[i]; tree[i] = temp; insert(parent); } } int main(int argc, char const *argv[]) { string s; do { cin >> s; if (s == "insert") { int value; cin >> value; tree[N] = value; insert(N); N++; } else if (s == "extract") { int value = tree[0]; tree[0] = tree[N - 1]; N--; maxHeapify(0); cout << value << endl; } } while (s != "end"); return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int tree[20000000]; int N = 0; void maxHeapify(int i) { int l = i * 2 + 1; int r = i * 2 + 2; int largest = i; if (l < N && tree[l] > tree[largest]) largest = l; if (r < N && tree[r] > tree[largest]) largest = r; if (largest != i) { int temp = tree[i]; tree[i] = tree[largest]; tree[largest] = temp; maxHeapify(largest); } } void insert(int i) { if (i == 0) return; int parent = (i - 1) / 2; if (tree[parent] < tree[i]) { int temp = tree[parent]; tree[parent] = tree[i]; tree[i] = temp; insert(parent); } } int main(int argc, char const *argv[]) { string s; do { cin >> s; if (s == "insert") { int value; cin >> value; tree[N] = value; insert(N); N++; } else if (s == "extract") { int value = tree[0]; tree[0] = tree[N - 1]; N--; maxHeapify(0); cout << value << endl; } } while (s != "end"); return 0; }
replace
38
39
38
39
0
p02289
C++
Time Limit Exceeded
#include <deque> #include <iostream> #include <utility> using namespace std; #define F first #define S second #define PB push_back template <class T> struct chain { deque<pair<T, chain *>> v; chain(void) {} void push(T n) { if (v.empty() || n <= v.back().F) v.PB({n, nullptr}); else if (n > v.front().F) v.push_front({n, nullptr}); else { int l = 0, r = static_cast<int>(v.size()), mid; while (r - l > 1) { mid = (r + l) >> 1; if (v[mid].F < n) r = mid; else l = mid; } if (!v[l].S) v[l].S = new chain<T>(); v[l].S->push(n); } return; } void pop() { if (v.size() > 1) { if (v[0].S) { v[0].F = v[0].S->v[0].F; if (v[0].S->v.size() > 1) v[0].S->pop(); else v[0].S = nullptr; } else v.pop_front(); } return; } T top() { return v[0].F; } void print() { for (int i = 0; i < v.size(); i++) { printf("%d ", v[i].F); if (v[i].S) { printf("to: "); v[i].S->print(); } } printf(":end "); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); chain<int> *root = new chain<int>(); string s; int temp; while (true) { cin >> s; if (s.size() < 0) return 0; else if (s[0] == 'i') { cin >> temp; root->push(temp); } else { cout << root->top() << endl; if (root->v.size() == 1) root->v.pop_front(); else root->pop(); } // root->print(); // printf("\n\n"); } }
#include <deque> #include <iostream> #include <utility> using namespace std; #define F first #define S second #define PB push_back template <class T> struct chain { deque<pair<T, chain *>> v; chain(void) {} void push(T n) { if (v.empty() || n <= v.back().F) v.PB({n, nullptr}); else if (n > v.front().F) v.push_front({n, nullptr}); else { int l = 0, r = static_cast<int>(v.size()), mid; while (r - l > 1) { mid = (r + l) >> 1; if (v[mid].F < n) r = mid; else l = mid; } if (!v[l].S) v[l].S = new chain<T>(); v[l].S->push(n); } return; } void pop() { if (v.size() > 1) { if (v[0].S) { v[0].F = v[0].S->v[0].F; if (v[0].S->v.size() > 1) v[0].S->pop(); else v[0].S = nullptr; } else v.pop_front(); } return; } T top() { return v[0].F; } void print() { for (int i = 0; i < v.size(); i++) { printf("%d ", v[i].F); if (v[i].S) { printf("to: "); v[i].S->print(); } } printf(":end "); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); chain<int> *root = new chain<int>(); string s; int temp; while (true) { cin >> s; if (s.size() < 5) return 0; else if (s[0] == 'i') { cin >> temp; root->push(temp); } else { cout << root->top() << endl; if (root->v.size() == 1) root->v.pop_front(); else root->pop(); } // root->print(); // printf("\n\n"); } }
replace
63
64
63
64
TLE
p02289
C++
Runtime Error
#include <iostream> using namespace std; void Insert(int); void Increase(int, int); int Extract(void); void maxHeapify(int i); int H[1000]; int n = 0; int main() { int key, n = 0; char ch[10]; while (1) { cin >> ch; if (ch[0] == 'i') { cin >> key; Insert(key); } else if (ch[0] == 'e' && ch[1] == 'x') { cout << Extract() << endl; } else { break; } } for (int i = 0; i < n; ++i) { cout << H[i] << endl; } return 0; } void Insert(int key) { n += 1; H[n] = -1; Increase(n, key); } void Increase(int i, int key) { int tmp; H[i] = key; while (i > 1 && H[i / 2] < H[i]) { tmp = H[i]; H[i] = H[i / 2]; H[i / 2] = tmp; i = i / 2; } } int Extract() { int max = H[1]; H[1] = H[n]; n--; maxHeapify(1); return max; } void maxHeapify(int i) { int r, l, largest, tmp; l = i * 2; r = i * 2 + 1; if (l <= n && H[l] > H[i]) largest = l; else largest = i; if (r <= n && H[r] > H[largest]) largest = r; if (largest != i) { tmp = H[i]; H[i] = H[largest]; H[largest] = tmp; maxHeapify(largest); } }
#include <iostream> using namespace std; void Insert(int); void Increase(int, int); int Extract(void); void maxHeapify(int i); int H[1000000]; int n = 0; int main() { int key, n = 0; char ch[10]; while (1) { cin >> ch; if (ch[0] == 'i') { cin >> key; Insert(key); } else if (ch[0] == 'e' && ch[1] == 'x') { cout << Extract() << endl; } else { break; } } for (int i = 0; i < n; ++i) { cout << H[i] << endl; } return 0; } void Insert(int key) { n += 1; H[n] = -1; Increase(n, key); } void Increase(int i, int key) { int tmp; H[i] = key; while (i > 1 && H[i / 2] < H[i]) { tmp = H[i]; H[i] = H[i / 2]; H[i / 2] = tmp; i = i / 2; } } int Extract() { int max = H[1]; H[1] = H[n]; n--; maxHeapify(1); return max; } void maxHeapify(int i) { int r, l, largest, tmp; l = i * 2; r = i * 2 + 1; if (l <= n && H[l] > H[i]) largest = l; else largest = i; if (r <= n && H[r] > H[largest]) largest = r; if (largest != i) { tmp = H[i]; H[i] = H[largest]; H[largest] = tmp; maxHeapify(largest); } }
replace
6
7
6
7
0
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <string> using namespace std; int N; int H[2000001]; int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int parent(int i) { return i / 2; } void Maximize(int i) { int l = left(i); int r = right(i); int largest = i; if (l <= N && H[l] > H[largest]) largest = l; if (r <= N && H[r] > H[largest]) largest = r; if (largest != i) { swap(H[largest], H[i]); Maximize(largest); } } void Minimize(int i) { int p = parent(i); if (p > 0 && H[p] < H[i]) { swap(H[p], H[i]); Minimize(p); } } void extract() { if (N == 0) return; cout << H[1] << endl; H[1] = H[N]; N--; Maximize(1); } void insert(int x) { N++; H[N] = x; Minimize(N); } int main() { int command; N = 0; while (true) { char com[16]; scanf("%d", com); if (com[2] == 'd') break; if (com[0] == 'i') { int num; cin >> num; insert(num); } else { extract(); } } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> using namespace std; int N; int H[2000001]; int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int parent(int i) { return i / 2; } void Maximize(int i) { int l = left(i); int r = right(i); int largest = i; if (l <= N && H[l] > H[largest]) largest = l; if (r <= N && H[r] > H[largest]) largest = r; if (largest != i) { swap(H[largest], H[i]); Maximize(largest); } } void Minimize(int i) { int p = parent(i); if (p > 0 && H[p] < H[i]) { swap(H[p], H[i]); Minimize(p); } } void extract() { if (N == 0) return; cout << H[1] << endl; H[1] = H[N]; N--; Maximize(1); } void insert(int x) { N++; H[N] = x; Minimize(N); } int main() { int command; N = 0; while (true) { char com[16]; scanf("%s", com); if (com[2] == 'd') break; if (com[0] == 'i') { int num; cin >> num; insert(num); } else { extract(); } } return 0; }
replace
60
61
60
61
TLE
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAX = 2000000; int S[MAX + 1]; int H = 0; void maxHeapify(int A[], int n, int i) { int largest = i; int l = i * 2; int r = i * 2 + 1; if (l <= n && A[l] > A[i]) largest = l; if (r <= n && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeapify(A, n, largest); } } void buildMaxHeap(int A[], int n) { for (int i = n / 2; i >= 1; --i) maxHeapify(A, n, i); } void insert(int S[], int k) { ++H; S[H] = k; int i = H; while (i > 1 && S[i / 2] < S[i]) { swap(S[i / 2], S[i]); i = i / 2; } } int extract(int S[]) { int ret = S[1]; swap(S[1], S[H]); --H; buildMaxHeap(S, H); return ret; } int main() { while (1) { char order[7]; scanf("%s", order); int k; if (strcmp(order, "insert") == 0) { scanf("%d", &k); insert(S, k); } else if (strcmp(order, "extract") == 0) { cout << extract(S) << endl; } else { break; } } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAX = 2000000; int S[MAX + 1]; int H = 0; void maxHeapify(int A[], int n, int i) { int largest = i; int l = i * 2; int r = i * 2 + 1; if (l <= n && A[l] > A[i]) largest = l; if (r <= n && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeapify(A, n, largest); } } void buildMaxHeap(int A[], int n) { for (int i = n / 2; i >= 1; --i) maxHeapify(A, n, i); } void insert(int S[], int k) { ++H; S[H] = k; int i = H; while (i > 1 && S[i / 2] < S[i]) { swap(S[i / 2], S[i]); i = i / 2; } } int extract(int S[]) { int ret = S[1]; swap(S[1], S[H]); --H; maxHeapify(S, H, 1); return ret; } int main() { while (1) { char order[7]; scanf("%s", order); int k; if (strcmp(order, "insert") == 0) { scanf("%d", &k); insert(S, k); } else if (strcmp(order, "extract") == 0) { cout << extract(S) << endl; } else { break; } } return 0; }
replace
46
47
46
47
TLE
p02289
C++
Runtime Error
#include <cstdio> #include <iostream> constexpr int MAXN = 250; int Heap[MAXN + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } #define FOR(i, a, b) for (int i = (a); i < (b); i++) int n; int heapsize; void ShowHeap(int *Heap) { FOR(i, 1, heapsize + 1) { std::printf("node %d: key = %d, ", i, Heap[i]); if (parent(i)) std::printf("parent key = %d, ", Heap[parent(i)]); if (left(i) <= heapsize) std::printf("left key = %d, ", Heap[left(i)]); if (right(i) <= heapsize) std::printf("right key = %d, ", Heap[right(i)]); std::cout << std::endl; } } void MaxHeapify(int *H, int i) { int l = left(i), r = right(i); int largest = i; if (l <= heapsize && H[largest] < H[l]) { largest = l; } if (r <= heapsize && H[largest] < H[r]) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest); } } void BuildHeap(int *H) { for (int i = heapsize / 2; i > 0; --i) { MaxHeapify(H, i); } } void IncreaseKeyHeap(int *H, int i, int k) { H[i] = k; int p = parent(i); while (H[p] < H[i]) { if (!p) break; std::swap(H[p], H[i]); if (!parent(p)) break; i = p; p = parent(p); } } void InsertHeap(int *H, int k) { H[++heapsize] = 0; IncreaseKeyHeap(H, heapsize, k); } int ExtractHeapMax(int *H) { int max = H[1]; H[1] = H[heapsize--]; MaxHeapify(H, 1); return max; } int main() { heapsize = 0; while (true) { std::string ins; std::cin >> ins; if (ins == "insert") { int i; std::cin >> i; InsertHeap(Heap, i); } else if (ins == "extract") { std::cout << ExtractHeapMax(Heap) << std::endl; } else { break; }; } }
#include <cstdio> #include <iostream> constexpr int MAXN = 2000000; int Heap[MAXN + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } #define FOR(i, a, b) for (int i = (a); i < (b); i++) int n; int heapsize; void ShowHeap(int *Heap) { FOR(i, 1, heapsize + 1) { std::printf("node %d: key = %d, ", i, Heap[i]); if (parent(i)) std::printf("parent key = %d, ", Heap[parent(i)]); if (left(i) <= heapsize) std::printf("left key = %d, ", Heap[left(i)]); if (right(i) <= heapsize) std::printf("right key = %d, ", Heap[right(i)]); std::cout << std::endl; } } void MaxHeapify(int *H, int i) { int l = left(i), r = right(i); int largest = i; if (l <= heapsize && H[largest] < H[l]) { largest = l; } if (r <= heapsize && H[largest] < H[r]) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest); } } void BuildHeap(int *H) { for (int i = heapsize / 2; i > 0; --i) { MaxHeapify(H, i); } } void IncreaseKeyHeap(int *H, int i, int k) { H[i] = k; int p = parent(i); while (H[p] < H[i]) { if (!p) break; std::swap(H[p], H[i]); if (!parent(p)) break; i = p; p = parent(p); } } void InsertHeap(int *H, int k) { H[++heapsize] = 0; IncreaseKeyHeap(H, heapsize, k); } int ExtractHeapMax(int *H) { int max = H[1]; H[1] = H[heapsize--]; MaxHeapify(H, 1); return max; } int main() { heapsize = 0; while (true) { std::string ins; std::cin >> ins; if (ins == "insert") { int i; std::cin >> i; InsertHeap(Heap, i); } else if (ins == "extract") { std::cout << ExtractHeapMax(Heap) << std::endl; } else { break; }; } }
replace
3
4
3
4
0
p02289
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct node { node *p, *l, *r; int cnt; void Init(node *p_) { p = p_; l = r = nullptr; cnt = 0; } }; int it; node pool[1000000]; node *new_node(node *p) { pool[it].Init(p); return &pool[it++]; } class uint_Trie { node *root; int siz; public: uint_Trie() : root(new_node(nullptr)) {} int size() { return root->cnt; } void insert(unsigned val) { // if (find(val)) return; // multiset ???????¶???? node *t = root; for (int i = 30; i >= 0; i--) { t->cnt++; if (val & (1 << i)) { if (t->r == nullptr) { t->r = new_node(t); } t = t->r; } else { if (t->l == nullptr) { t->l = new_node(t); } t = t->l; } } t->cnt++; } void erase(unsigned val) { node *t = root, *ne; for (int i = 30; i >= 0; i--) { t->cnt--; if (val & (1 << i)) { if (t->r == nullptr) return; t = t->r; } else { if (t->l == nullptr) return; t = t->l; } } t->cnt--; while (t != root && t->cnt == 0) { ne = t->p; (t == ne->l ? ne->l : ne->r) = nullptr; t = ne; } } bool find(unsigned val) { node *t = root; for (int i = 30; i >= 0; i--) { if (val & (1 << i)) { if (t->r == nullptr) return false; t = t->r; } else { if (t->l == nullptr) return false; t = t->l; } } return true; } unsigned max() { assert(root->cnt >= 1); node *t = root; unsigned res = 0; for (int i = 30; i >= 0; i--) { assert(t->l != nullptr || t->r != nullptr); if (t->r != nullptr) { res |= 1 << i; t = t->r; } else { t = t->l; } } return res; } unsigned min() { assert(root->cnt >= 1); node *t = root; unsigned res = 0; for (int i = 30; i >= 0; i--) { assert(t->l != nullptr || t->r != nullptr); if (t->l != nullptr) { t = t->l; } else { res |= 1 << i; t = t->r; } } return res; } }; int main() { cin.sync_with_stdio(false); uint_Trie ut; string com; int k; while (cin >> com, com != "end") { if (com == "insert") { cin >> k; ut.insert(k); } else { int res = ut.max(); printf("%d\n", res); ut.erase(res); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { node *p, *l, *r; int cnt; void Init(node *p_) { p = p_; l = r = nullptr; cnt = 0; } }; int it; node pool[2000000]; node *new_node(node *p) { pool[it].Init(p); return &pool[it++]; } class uint_Trie { node *root; int siz; public: uint_Trie() : root(new_node(nullptr)) {} int size() { return root->cnt; } void insert(unsigned val) { // if (find(val)) return; // multiset ???????¶???? node *t = root; for (int i = 30; i >= 0; i--) { t->cnt++; if (val & (1 << i)) { if (t->r == nullptr) { t->r = new_node(t); } t = t->r; } else { if (t->l == nullptr) { t->l = new_node(t); } t = t->l; } } t->cnt++; } void erase(unsigned val) { node *t = root, *ne; for (int i = 30; i >= 0; i--) { t->cnt--; if (val & (1 << i)) { if (t->r == nullptr) return; t = t->r; } else { if (t->l == nullptr) return; t = t->l; } } t->cnt--; while (t != root && t->cnt == 0) { ne = t->p; (t == ne->l ? ne->l : ne->r) = nullptr; t = ne; } } bool find(unsigned val) { node *t = root; for (int i = 30; i >= 0; i--) { if (val & (1 << i)) { if (t->r == nullptr) return false; t = t->r; } else { if (t->l == nullptr) return false; t = t->l; } } return true; } unsigned max() { assert(root->cnt >= 1); node *t = root; unsigned res = 0; for (int i = 30; i >= 0; i--) { assert(t->l != nullptr || t->r != nullptr); if (t->r != nullptr) { res |= 1 << i; t = t->r; } else { t = t->l; } } return res; } unsigned min() { assert(root->cnt >= 1); node *t = root; unsigned res = 0; for (int i = 30; i >= 0; i--) { assert(t->l != nullptr || t->r != nullptr); if (t->l != nullptr) { t = t->l; } else { res |= 1 << i; t = t->r; } } return res; } }; int main() { cin.sync_with_stdio(false); uint_Trie ut; string com; int k; while (cin >> com, com != "end") { if (com == "insert") { cin >> k; ut.insert(k); } else { int res = ut.max(); printf("%d\n", res); ut.erase(res); } } return 0; }
replace
14
15
14
15
0
p02289
C++
Runtime Error
#include <iostream> #include <stdio.h> #define MAX_NODES 2000000 #define INFTY 2000000010 using namespace std; // think easy make easy // think stupid int n = 0; // num of nodes // iを根とするMaxヒープをつくる関数.初期状態はiを根とする二分木 void maxHeapify(int A[], int i) { // i is index of root int l, r; int largest; int tmp; l = 2 * i; r = l + 1; if (l <= n && A[l] > A[i]) largest = l; else largest = i; if (r <= n && A[r] > A[largest]) largest = r; if (largest != i) { tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(A, largest); } } void buildMaxHeap(int A[]) { // AをMaxヒープにする関数 for (int i = n / 2; i > 0; i--) { // 1~n/2が左右の子少なくとも一方が存在する範囲 maxHeapify(A, i); } } void heapIncreasingKey(int A[]) { int tmp; int tmp_n; for (int i = n; i > 1; i = i / 2) { if (A[i / 2] < A[i]) { tmp = A[i]; A[i] = A[i / 2]; A[i / 2] = tmp; } else break; } } void insert(int A[], int num) { n++; A[n] = num; heapIncreasingKey(A); } void extract(int A[]) { cout << A[1] << endl; n--; A[1] = A[n]; n--; maxHeapify(A, 1); } void swap() {} int main() { // 1オリジン(添え字が1からはじまる)のためナル文字分の+1に加えさらに+1する int H[MAX_NODES + 1 + 1]; int num; char type[100]; while (true) { scanf("%s", &type); if (type[0] == 'i') { scanf("%d", &num); insert(H, num); } else if (type[1] == 'x') { extract(H); } else break; } return 0; }
#include <iostream> #include <stdio.h> #define MAX_NODES 2000000 #define INFTY 2000000010 using namespace std; // think easy make easy // think stupid int n = 0; // num of nodes // iを根とするMaxヒープをつくる関数.初期状態はiを根とする二分木 void maxHeapify(int A[], int i) { // i is index of root int l, r; int largest; int tmp; l = 2 * i; r = l + 1; if (l <= n && A[l] > A[i]) largest = l; else largest = i; if (r <= n && A[r] > A[largest]) largest = r; if (largest != i) { tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(A, largest); } } void buildMaxHeap(int A[]) { // AをMaxヒープにする関数 for (int i = n / 2; i > 0; i--) { // 1~n/2が左右の子少なくとも一方が存在する範囲 maxHeapify(A, i); } } void heapIncreasingKey(int A[]) { int tmp; int tmp_n; for (int i = n; i > 1; i = i / 2) { if (A[i / 2] < A[i]) { tmp = A[i]; A[i] = A[i / 2]; A[i / 2] = tmp; } else break; } } void insert(int A[], int num) { n++; A[n] = num; heapIncreasingKey(A); } void extract(int A[]) { cout << A[1] << endl; A[1] = A[n]; n--; maxHeapify(A, 1); } void swap() {} int main() { // 1オリジン(添え字が1からはじまる)のためナル文字分の+1に加えさらに+1する int H[MAX_NODES + 1 + 1]; int num; char type[100]; while (true) { scanf("%s", &type); if (type[0] == 'i') { scanf("%d", &num); insert(H, num); } else if (type[1] == 'x') { extract(H); } else break; } return 0; }
delete
64
65
64
64
0
p02289
C++
Runtime Error
#include <algorithm> #include <iostream> #define MAX 1000000 using namespace std; int n; int parent(int i); int left(int i); int right(int i); void maxHeap(int node[], int i); void buildMaxHeap(int node[]); void insert(int node[], int k); int extractMax(int node[]); int main() { int k, node[MAX]; char com[10]; n = 0; while (1) { cin >> com; if (com[2] == 'd') break; else if (com[0] == 'e') cout << extractMax(node) << endl; else { cin >> k; insert(node, k); } } return 0; } int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeap(int node[], int i) { int r, l, largest; if (left(i) <= n) l = left(i); else l = i; if (right(i) <= n) r = right(i); else r = i; if (node[l] < node[r]) largest = r; else largest = l; if (node[largest] < node[i]) largest = i; if (largest != i) { swap(node[i], node[largest]); maxHeap(node, largest); } } void buildMaxHeap(int node[]) { int i; for (i = n / 2; i > 0; i--) maxHeap(node, i); } void insert(int node[], int k) { int i; node[++n] = k; i = n; while (i > 1 && node[i] > node[i / 2]) { swap(node[i], node[i / 2]); i /= 2; } } int extractMax(int node[]) { int max; max = node[1]; node[1] = node[n--]; maxHeap(node, 1); return max; }
#include <algorithm> #include <iostream> #define MAX 2000000 using namespace std; int n; int parent(int i); int left(int i); int right(int i); void maxHeap(int node[], int i); void buildMaxHeap(int node[]); void insert(int node[], int k); int extractMax(int node[]); int main() { int k, node[MAX]; char com[10]; n = 0; while (1) { cin >> com; if (com[2] == 'd') break; else if (com[0] == 'e') cout << extractMax(node) << endl; else { cin >> k; insert(node, k); } } return 0; } int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeap(int node[], int i) { int r, l, largest; if (left(i) <= n) l = left(i); else l = i; if (right(i) <= n) r = right(i); else r = i; if (node[l] < node[r]) largest = r; else largest = l; if (node[largest] < node[i]) largest = i; if (largest != i) { swap(node[i], node[largest]); maxHeap(node, largest); } } void buildMaxHeap(int node[]) { int i; for (i = n / 2; i > 0; i--) maxHeap(node, i); } void insert(int node[], int k) { int i; node[++n] = k; i = n; while (i > 1 && node[i] > node[i / 2]) { swap(node[i], node[i / 2]); i /= 2; } } int extractMax(int node[]) { int max; max = node[1]; node[1] = node[n--]; maxHeap(node, 1); return max; }
replace
2
3
2
3
0
p02289
C++
Runtime Error
#include <iostream> #include <limits.h> #include <string> using namespace std; long int A[500000]; int n; int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void heapIncreaseKey(int i, int key) { if (key < A[i]) return; A[i] = key; while (i > 1 && A[parent(i)] < A[i]) { swap(A[i], A[parent(i)]); i = parent(i); } } void maxHeapInsert(int key) { n++; A[n] = INT_MIN; heapIncreaseKey(n, key); } void maxHeapify(int); int heapExtractMax() { int maxi; if (n < 1) return -1; maxi = A[1]; A[1] = A[n]; n--; maxHeapify(1); return maxi; } void maxHeapify(int i) { int l = left(i); int r = right(i); int large; if (l <= n && A[l] > A[i]) { large = l; } else { large = i; } if (r <= n && A[r] > A[large]) { large = r; } if (large != i) { swap(A[i], A[large]); maxHeapify(large); } } void buildMaxHeap() { for (int i = n / 2; i > 0; i--) maxHeapify(i); } int main() { int id; string cmd; A[0] = 0; do { cin >> cmd; if (cmd == "insert") { cin >> id; maxHeapInsert(id); } if (cmd == "extract") { cout << heapExtractMax() << endl; ; } } while (cmd != "end"); return 0; }
#include <iostream> #include <limits.h> #include <string> using namespace std; long int A[2000000]; int n; int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void heapIncreaseKey(int i, int key) { if (key < A[i]) return; A[i] = key; while (i > 1 && A[parent(i)] < A[i]) { swap(A[i], A[parent(i)]); i = parent(i); } } void maxHeapInsert(int key) { n++; A[n] = INT_MIN; heapIncreaseKey(n, key); } void maxHeapify(int); int heapExtractMax() { int maxi; if (n < 1) return -1; maxi = A[1]; A[1] = A[n]; n--; maxHeapify(1); return maxi; } void maxHeapify(int i) { int l = left(i); int r = right(i); int large; if (l <= n && A[l] > A[i]) { large = l; } else { large = i; } if (r <= n && A[r] > A[large]) { large = r; } if (large != i) { swap(A[i], A[large]); maxHeapify(large); } } void buildMaxHeap() { for (int i = n / 2; i > 0; i--) maxHeapify(i); } int main() { int id; string cmd; A[0] = 0; do { cin >> cmd; if (cmd == "insert") { cin >> id; maxHeapInsert(id); } if (cmd == "extract") { cout << heapExtractMax() << endl; ; } } while (cmd != "end"); return 0; }
replace
4
5
4
5
0
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL int main() { priority_queue<int> PQ; while (true) { string command; cin >> command; if (command[0] == 'i') { int key; scanf("%d", &key); PQ.push(key); } else if (command[1] == 'x') { printf("%d\n", PQ.top()); PQ.pop(); } else { break; } } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL int main() { priority_queue<int> PQ; while (true) { char command[10]; cin >> command; if (command[0] == 'i') { int key; scanf("%d", &key); PQ.push(key); } else if (command[1] == 'x') { printf("%d\n", PQ.top()); PQ.pop(); } else { break; } } return 0; }
replace
27
28
27
28
TLE
p02289
C++
Runtime Error
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; #define NUM 100000 #define NIL -1 int A[NUM]; int heap_n = 0; void init() { for (int i = 0; i < NUM; i++) { A[i] = NIL; } } void upHeapify(int num) { if (num == 1) { return; } if (A[num] > A[num / 2]) { int tmp = A[num]; A[num] = A[num / 2]; A[num / 2] = tmp; upHeapify(num / 2); } } void downHeapify(int num) { int m = max(A[num], max(A[num * 2], A[num * 2 + 1])); if (m == A[num * 2]) { swap(A[num * 2], A[num]); downHeapify(num * 2); return; } if (m == A[num * 2 + 1]) { swap(A[num * 2 + 1], A[num]); downHeapify(num * 2 + 1); return; } } void insert_(int num) { A[heap_n + 1] = num; heap_n++; upHeapify(heap_n); } void pop_() { A[1] = A[heap_n]; A[heap_n] = NIL; heap_n--; downHeapify(1); } int main(void) { init(); char buf[100]; int num; while (1) { scanf("%s", buf); if (!strcmp(buf, "end")) { break; } if (!strcmp(buf, "insert")) { scanf("%d", &num); insert_(num); } if (!strcmp(buf, "extract")) { printf("%d\n", A[1]); pop_(); } if (!strcmp(buf, "print")) { for (int i = 1; i <= heap_n; i++) { printf("%d ", A[i]); } } } }
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; #define NUM 4000000 #define NIL -1 int A[NUM]; int heap_n = 0; void init() { for (int i = 0; i < NUM; i++) { A[i] = NIL; } } void upHeapify(int num) { if (num == 1) { return; } if (A[num] > A[num / 2]) { int tmp = A[num]; A[num] = A[num / 2]; A[num / 2] = tmp; upHeapify(num / 2); } } void downHeapify(int num) { int m = max(A[num], max(A[num * 2], A[num * 2 + 1])); if (m == A[num * 2]) { swap(A[num * 2], A[num]); downHeapify(num * 2); return; } if (m == A[num * 2 + 1]) { swap(A[num * 2 + 1], A[num]); downHeapify(num * 2 + 1); return; } } void insert_(int num) { A[heap_n + 1] = num; heap_n++; upHeapify(heap_n); } void pop_() { A[1] = A[heap_n]; A[heap_n] = NIL; heap_n--; downHeapify(1); } int main(void) { init(); char buf[100]; int num; while (1) { scanf("%s", buf); if (!strcmp(buf, "end")) { break; } if (!strcmp(buf, "insert")) { scanf("%d", &num); insert_(num); } if (!strcmp(buf, "extract")) { printf("%d\n", A[1]); pop_(); } if (!strcmp(buf, "print")) { for (int i = 1; i <= heap_n; i++) { printf("%d ", A[i]); } } } }
replace
6
7
6
7
-11
p02289
C++
Time Limit Exceeded
#include <climits> #include <iostream> #include <vector> using namespace std; class Heap { private: public: Heap() { node.push_back(0); }; void print() { long n = node.size(); for (long i = 1; i < n; ++i) cout << " " << node[i]; cout << endl; }; void insert(long num) { node.push_back(num); }; long left(long i) { return (2 * i); }; long right(long i) { return (2 * i + 1); }; long parent(long i) { return (i / 2); }; void maxHeapify(long i) { long l = left(i); long r = right(i); long nodeSize = node.size() - 1; long largest = 0; if (l <= nodeSize && node[l] > node[i]) { largest = l; } else { largest = i; } if (r <= nodeSize && node[r] > node[largest]) largest = r; if (largest != i) { long buf = node[i]; node[i] = node[largest]; node[largest] = buf; maxHeapify(largest); } }; void buildMaxHeap() { long nodeSize = node.size() - 1; for (long i = nodeSize; i > 0; --i) maxHeapify(i); }; long getNodesize() { return node.size(); }; protected: vector<long> node; }; class PriorityQueue : public Heap { private: void heapIncreaseKey(long i, long num) { node[i] = num; while (i > 1 && (node[parent(i)] < node[i])) { long pi = parent(i); long buf = node[i]; node[i] = node[pi]; node[pi] = buf; i = pi; } }; public: void maxHeapInsert(long num) { insert(LONG_MIN); long i = getNodesize() - 1; heapIncreaseKey(i, num); }; long extractMax() { long heapSize = getNodesize() - 1; if (heapSize < 1) return LONG_MIN; long max = node[1]; node[1] = node[heapSize]; node.pop_back(); maxHeapify(1); return max; }; protected: }; int main() { PriorityQueue priorityQueue; do { string str = ""; cin >> str; if (str == "insert") { long num = 0; cin >> num; priorityQueue.maxHeapInsert(num); } else if (str == "extract") { cout << priorityQueue.extractMax() << endl; } else if (str == "end") { break; } } while (true); return 0; }
#include <climits> #include <iostream> #include <vector> using namespace std; class Heap { private: public: Heap() { node.push_back(0); }; void print() { long n = node.size(); for (long i = 1; i < n; ++i) cout << " " << node[i]; cout << endl; }; void insert(long num) { node.push_back(num); }; long left(long i) { return (2 * i); }; long right(long i) { return (2 * i + 1); }; long parent(long i) { return (i / 2); }; void maxHeapify(long i) { long l = left(i); long r = right(i); long nodeSize = node.size() - 1; long largest = 0; if (l <= nodeSize && node[l] > node[i]) { largest = l; } else { largest = i; } if (r <= nodeSize && node[r] > node[largest]) largest = r; if (largest != i) { long buf = node[i]; node[i] = node[largest]; node[largest] = buf; maxHeapify(largest); } }; void buildMaxHeap() { long nodeSize = node.size() - 1; for (long i = nodeSize; i > 0; --i) maxHeapify(i); }; long getNodesize() { return node.size(); }; protected: vector<long> node; }; class PriorityQueue : public Heap { private: void heapIncreaseKey(long i, long num) { node[i] = num; while (i > 1 && (node[parent(i)] < node[i])) { long pi = parent(i); long buf = node[i]; node[i] = node[pi]; node[pi] = buf; i = pi; } }; public: void maxHeapInsert(long num) { insert(LONG_MIN); long i = getNodesize() - 1; heapIncreaseKey(i, num); }; long extractMax() { long heapSize = getNodesize() - 1; if (heapSize < 1) return LONG_MIN; long max = node[1]; node[1] = node[heapSize]; node.pop_back(); maxHeapify(1); return max; }; protected: }; int main() { ios::sync_with_stdio(false); cin.tie(0); PriorityQueue priorityQueue; do { string str = ""; cin >> str; if (str == "insert") { long num = 0; cin >> num; priorityQueue.maxHeapInsert(num); } else if (str == "extract") { cout << priorityQueue.extractMax() << endl; } else if (str == "end") { break; } } while (true); return 0; }
insert
95
95
95
98
TLE
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct H { vector<int> dat; void insert(int x) { int i = (int)dat.size(); dat.push_back(x); while (i > 0) { auto p = (i - 1) / 2; if (dat[p] < dat[i]) { swap(dat[p], dat[i]); i = p; } else { break; } } } int front() { return dat[0]; } void remove() { swap(dat[0], dat[dat.size() - 1]); dat.pop_back(); int len = (int)dat.size(); int i = 0; while (i * 2 + 1 < len) { if (i * 2 + 2 < len) { int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2); if (dat[i] < dat[j]) { swap(dat[i], dat[j]); i = j; } else { break; } } else { if (dat[i] < dat[i * 2 + 1]) { swap(dat[i], dat[i * 2 + 1]); i = i * 2 + 1; } } } } }; int main() { H h; while (1) { string s; cin >> s; if (s[0] == 'e') { if (s[1] == 'n') break; cout << h.front() << endl; h.remove(); } else { int x; cin >> x; h.insert(x); } } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct H { vector<int> dat; void insert(int x) { int i = (int)dat.size(); dat.push_back(x); while (i > 0) { auto p = (i - 1) / 2; if (dat[p] < dat[i]) { swap(dat[p], dat[i]); i = p; } else { break; } } } int front() { return dat[0]; } void remove() { swap(dat[0], dat[dat.size() - 1]); dat.pop_back(); int len = (int)dat.size(); int i = 0; while (i * 2 + 1 < len) { if (i * 2 + 2 < len) { int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2); if (dat[i] < dat[j]) { swap(dat[i], dat[j]); i = j; } else { break; } } else { if (dat[i] < dat[i * 2 + 1]) { swap(dat[i], dat[i * 2 + 1]); i = i * 2 + 1; } else { break; } } } } }; int main() { H h; while (1) { string s; cin >> s; if (s[0] == 'e') { if (s[1] == 'n') break; cout << h.front() << endl; h.remove(); } else { int x; cin >> x; h.insert(x); } } }
insert
40
40
40
42
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; int A[202020]; int H; // 1-indexed void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } void insert(int x) { int i = ++H; A[i] = x; // 親より大きかったら入れ替える while (i > 1 && A[i] > A[i / 2]) { swap(A[i], A[i / 2]); i /= 2; } } int extract() { int ret = A[1]; A[1] = A[H--]; maxHeapify(1); return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); string s; while (cin >> s && s != "end") { if (s == "insert") { int x; cin >> x; insert(x); } else { cout << extract() << endl; } } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; int A[2020200]; int H; // 1-indexed void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } void insert(int x) { int i = ++H; A[i] = x; // 親より大きかったら入れ替える while (i > 1 && A[i] > A[i / 2]) { swap(A[i], A[i / 2]); i /= 2; } } int extract() { int ret = A[1]; A[1] = A[H--]; maxHeapify(1); return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); string s; while (cin >> s && s != "end") { if (s == "insert") { int x; cin >> x; insert(x); } else { cout << extract() << endl; } } }
replace
23
24
23
24
0
p02289
C++
Runtime Error
#include <iostream> #include <queue> #include <string> using namespace std; int main() { priority_queue<int> pq; string ope; int num; cin >> ope; while (ope != "end") { if (ope == "extract") { cout << pq.top() << endl; pq.pop(); } else if (ope == "insert") { cin >> num; pq.push(num); } cin >> ope >> num; } return 0; }
#include <iostream> #include <queue> #include <string> using namespace std; int main() { priority_queue<int> pq; string ope; int num; cin >> ope; while (ope != "end") { if (ope == "extract") { cout << pq.top() << endl; pq.pop(); } else if (ope == "insert") { cin >> num; pq.push(num); } cin >> ope; } return 0; }
replace
19
20
19
20
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> struct Node { int index, val; bool operator<(Node n) { return this->val > n.val; } bool operator>(Node n) { return this->val < n.val; } Node() { this->index = 0; } }; class Min_heap { private: Node **n; //????????????????????? int N; //????°????index void swap(int a_index, int b_index) { Node *temp = n[b_index]; n[b_index] = n[a_index]; n[a_index] = temp; n[a_index]->index = a_index; n[b_index]->index = b_index; } public: Min_heap(int size) { this->n = new Node *[size + 1]; std::fill_n(n, size + 1, (Node *)NULL); this->N = 0; } void clear() { for (int i = 0; i < N; i++) { n[i]->index = 0; n[i] = NULL; } } ~Min_heap() { clear(); delete n; } bool isEmpty() { return N == 0; } void push(Node &e) { int i = ++N; n[i] = &e; e.index = i; while (i > 1) { if (*n[i / 2] > *n[i]) { swap(i / 2, i); i = i / 2; } else { break; } } } void remove(Node &a) { int index = a.index; swap(index, N); n[N]->index = 0; n[N--] = NULL; int i = index; while (i < N) { if (i * 2 + 1 <= N) { if (*n[i * 2] < *n[i * 2 + 1]) { if (*n[i] > *n[i * 2]) { swap(i, i * 2); i = i * 2; continue; } } else { if (*n[i] > *n[i * 2 + 1]) { swap(i, i * 2 + 1); i = i * 2 + 1; continue; } } } else if (i * 2 <= N) { if (*n[i] > *n[i * 2]) { swap(i, i * 2); i = i * 2; continue; } } break; } } Node *pop() { Node *p = n[1]; remove(*n[1]); return p; } }; int main(void) { char str[10]; Min_heap que = Min_heap(2000000); Node *V = new Node[2000000]; int i = 0; while (std::scanf("%s", str), str[2] != 'd') { if (str[0] == 'i') { int k; std::scanf("%d", &k); V[i].val = k; que.push(V[i]); } else { std::printf("%d\n", que.pop()->val); } i++; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> struct Node { int index, val; bool operator<(Node n) { return this->val > n.val; } bool operator>(Node n) { return this->val < n.val; } Node() { this->index = 0; } }; class Min_heap { private: Node **n; //????????????????????? int N; //????°????index void swap(int a_index, int b_index) { Node *temp = n[b_index]; n[b_index] = n[a_index]; n[a_index] = temp; n[a_index]->index = a_index; n[b_index]->index = b_index; } public: Min_heap(int size) { this->n = new Node *[size + 1]; std::fill_n(n, size + 1, (Node *)NULL); this->N = 0; } void clear() { for (int i = 0; i < N; i++) { n[i]->index = 0; n[i] = NULL; } } bool isEmpty() { return N == 0; } void push(Node &e) { int i = ++N; n[i] = &e; e.index = i; while (i > 1) { if (*n[i / 2] > *n[i]) { swap(i / 2, i); i = i / 2; } else { break; } } } void remove(Node &a) { int index = a.index; swap(index, N); n[N]->index = 0; n[N--] = NULL; int i = index; while (i < N) { if (i * 2 + 1 <= N) { if (*n[i * 2] < *n[i * 2 + 1]) { if (*n[i] > *n[i * 2]) { swap(i, i * 2); i = i * 2; continue; } } else { if (*n[i] > *n[i * 2 + 1]) { swap(i, i * 2 + 1); i = i * 2 + 1; continue; } } } else if (i * 2 <= N) { if (*n[i] > *n[i * 2]) { swap(i, i * 2); i = i * 2; continue; } } break; } } Node *pop() { Node *p = n[1]; remove(*n[1]); return p; } }; int main(void) { char str[10]; Min_heap que = Min_heap(2000000); Node *V = new Node[2000000]; int i = 0; while (std::scanf("%s", str), str[2] != 'd') { if (str[0] == 'i') { int k; std::scanf("%d", &k); V[i].val = k; que.push(V[i]); } else { std::printf("%d\n", que.pop()->val); } i++; } return 0; }
delete
34
38
34
34
0
p02289
C++
Time Limit Exceeded
//////////////////////////////////////// // ALDS1_9_A: Complete Binary Tree // ALDS1_9_B: Maximum Heap // ALDS1_9_C: Priority Queue //////////////////////////////////////// #include <algorithm> // next_permutation #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <numeric> //accumulate #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> // #include <unordered_map> //???????????\??¢??° #include <fstream> //ifstream, ofstream // #define NDEBUG //NDEBUG???#include // <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG????????????????????????????????????????????? #include <cassert> //assert using namespace std; //???????????°??????TEST????????????????????¬???????????????????????????????????¢???????????? #define TEST //******************************************************************************************************************************************* //?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬?????????????????????????????????????????? //??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨??? //????????????????????????cerr????????£???????????????????????????????????? #ifdef TEST #define dout cout #define din cin #else stringstream dummy; //???????????°??????dout????????????????????????????????? #define dout \ dummy.str(""); \ dummy.clear(stringstream::goodbit); \ dummy // dummy?????????????????????????????????????????¨?????¢?????¢??????????????? //???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? //http://d.hatena.ne.jp/linden/20060427/p1 #endif //?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢???????????? // #define OUTPUTFILE "output.txt" // //******************************************************************************************************************************************* #ifdef OUTPUTFILE #define dout \ outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§????????????????????? ofstream outputfile(OUTPUTFILE); #define OutputFilePath \ "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/" \ "output.txt" #endif //?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢???????????? // #define INPUTFILE "input.txt" // //******************************************************************************************************************************************* #ifdef INPUTFILE #define din inputfile ifstream inputfile(INPUTFILE); #endif #define disp(A) dout << #A << " = " << (A) << endl #define disP(A) \ dout << setw(3) << (A) << " " // << setw(3) ??????????????\???????????? #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define dispAll(A, n) \ dout << #A << " = "; \ rep(j, 0, (n)) { disP(A[j]); } \ dout << endl typedef pair<int, int> pii; typedef vector<int> vi; typedef unsigned long long ll; const int INF = 1e9 - 1; int maxHeap[2000001]; int H; int parentOf(int i) { return i / 2; } int leftChildOf(int i) { return i * 2; } int rightChildOf(int i) { return i * 2 + 1; } void maxHeapify(int i) { // dout << "---------\n"; // dout << "maxHeapify( " << i << " ) : " << "key = " << maxHeap[i] << // "\n"; int largest; int left = leftChildOf(i); int right = rightChildOf(i); // i,?????????,????????????3????????????????????§??????????????¢??? if (left <= H && maxHeap[left] > maxHeap[i]) largest = left; else largest = i; if (right <= H && maxHeap[right] > maxHeap[largest]) largest = right; if (largest != i) { //?????§????????????i??§???????????°???i??¨largest?????????swap?????????largest??\??????????§???? int tmp = maxHeap[i]; maxHeap[i] = maxHeap[largest]; maxHeap[largest] = tmp; maxHeapify(largest); // recursive call } } void buildMaxHeap() { for (int i = H / 2; i > 0; i--) { maxHeapify(i); } } void display() { string parentKeyStr; string leftKeyStr; string rightKeyStr; ostringstream oss; rep(i, 1, H + 1) { if (parentOf(i) > 0) { parentKeyStr = "parent key = "; oss << maxHeap[parentOf(i)]; parentKeyStr += oss.str(); parentKeyStr += ", "; oss.str(""); } else parentKeyStr = ""; if (leftChildOf(i) <= H) { leftKeyStr = "left key = "; oss << maxHeap[leftChildOf(i)]; leftKeyStr += oss.str(); leftKeyStr += ", "; oss.str(""); } else leftKeyStr = ""; if (rightChildOf(i) <= H) { rightKeyStr = "right key = "; oss << maxHeap[rightChildOf(i)]; rightKeyStr += oss.str(); rightKeyStr += ", "; oss.str(""); } else rightKeyStr = ""; dout << "node " << i << ": key = " << maxHeap[i] << ", " << parentKeyStr << leftKeyStr << rightKeyStr << endl; } } void insert(int x) { H++; maxHeap[H] = x; int i = H; while (i > 1 && maxHeap[parentOf(i)] < maxHeap[i]) { swap(maxHeap[parentOf(i)], maxHeap[i]); i = parentOf(i); } } int main() { H = 0; string command; int x; while (1) { // dout << "---------------------------------\n"; din >> command; if (command[0] == 'i') { cin >> x; // dout << "insert " << x << endl; insert(x); // maxHeap[++H] = x; // buildMaxHeap(); } else if (command[1] == 'x') { // dout << "extract " << endl; dout << maxHeap[1] << endl; //????°????????´??????????????????£?????????????§???? maxHeap[1] = maxHeap[H--]; buildMaxHeap(); } else { break; } // disp(H); // dispAll(maxHeap, 20); // display(); } #ifdef INPUTFILE inputfile.close(); #endif #ifdef OUTPUTFILE outputfile.close(); cout << "\"" << OutputFilePath << "\"" << endl; #endif return 0; }
//////////////////////////////////////// // ALDS1_9_A: Complete Binary Tree // ALDS1_9_B: Maximum Heap // ALDS1_9_C: Priority Queue //////////////////////////////////////// #include <algorithm> // next_permutation #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <numeric> //accumulate #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> // #include <unordered_map> //???????????\??¢??° #include <fstream> //ifstream, ofstream // #define NDEBUG //NDEBUG???#include // <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG????????????????????????????????????????????? #include <cassert> //assert using namespace std; //???????????°??????TEST????????????????????¬???????????????????????????????????¢???????????? #define TEST //******************************************************************************************************************************************* //?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬?????????????????????????????????????????? //??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨??? //????????????????????????cerr????????£???????????????????????????????????? #ifdef TEST #define dout cout #define din cin #else stringstream dummy; //???????????°??????dout????????????????????????????????? #define dout \ dummy.str(""); \ dummy.clear(stringstream::goodbit); \ dummy // dummy?????????????????????????????????????????¨?????¢?????¢??????????????? //???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? //http://d.hatena.ne.jp/linden/20060427/p1 #endif //?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢???????????? // #define OUTPUTFILE "output.txt" // //******************************************************************************************************************************************* #ifdef OUTPUTFILE #define dout \ outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§????????????????????? ofstream outputfile(OUTPUTFILE); #define OutputFilePath \ "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/" \ "output.txt" #endif //?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢???????????? // #define INPUTFILE "input.txt" // //******************************************************************************************************************************************* #ifdef INPUTFILE #define din inputfile ifstream inputfile(INPUTFILE); #endif #define disp(A) dout << #A << " = " << (A) << endl #define disP(A) \ dout << setw(3) << (A) << " " // << setw(3) ??????????????\???????????? #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define dispAll(A, n) \ dout << #A << " = "; \ rep(j, 0, (n)) { disP(A[j]); } \ dout << endl typedef pair<int, int> pii; typedef vector<int> vi; typedef unsigned long long ll; const int INF = 1e9 - 1; int maxHeap[2000001]; int H; int parentOf(int i) { return i / 2; } int leftChildOf(int i) { return i * 2; } int rightChildOf(int i) { return i * 2 + 1; } void maxHeapify(int i) { // dout << "---------\n"; // dout << "maxHeapify( " << i << " ) : " << "key = " << maxHeap[i] << // "\n"; int largest; int left = leftChildOf(i); int right = rightChildOf(i); // i,?????????,????????????3????????????????????§??????????????¢??? if (left <= H && maxHeap[left] > maxHeap[i]) largest = left; else largest = i; if (right <= H && maxHeap[right] > maxHeap[largest]) largest = right; if (largest != i) { //?????§????????????i??§???????????°???i??¨largest?????????swap?????????largest??\??????????§???? int tmp = maxHeap[i]; maxHeap[i] = maxHeap[largest]; maxHeap[largest] = tmp; maxHeapify(largest); // recursive call } } void buildMaxHeap() { for (int i = H / 2; i > 0; i--) { maxHeapify(i); } } void display() { string parentKeyStr; string leftKeyStr; string rightKeyStr; ostringstream oss; rep(i, 1, H + 1) { if (parentOf(i) > 0) { parentKeyStr = "parent key = "; oss << maxHeap[parentOf(i)]; parentKeyStr += oss.str(); parentKeyStr += ", "; oss.str(""); } else parentKeyStr = ""; if (leftChildOf(i) <= H) { leftKeyStr = "left key = "; oss << maxHeap[leftChildOf(i)]; leftKeyStr += oss.str(); leftKeyStr += ", "; oss.str(""); } else leftKeyStr = ""; if (rightChildOf(i) <= H) { rightKeyStr = "right key = "; oss << maxHeap[rightChildOf(i)]; rightKeyStr += oss.str(); rightKeyStr += ", "; oss.str(""); } else rightKeyStr = ""; dout << "node " << i << ": key = " << maxHeap[i] << ", " << parentKeyStr << leftKeyStr << rightKeyStr << endl; } } void insert(int x) { H++; maxHeap[H] = x; int i = H; while (i > 1 && maxHeap[parentOf(i)] < maxHeap[i]) { swap(maxHeap[parentOf(i)], maxHeap[i]); i = parentOf(i); } } int main() { H = 0; string command; int x; while (1) { // dout << "---------------------------------\n"; din >> command; if (command[0] == 'i') { cin >> x; // dout << "insert " << x << endl; insert(x); // maxHeap[++H] = x; // buildMaxHeap(); } else if (command[1] == 'x') { // dout << "extract " << endl; dout << maxHeap[1] << endl; //????°????????´??????????????????£?????????????§???? maxHeap[1] = maxHeap[H--]; maxHeapify(1); } else { break; } // disp(H); // dispAll(maxHeap, 20); // display(); } #ifdef INPUTFILE inputfile.close(); #endif #ifdef OUTPUTFILE outputfile.close(); cout << "\"" << OutputFilePath << "\"" << endl; #endif return 0; }
replace
199
200
199
200
TLE
p02289
C++
Runtime Error
// AOJ Algorithms and Data Structures I - Heaps - Priority Queue // s 2013 0913 1556 // /* [ヒープを表す配列A、ヒープのサイズ(要素数)をheap_sizeとすれば、A[1...heap_size]にヒープの要素が格納される。木の根はA[1]であり、節点の添え字iが与えられたとき、その親parent(i)、左の子left(i)、右の子right(i)は以下の式で算出される: ] と、問題文に書いてあるので、添え字0は使わず、獲得する領域も1大きくする。 add 2013 0913 1714  要素数が決まってないので、動的配列で作りたいが、vectorは要素追加したときに アドレス変わっちゃうし、面倒なので、最初に静的に持っておく。 問題はそのサイズだが、命令数2bilion に合わせて、int型2billionの配列とったら 「Memory Limit : 65536 KB」なので完璧オーバー。 よって適当に10000KB取って実行後に足りなかったら、足す。  add 2013 0913 1556 ヒープは、最大(最少)を簡単に求められるデータ構造なので  優先度付きキュー(priority queue)にはぴったり。 計算機上でのジョブスケジューリング 特に、以下の関数が、いい味を出している。 => extractMax(S): 最大のキーを持つSの要素をSから削除してその値を返す そういえば、なんで普通のソートで並べておかないんだろ? それでも最大と最少は出るのに。 唯のソートなら、クイックソートとか使えばいいが、 木構造の探索の速さ(そこからくるinsertのはやさ)(これは普通にソートした配列でも同じ用にヒープにできるが) (あと、ヒープは探索まともにできない。挿入場所を早く決められるってだけ?) と、 値を取った後木を組みなおす簡単さが 一番うまいこといってるのが ヒープなんでしょう。 最大とか、最少のみがほしいときにしか使えないが add 2013 0913 1854 insert は とりあえず配列の後ろに入れて 親より大きかったら交換を繰り返して適切な位置へ。 (確かに挿入の適切な位置への探索はいらない。 木構造内での交換のみ) extractはrootを出して、そこに配列の一番後ろの要素入れて、要素数減らしてから、maxHeapifyでヒープ組み直し */ #include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; // 関数の引数に書くの面倒なので、ここで宣言 int heap_size; void maxHeapify(int *heap, int i) { int l, r, largest; l = i * 2; r = i * 2 + 1; if (l <= heap_size && heap[l] > heap[i]) largest = l; else largest = i; if (r <= heap_size && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(heap, largest); } } void buildMaxHeap(int *heap) { int i; for (i = heap_size / 2; i >= 1; i--) maxHeapify(heap, i); } void heapIncreaseKey(int *heap, int i, int key) { if (key < heap[i]) printf("error 新しいキーは現在のキーより小さい"); heap[i] = key; while (i > 1 && heap[i / 2] < heap[i]) { swap(heap[i], heap[i / 2]); i = i / 2; } } void maxHeapInsert(int *heap, int key) { heap_size++; heap[heap_size] = -1; // キーは非負なのでこれでOK heapIncreaseKey(heap, heap_size, key); } int heapExtractMax(int *heap) { int max; if (heap_size < 1) { printf("ヒープアンダーフロー"); return 0; } max = heap[1]; heap[1] = heap[heap_size]; heap_size--; maxHeapify(heap, 1); return max; } int main() { int i, j; // int heap_size; int key; string order; // input // 標準入力をファイルに変更//////////////////////////////////////////////////////// // FILE* fp_in = // freopen("\\\\k1300\\k1300\\s1350010\\.windows\\Desktop\\input_data.txt", // "r", stdin); ///////////////////////////////////////////////////////////////////////////////////// heap_size = 0; // int *heap = new int[heap_size+1]; //[1...heap_size]まで使うので int heap[3000000]; while (cin >> order) { if (order == "insert") { cin >> key; maxHeapInsert(heap, key); } else if (order == "extract") { printf("%d\n", heapExtractMax(heap)); } else { // if(order=="end"){ break; } } // ファイルを閉じる////////////////////////////////////////////////////////////////// // fclose(fp_in); ///////////////////////////////////////////////////////////////////////////////////// return 0; }
// AOJ Algorithms and Data Structures I - Heaps - Priority Queue // s 2013 0913 1556 // /* [ヒープを表す配列A、ヒープのサイズ(要素数)をheap_sizeとすれば、A[1...heap_size]にヒープの要素が格納される。木の根はA[1]であり、節点の添え字iが与えられたとき、その親parent(i)、左の子left(i)、右の子right(i)は以下の式で算出される: ] と、問題文に書いてあるので、添え字0は使わず、獲得する領域も1大きくする。 add 2013 0913 1714  要素数が決まってないので、動的配列で作りたいが、vectorは要素追加したときに アドレス変わっちゃうし、面倒なので、最初に静的に持っておく。 問題はそのサイズだが、命令数2bilion に合わせて、int型2billionの配列とったら 「Memory Limit : 65536 KB」なので完璧オーバー。 よって適当に10000KB取って実行後に足りなかったら、足す。  add 2013 0913 1556 ヒープは、最大(最少)を簡単に求められるデータ構造なので  優先度付きキュー(priority queue)にはぴったり。 計算機上でのジョブスケジューリング 特に、以下の関数が、いい味を出している。 => extractMax(S): 最大のキーを持つSの要素をSから削除してその値を返す そういえば、なんで普通のソートで並べておかないんだろ? それでも最大と最少は出るのに。 唯のソートなら、クイックソートとか使えばいいが、 木構造の探索の速さ(そこからくるinsertのはやさ)(これは普通にソートした配列でも同じ用にヒープにできるが) (あと、ヒープは探索まともにできない。挿入場所を早く決められるってだけ?) と、 値を取った後木を組みなおす簡単さが 一番うまいこといってるのが ヒープなんでしょう。 最大とか、最少のみがほしいときにしか使えないが add 2013 0913 1854 insert は とりあえず配列の後ろに入れて 親より大きかったら交換を繰り返して適切な位置へ。 (確かに挿入の適切な位置への探索はいらない。 木構造内での交換のみ) extractはrootを出して、そこに配列の一番後ろの要素入れて、要素数減らしてから、maxHeapifyでヒープ組み直し */ #include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; // 関数の引数に書くの面倒なので、ここで宣言 int heap_size; void maxHeapify(int *heap, int i) { int l, r, largest; l = i * 2; r = i * 2 + 1; if (l <= heap_size && heap[l] > heap[i]) largest = l; else largest = i; if (r <= heap_size && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(heap, largest); } } void buildMaxHeap(int *heap) { int i; for (i = heap_size / 2; i >= 1; i--) maxHeapify(heap, i); } void heapIncreaseKey(int *heap, int i, int key) { if (key < heap[i]) printf("error 新しいキーは現在のキーより小さい"); heap[i] = key; while (i > 1 && heap[i / 2] < heap[i]) { swap(heap[i], heap[i / 2]); i = i / 2; } } void maxHeapInsert(int *heap, int key) { heap_size++; heap[heap_size] = -1; // キーは非負なのでこれでOK heapIncreaseKey(heap, heap_size, key); } int heapExtractMax(int *heap) { int max; if (heap_size < 1) { printf("ヒープアンダーフロー"); return 0; } max = heap[1]; heap[1] = heap[heap_size]; heap_size--; maxHeapify(heap, 1); return max; } int main() { int i, j; // int heap_size; int key; string order; // input // 標準入力をファイルに変更//////////////////////////////////////////////////////// // FILE* fp_in = // freopen("\\\\k1300\\k1300\\s1350010\\.windows\\Desktop\\input_data.txt", // "r", stdin); ///////////////////////////////////////////////////////////////////////////////////// heap_size = 0; // int *heap = new int[heap_size+1]; //[1...heap_size]まで使うので int heap[2000000]; while (cin >> order) { if (order == "insert") { cin >> key; maxHeapInsert(heap, key); } else if (order == "extract") { printf("%d\n", heapExtractMax(heap)); } else { // if(order=="end"){ break; } } // ファイルを閉じる////////////////////////////////////////////////////////////////// // fclose(fp_in); ///////////////////////////////////////////////////////////////////////////////////// return 0; }
replace
126
127
126
127
-11
p02292
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; #define rep(i, a, b) for (int i = a; i <= b; ++i) #define dow(i, a, b) for (int i = a; i >= b; --i) #define mem(a) memset(a, 0, sizeof(a)) #define mst(a, b) memset(a, b, sizeof(a)) #define sfi(a) scanf("%d", &a) #define sfl(a) scanf("%lld", &a) #define sfd(a) scanf("%lf", &a) #define sfs(a) scanf("%s", a) #define pb(a) push_back(a) #define sync \ ios::sync_with_stdio(0); \ cin.tie(0); const int MAXN = 1000 + 5; const double EPS = 1e-6; const double PI = acos(-1.0); const int L = 2; const LL MOD = 100000 + 50; const int MAX = 100000 + 50; typedef struct Point Point; typedef struct Line Line; typedef struct Polygon Polygon; typedef struct Polygon_convex Polygon_convex; int cmp(double x); double sqr(double x); double det(Point a, Point b); double dot(Point a, Point b); double dist(Point a, Point b); Point rotate_point(Point p, double A); bool parallel(Line a, Line b); bool orthogonal(Line a, Line b); Point PointProjLine(Point p, Line l, Point &ans); Point reflect(Line l, Point p); double dis_point_segment(Point p, Line l); double dis_segment_segment(Line a, Line b); Line point_make_line(Point a, Point b); bool in_segment(Line l, Point p); int dcmp(double k); double mysqrt(double n); Point rotate(Point p, double cost, double sint); pair<Point, Point> crosspoint(Point ap, double ar, Point bp, double br); bool PointOnSegment(Point p, Point s, Point t); bool comp_less(Point a, Point b); Polygon_convex convex_hull(vector<Point> a); double convex_diameter(Polygon_convex a, int &First, int &Second); double min_dist(Point a[], int s[], int l, int r); double Min_Dist(Point a[], int s[], int n); struct Point { double x, y; Point() {} Point(double a, double b) : x(a), y(b) {} friend Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } friend Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); } friend bool operator==(Point a, Point b) { return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0; } friend Point operator*(Point a, double b) { return Point(a.x * b, a.y * b); } friend Point operator*(double a, Point b) { return Point(a * b.x, a * b.y); } friend Point operator/(Point a, double b) { return Point(a.x / b, a.y / b); } double norm() { return sqrt(sqr(x) + sqr(y)); } }; struct Line { Point a, b; Line() {} Line(Point x, Point y) : a(x), b(y) {} }; struct Polygon { int n; Point a[80050]; Polygon() {} double perimeter() { double sum = 0; a[n] = a[0]; rep(i, 0, n - 1) sum += (a[i + 1] - a[i]).norm(); return sum; } int Point_In(Point t) { int num = 0; a[n] = a[0]; rep(i, 0, n - 1) { if (PointOnSegment(t, a[i], a[i + 1])) return 2; int k = cmp(det(a[i + 1] - a[i], t - a[i])); int d1 = cmp(a[i].y - t.y); int d2 = cmp(a[i + 1].y - t.y); if (k > 0 && d1 <= 0 && d2 > 0) num++; if (k < 0 && d2 <= 0 && d1 > 0) num--; } return num != 0; } }; struct Polygon_convex { vector<Point> P; Polygon_convex(int Size = 0) { P.resize(Size); } }; bool comp_less(Point a, Point b) { return cmp(a.x - b.x) < 0 || cmp(a.x - b.x) == 0 && cmp(a.y - b.y) < 0; } Polygon_convex convex_hull(vector<Point> a) { Polygon_convex res(2 * a.size() + 5); sort(a.begin(), a.end(), comp_less); a.erase(unique(a.begin(), a.end()), a.end()); int m = 0; int len = a.size(); rep(i, 0, len - 1) { while (m > 1 && cmp(det(res.P[m - 1] - res.P[m - 2], a[i] - res.P[m - 2])) <= 0) --m; res.P[m++] = a[i]; } int k = m; len = a.size(); dow(i, len - 2, 0) { while (m > k && cmp(det(res.P[m - 1] - res.P[m - 2], a[i] - res.P[m - 2])) <= 0) --m; res.P[m++] = a[i]; } res.P.resize(m); if (a.size() > 1) res.P.resize(m - 1); return res; } bool PointOnSegment(Point p, Point s, Point t) { return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0; } bool parallel(Line a, Line b) { return !cmp(det(a.a - a.b, b.a - b.b)); } bool orthogonal(Line a, Line b) { return !cmp(dot(a.a - a.b, b.a - b.b)); } Point PointProjLine(Point p, Line l) { Point ans; double r = dot((l.b - l.a), (p - l.a)) / dot(l.b - l.a, l.b - l.a); ans = l.a + r * (l.b - l.a); return ans; } int cmp(double x) { if (abs(x) < EPS) return 0; if (x > 0) return 1; return -1; } double dis_point_segment(Point p, Line l) { if (cmp(dot(p - l.a, l.b - l.a)) < 0) return (p - l.a).norm(); if (cmp(dot(p - l.b, l.a - l.b)) < 0) return (p - l.b).norm(); return abs(det(l.a - p, l.b - p)) / dist(l.a, l.b); } bool line_make_point(Line a, Line b, Point &res) { if (parallel(a, b)) return false; double s1 = det(a.a - b.a, b.b - b.a); double s2 = det(a.b - b.a, b.b - b.a); res = (s1 * a.b - s2 * a.a) / (s1 - s2); return true; } double dis_segment_segment(Line a, Line b) { Point res; if (line_make_point(a, b, res) && in_segment(a, res) && in_segment(b, res)) return 0.; return min(min(dis_point_segment(b.a, a), dis_point_segment(b.b, a)), min(dis_point_segment(a.a, b), dis_point_segment(a.b, b))); } double sqr(double x) { return x * x; } double det(Point a, Point b) { return a.x * b.y - a.y * b.x; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double dist(Point a, Point b) { return (a - b).norm(); } Point rotate_point(Point p, double A) { double tx = p.x, ty = p.y; return Point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A)); } Point reflect(Line l, Point p) { return p + ((PointProjLine(p, l) - p) * 2.0); } bool in_segment(Line l, Point p) { bool fa = false, fb = false; if ((cmp(p.x - l.a.x) != -1 && cmp(p.x - l.b.x) != 1) || (cmp(p.x - l.a.x) != 1 && cmp(p.x - l.b.x) != -1)) fa = true; if ((cmp(p.y - l.a.y) != -1 && cmp(p.y - l.b.y) != 1) || (cmp(p.y - l.a.y) != 1 && cmp(p.y - l.b.y) != -1)) fb = true; return fa && fb; } int dcmp(double k) { return k < -EPS ? -1 : k > EPS ? 1 : 0; } double mysqrt(double n) { return sqrt(max(0.0, n)); } void circle_cross_line(Point a, Point b, Point o, double r, Point ret[], int &num) { double x0 = o.x, y0 = o.y; double x1 = a.x, y1 = a.y; double x2 = b.x, y2 = b.y; double dx = x2 - x1, dy = y2 - y1; double A = dx * dx + dy * dy; double B = 2 * dx * (x1 - x0) + 2 * dy * (y1 - y0); double C = sqr(x1 - x0) + sqr(y1 - y0) - sqr(r); double delta = B * B - 4 * A * C; num = 0; if (dcmp(delta) >= 0) { double t1 = (-B - mysqrt(delta)) / (2 * A); double t2 = (-B + mysqrt(delta)) / (2 * A); ret[num++] = Point(x1 + t1 * dx, y1 + t1 * dy); ret[num++] = Point(x1 + t2 * dx, y1 + t2 * dy); } } Point rotate(Point p, double cost, double sint) { double x = p.x, y = p.y; return Point(x * cost - y * sint, x * sint + y * cost); } pair<Point, Point> crosspoint(Point ap, double ar, Point bp, double br) { double d = (ap - bp).norm(); double cost = (ar * ar + d * d - br * br) / (2 * ar * d); double sint = sqrt(1. - cost * cost); Point v = (bp - ap) / (bp - ap).norm() * ar; return make_pair(ap + rotate(v, cost, -sint), ap + rotate(v, cost, sint)); } double convex_diameter(Polygon_convex a, int &First, int &Second) { vector<Point> &p = a.P; int n = p.size(); double maxd = 0.0; if (n == 1) { First = Second = 0; return maxd; } #define next(i) ((i + 1) % n) for (int i = 0, j = 1; i < n; ++i) { while (cmp(det(p[next(i)] - p[i], p[j] - p[i]) - det(p[next(i)] - p[i], p[next(j)] - p[i])) < 0) j = next(j); double d = dist(p[i], p[j]); if (d > maxd) { maxd = d; First = i, Second = j; } d = dist(p[next(i)], p[next(j)]); if (d > maxd) { maxd = d; First = i, Second = j; } } return maxd; } void ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (det(a, b) > EPS) puts("COUNTER_CLOCKWISE"); else if (det(a, b) < -EPS) puts("CLOCKWISE"); else if (dot(a, b) < -EPS) puts("ONLINE_BACK"); else if (a.norm() < b.norm()) puts("ONLINE_FRONT"); else puts("ON_SEGMENT"); } int main() { #ifdef LOCAL //~ freopen("in.txt", "r", stdin); #endif Point a, b, c; scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); int n; sfi(n); while (n - 1) { scanf("%lf%lf", &c.x, &c.y); ccw(a, b, c); } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; #define rep(i, a, b) for (int i = a; i <= b; ++i) #define dow(i, a, b) for (int i = a; i >= b; --i) #define mem(a) memset(a, 0, sizeof(a)) #define mst(a, b) memset(a, b, sizeof(a)) #define sfi(a) scanf("%d", &a) #define sfl(a) scanf("%lld", &a) #define sfd(a) scanf("%lf", &a) #define sfs(a) scanf("%s", a) #define pb(a) push_back(a) #define sync \ ios::sync_with_stdio(0); \ cin.tie(0); const int MAXN = 1000 + 5; const double EPS = 1e-6; const double PI = acos(-1.0); const int L = 2; const LL MOD = 100000 + 50; const int MAX = 100000 + 50; typedef struct Point Point; typedef struct Line Line; typedef struct Polygon Polygon; typedef struct Polygon_convex Polygon_convex; int cmp(double x); double sqr(double x); double det(Point a, Point b); double dot(Point a, Point b); double dist(Point a, Point b); Point rotate_point(Point p, double A); bool parallel(Line a, Line b); bool orthogonal(Line a, Line b); Point PointProjLine(Point p, Line l, Point &ans); Point reflect(Line l, Point p); double dis_point_segment(Point p, Line l); double dis_segment_segment(Line a, Line b); Line point_make_line(Point a, Point b); bool in_segment(Line l, Point p); int dcmp(double k); double mysqrt(double n); Point rotate(Point p, double cost, double sint); pair<Point, Point> crosspoint(Point ap, double ar, Point bp, double br); bool PointOnSegment(Point p, Point s, Point t); bool comp_less(Point a, Point b); Polygon_convex convex_hull(vector<Point> a); double convex_diameter(Polygon_convex a, int &First, int &Second); double min_dist(Point a[], int s[], int l, int r); double Min_Dist(Point a[], int s[], int n); struct Point { double x, y; Point() {} Point(double a, double b) : x(a), y(b) {} friend Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } friend Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); } friend bool operator==(Point a, Point b) { return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0; } friend Point operator*(Point a, double b) { return Point(a.x * b, a.y * b); } friend Point operator*(double a, Point b) { return Point(a * b.x, a * b.y); } friend Point operator/(Point a, double b) { return Point(a.x / b, a.y / b); } double norm() { return sqrt(sqr(x) + sqr(y)); } }; struct Line { Point a, b; Line() {} Line(Point x, Point y) : a(x), b(y) {} }; struct Polygon { int n; Point a[80050]; Polygon() {} double perimeter() { double sum = 0; a[n] = a[0]; rep(i, 0, n - 1) sum += (a[i + 1] - a[i]).norm(); return sum; } int Point_In(Point t) { int num = 0; a[n] = a[0]; rep(i, 0, n - 1) { if (PointOnSegment(t, a[i], a[i + 1])) return 2; int k = cmp(det(a[i + 1] - a[i], t - a[i])); int d1 = cmp(a[i].y - t.y); int d2 = cmp(a[i + 1].y - t.y); if (k > 0 && d1 <= 0 && d2 > 0) num++; if (k < 0 && d2 <= 0 && d1 > 0) num--; } return num != 0; } }; struct Polygon_convex { vector<Point> P; Polygon_convex(int Size = 0) { P.resize(Size); } }; bool comp_less(Point a, Point b) { return cmp(a.x - b.x) < 0 || cmp(a.x - b.x) == 0 && cmp(a.y - b.y) < 0; } Polygon_convex convex_hull(vector<Point> a) { Polygon_convex res(2 * a.size() + 5); sort(a.begin(), a.end(), comp_less); a.erase(unique(a.begin(), a.end()), a.end()); int m = 0; int len = a.size(); rep(i, 0, len - 1) { while (m > 1 && cmp(det(res.P[m - 1] - res.P[m - 2], a[i] - res.P[m - 2])) <= 0) --m; res.P[m++] = a[i]; } int k = m; len = a.size(); dow(i, len - 2, 0) { while (m > k && cmp(det(res.P[m - 1] - res.P[m - 2], a[i] - res.P[m - 2])) <= 0) --m; res.P[m++] = a[i]; } res.P.resize(m); if (a.size() > 1) res.P.resize(m - 1); return res; } bool PointOnSegment(Point p, Point s, Point t) { return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0; } bool parallel(Line a, Line b) { return !cmp(det(a.a - a.b, b.a - b.b)); } bool orthogonal(Line a, Line b) { return !cmp(dot(a.a - a.b, b.a - b.b)); } Point PointProjLine(Point p, Line l) { Point ans; double r = dot((l.b - l.a), (p - l.a)) / dot(l.b - l.a, l.b - l.a); ans = l.a + r * (l.b - l.a); return ans; } int cmp(double x) { if (abs(x) < EPS) return 0; if (x > 0) return 1; return -1; } double dis_point_segment(Point p, Line l) { if (cmp(dot(p - l.a, l.b - l.a)) < 0) return (p - l.a).norm(); if (cmp(dot(p - l.b, l.a - l.b)) < 0) return (p - l.b).norm(); return abs(det(l.a - p, l.b - p)) / dist(l.a, l.b); } bool line_make_point(Line a, Line b, Point &res) { if (parallel(a, b)) return false; double s1 = det(a.a - b.a, b.b - b.a); double s2 = det(a.b - b.a, b.b - b.a); res = (s1 * a.b - s2 * a.a) / (s1 - s2); return true; } double dis_segment_segment(Line a, Line b) { Point res; if (line_make_point(a, b, res) && in_segment(a, res) && in_segment(b, res)) return 0.; return min(min(dis_point_segment(b.a, a), dis_point_segment(b.b, a)), min(dis_point_segment(a.a, b), dis_point_segment(a.b, b))); } double sqr(double x) { return x * x; } double det(Point a, Point b) { return a.x * b.y - a.y * b.x; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double dist(Point a, Point b) { return (a - b).norm(); } Point rotate_point(Point p, double A) { double tx = p.x, ty = p.y; return Point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A)); } Point reflect(Line l, Point p) { return p + ((PointProjLine(p, l) - p) * 2.0); } bool in_segment(Line l, Point p) { bool fa = false, fb = false; if ((cmp(p.x - l.a.x) != -1 && cmp(p.x - l.b.x) != 1) || (cmp(p.x - l.a.x) != 1 && cmp(p.x - l.b.x) != -1)) fa = true; if ((cmp(p.y - l.a.y) != -1 && cmp(p.y - l.b.y) != 1) || (cmp(p.y - l.a.y) != 1 && cmp(p.y - l.b.y) != -1)) fb = true; return fa && fb; } int dcmp(double k) { return k < -EPS ? -1 : k > EPS ? 1 : 0; } double mysqrt(double n) { return sqrt(max(0.0, n)); } void circle_cross_line(Point a, Point b, Point o, double r, Point ret[], int &num) { double x0 = o.x, y0 = o.y; double x1 = a.x, y1 = a.y; double x2 = b.x, y2 = b.y; double dx = x2 - x1, dy = y2 - y1; double A = dx * dx + dy * dy; double B = 2 * dx * (x1 - x0) + 2 * dy * (y1 - y0); double C = sqr(x1 - x0) + sqr(y1 - y0) - sqr(r); double delta = B * B - 4 * A * C; num = 0; if (dcmp(delta) >= 0) { double t1 = (-B - mysqrt(delta)) / (2 * A); double t2 = (-B + mysqrt(delta)) / (2 * A); ret[num++] = Point(x1 + t1 * dx, y1 + t1 * dy); ret[num++] = Point(x1 + t2 * dx, y1 + t2 * dy); } } Point rotate(Point p, double cost, double sint) { double x = p.x, y = p.y; return Point(x * cost - y * sint, x * sint + y * cost); } pair<Point, Point> crosspoint(Point ap, double ar, Point bp, double br) { double d = (ap - bp).norm(); double cost = (ar * ar + d * d - br * br) / (2 * ar * d); double sint = sqrt(1. - cost * cost); Point v = (bp - ap) / (bp - ap).norm() * ar; return make_pair(ap + rotate(v, cost, -sint), ap + rotate(v, cost, sint)); } double convex_diameter(Polygon_convex a, int &First, int &Second) { vector<Point> &p = a.P; int n = p.size(); double maxd = 0.0; if (n == 1) { First = Second = 0; return maxd; } #define next(i) ((i + 1) % n) for (int i = 0, j = 1; i < n; ++i) { while (cmp(det(p[next(i)] - p[i], p[j] - p[i]) - det(p[next(i)] - p[i], p[next(j)] - p[i])) < 0) j = next(j); double d = dist(p[i], p[j]); if (d > maxd) { maxd = d; First = i, Second = j; } d = dist(p[next(i)], p[next(j)]); if (d > maxd) { maxd = d; First = i, Second = j; } } return maxd; } void ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (det(a, b) > EPS) puts("COUNTER_CLOCKWISE"); else if (det(a, b) < -EPS) puts("CLOCKWISE"); else if (dot(a, b) < -EPS) puts("ONLINE_BACK"); else if (a.norm() < b.norm()) puts("ONLINE_FRONT"); else puts("ON_SEGMENT"); } int main() { #ifdef LOCAL //~ freopen("in.txt", "r", stdin); #endif Point a, b, c; scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); int n; sfi(n); while (n--) { scanf("%lf%lf", &c.x, &c.y); ccw(a, b, c); } return 0; }
replace
320
321
320
321
TLE
p02294
C++
Time Limit Exceeded
/** * @brief 線分を扱います * @date 2016/03/20 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <complex> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** // #define GEOMETRY_BEGIN namespace geom { // #define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using elem_t = long double; using point_t = std::complex<elem_t>; using vec2_t = point_t; using geom_t = std::vector<point_t>; namespace limits { const auto pi = std::acos(-1.0); const auto eps = 1e-10; const auto inf = 1e12; } // namespace limits //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t det(A) 行列式|(a, b)| */ static inline elem_t det(const point_t &a, const point_t &b) { return std::real(a) * std::imag(b) - std::imag(a) * std::real(b); } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t a x b クロス積a x b */ static inline elem_t cross(const point_t &a, const point_t &b) { return std::real(a) * std::imag(b) - std::imag(a) * std::real(b); // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t a・b ドット積a・b */ static inline elem_t dot(const point_t &a, const point_t &b) { return std::real(a) * std::real(b) + std::imag(a) * std::imag(b); } /** * @brief 3点(pi, pj, pk)を引数に取り、クロス積(pk - pi) x (pj - pi)を返す * @note direction > epsのとき、cw(clockwise)...ただし、定義によってはccw * direction < * -epsのとき、ccw(counterclockwise)...ただし、定義によってはcw * それ以外のとき、0であり、境界条件が発生する. * このとき、ベクトルは同一直線上(colinear)にあり、 * それらの方向は同じか互いに逆である */ static inline elem_t direction(const point_t &pi, const point_t &pj, const point_t &pk) { return cross(pk - pi, pj - pi); } /** * @brief 絶対許容誤差(absolute tolerance)を比較します * * @note * 2つの浮動小数点数値が等しいかどうか比較するためのイプシロン許容誤差の利用は、 * イプシロンの値が固定されているので、絶対許容誤差(absolute * tolerance)と呼ばれている * 絶対許容誤差の欠点は適切なイプシロンの値を見つけるのが困難なことである * イプシロンの値は入力データの値の範囲、および使用している浮動小数点の形式に依存する * 浮動小数点数の範囲全体に対するイプシロンの値を1つだけ選ぶことは不可能である * xおよびyの値が非常に小さな(互いに等しくない)値の場合は、その差は常にイプシロンよりも小さくなる可能性があり、 * 逆に大きな値の場合は、その差はイプシロンよりも常に大きくなるかもしれない. * 別の見方として、 * 判定している数が大きくなればなるほど、絶対値による判定が成立するために必要な桁数はどんどん大きくなっていく * * @note * 固定されているイプシロンよりも数値が十分大きくなったとき、数値が正確に等しくない限り判定は常に失敗する * これは通常、意図したことではない. * 絶対許容誤差は数値の桁数の大きさが予めわかっており、 * 許容誤差の値をそれに応じて設定することができる場合にのみ利用するべきである */ static inline bool absolute_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps; } /** * @brief 相対許容誤差(relatice tolerance)を比較します * * @note * 基本的な考え方はある数を別の数によって除算し、その結果がどのくらい1に近づいているかを見るというものである * * * @note |x| <= |y|を仮定すると、判定は以下のようになる * if (Abs(x/y - 1.0) <= epsilon)... * これは以下のように書き直せる * if (Abs((x - y) / y) <= epsilon)... * コストのかかる除算を避け、ゼロによる除算のエラーから守るために、後者の式の両辺にAbs(y)を乗算して、以下のように単純化する * if (Abs(x - y) <= epsilon * Abs(y))... * 仮定|x| <= |y|を取り除くと、式は最終的に以下のようになる * if (Abs(x - y) <= epsilon * Max(Abs(x), Abs(y)))... // * 相対許容誤差の比較 * * * @note * 比較において相対的な判定は「より小さいか等しい」であり、「より小さい」ではないことは重要である * もしそうでなければ、両方の数が正確にゼロだった場合、判定は失敗する。相対的な判定も問題がないわけではない * 判定の式はAbs(x)およびAbs(y)が1よりも大きいときには、望み通りの働きをするが、それらの数値が1よりも小さいときは、 * イプシロンはより小さくないと効力がなくなってしまい、それらの数値が小さくなるほど式を成立させるのに必要な桁数はより多く必要になる */ static inline bool relative_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * std::max(std::fabs(x), std::fabs(y)); } /** * @brief 上記2つの判定を1つに結合させる * @note * 数値の絶対値が1よりも大きい場合には、相対的な判定を用い、1よりも小さい場合には、絶対的な判定を用いる * @attention * この式はMax()が機械語による命令によって利用できない場合には高価な計算になる可能性がある */ static inline bool combined_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * std::max({std::fabs(x), std::fabs(y), static_cast<elem_t>(1.0)}); } /** * @brief COMBINED-TOLERANCE-COMPAREより少ない労力で行える近似的な判定 */ static inline bool approximate_combined_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * (std::fabs(x) + std::fabs(y) + 1.0); } /** * @brief pkが|pipjの端点の間にあるか否かを判定する * * @note この手続きは、pkが線分|pipjと同一直線上にあると仮定する */ bool on_segment(const point_t &pi, const point_t &pj, const point_t &pk) { elem_t xi = pi.real(), xj = pj.real(), xk = pk.real(); elem_t yi = pi.imag(), yj = pj.imag(), yk = pk.imag(); return std::min(xi, xj) <= xk && xk <= std::max(xi, xj) && std::min(yi, yj) <= yk && yk <= std::max(yi, yj); } /** * @brief 2本の線分の交差判定 * * @note * 2本の線分の交差性を判定するために、各線分が他方を含む直線を跨ぐか否か調べる * 線分|p1p2がある直線を跨ぐ(straddle)のは、点p1がこの直線の一方の側にあり、 * 点p2が他方の側にあるときである. * 境界となるのは、p1かp2が直線上にある場合である * 2本の線分が交差するための必要十分条件は次の条件の一方(あるいは両方)が成り立つときである * * 1. どちらの線分も他方を含む直線を跨ぐ * 2. * 一方の線分の端点が線分上にある(この条件は境界上にある場合から発生する) * * @note このアイデアを次の手続きで実現する. * SEGMENT-INTERSECTは、線分|p1p2と線分|p3p4が交差するときに * TRUEを返し、そうでないときはFALSEを返す. * この手続きは、サブルーチンDIRECTIONを呼び出して * クロス積法を用いて相対的な方向を求め、ON-SEGMENTを呼び出して、線分を含む直線上にあることが分かっている点が * この線分上にあるかどうかを判定する */ bool segment_intersect(const point_t &p1, const point_t &p2, const point_t &p3, const point_t &p4) { elem_t d1 = direction(p3, p4, p1); elem_t d2 = direction(p3, p4, p2); elem_t d3 = direction(p1, p2, p3); elem_t d4 = direction(p1, p2, p4); // 線分↑p1p2と線分↑p3p4が互いに他方の直線を跨ぐ場合 if (((d1 > limits::eps && d2 < -limits::eps) || (d1 < -limits::eps && d2 > limits::eps)) && ((d3 > limits::eps && d4 < -limits::eps) || (d3 < -limits::eps && d4 > limits::eps))) { // |p1p2が|p3p4を含む直線を跨ぐから、クロス積(p1-p3)x(p2-p1)と(p4-p2)x(p2-p3)の符号は異なる // |p3p4が|p1p2を含む直線を跨ぐから、クロス積(p3-p1)x(p2-p1)と(p4-p1)x(p2-p1)の符号は異なる return true; } // そうではないとき、これらの線分が互いに他方を跨ぐことはないが、端点が他方の線分上にある余地は残る // どの相対的な方向も0でなければこの可能性は消える // ある相対的方向dkが0のときには、pkは他方の線分と同一直線上にある // pkがこの線分上にあるための必要十分条件は、これがこの線分の端点の間にあることである // ON-SEGMENT呼び出しにおいて、この線分は、第一引数を端点とする線分と異なる方の線分である else if (approximate_combined_tolerance_compare(d1, 0) && on_segment(p3, p4, p1)) { return true; } else if (approximate_combined_tolerance_compare(d2, 0) && on_segment(p3, p4, p2)) { return true; } else if (approximate_combined_tolerance_compare(d3, 0) && on_segment(p1, p2, p3)) { return true; } else if (approximate_combined_tolerance_compare(d4, 0) && on_segment(p1, p2, p4)) { return true; } else { return false; // 0判定はすべて失敗し、FALSEを返す } } int main() { using namespace std; int q; cin >> q; const int points_num = 4; while (q) { geom_t p(points_num); int x, y; for (int i = 0; i < points_num; i++) { cin >> x >> y; p[i] = point_t(x, y); } cout << segment_intersect(p[0], p[1], p[2], p[3]) << endl; } return 0; }
/** * @brief 線分を扱います * @date 2016/03/20 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <complex> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** // #define GEOMETRY_BEGIN namespace geom { // #define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using elem_t = long double; using point_t = std::complex<elem_t>; using vec2_t = point_t; using geom_t = std::vector<point_t>; namespace limits { const auto pi = std::acos(-1.0); const auto eps = 1e-10; const auto inf = 1e12; } // namespace limits //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t det(A) 行列式|(a, b)| */ static inline elem_t det(const point_t &a, const point_t &b) { return std::real(a) * std::imag(b) - std::imag(a) * std::real(b); } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t a x b クロス積a x b */ static inline elem_t cross(const point_t &a, const point_t &b) { return std::real(a) * std::imag(b) - std::imag(a) * std::real(b); // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const point_t& a ベクトルa * @param const point_t& b ベクトルb * @return elem_t a・b ドット積a・b */ static inline elem_t dot(const point_t &a, const point_t &b) { return std::real(a) * std::real(b) + std::imag(a) * std::imag(b); } /** * @brief 3点(pi, pj, pk)を引数に取り、クロス積(pk - pi) x (pj - pi)を返す * @note direction > epsのとき、cw(clockwise)...ただし、定義によってはccw * direction < * -epsのとき、ccw(counterclockwise)...ただし、定義によってはcw * それ以外のとき、0であり、境界条件が発生する. * このとき、ベクトルは同一直線上(colinear)にあり、 * それらの方向は同じか互いに逆である */ static inline elem_t direction(const point_t &pi, const point_t &pj, const point_t &pk) { return cross(pk - pi, pj - pi); } /** * @brief 絶対許容誤差(absolute tolerance)を比較します * * @note * 2つの浮動小数点数値が等しいかどうか比較するためのイプシロン許容誤差の利用は、 * イプシロンの値が固定されているので、絶対許容誤差(absolute * tolerance)と呼ばれている * 絶対許容誤差の欠点は適切なイプシロンの値を見つけるのが困難なことである * イプシロンの値は入力データの値の範囲、および使用している浮動小数点の形式に依存する * 浮動小数点数の範囲全体に対するイプシロンの値を1つだけ選ぶことは不可能である * xおよびyの値が非常に小さな(互いに等しくない)値の場合は、その差は常にイプシロンよりも小さくなる可能性があり、 * 逆に大きな値の場合は、その差はイプシロンよりも常に大きくなるかもしれない. * 別の見方として、 * 判定している数が大きくなればなるほど、絶対値による判定が成立するために必要な桁数はどんどん大きくなっていく * * @note * 固定されているイプシロンよりも数値が十分大きくなったとき、数値が正確に等しくない限り判定は常に失敗する * これは通常、意図したことではない. * 絶対許容誤差は数値の桁数の大きさが予めわかっており、 * 許容誤差の値をそれに応じて設定することができる場合にのみ利用するべきである */ static inline bool absolute_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps; } /** * @brief 相対許容誤差(relatice tolerance)を比較します * * @note * 基本的な考え方はある数を別の数によって除算し、その結果がどのくらい1に近づいているかを見るというものである * * * @note |x| <= |y|を仮定すると、判定は以下のようになる * if (Abs(x/y - 1.0) <= epsilon)... * これは以下のように書き直せる * if (Abs((x - y) / y) <= epsilon)... * コストのかかる除算を避け、ゼロによる除算のエラーから守るために、後者の式の両辺にAbs(y)を乗算して、以下のように単純化する * if (Abs(x - y) <= epsilon * Abs(y))... * 仮定|x| <= |y|を取り除くと、式は最終的に以下のようになる * if (Abs(x - y) <= epsilon * Max(Abs(x), Abs(y)))... // * 相対許容誤差の比較 * * * @note * 比較において相対的な判定は「より小さいか等しい」であり、「より小さい」ではないことは重要である * もしそうでなければ、両方の数が正確にゼロだった場合、判定は失敗する。相対的な判定も問題がないわけではない * 判定の式はAbs(x)およびAbs(y)が1よりも大きいときには、望み通りの働きをするが、それらの数値が1よりも小さいときは、 * イプシロンはより小さくないと効力がなくなってしまい、それらの数値が小さくなるほど式を成立させるのに必要な桁数はより多く必要になる */ static inline bool relative_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * std::max(std::fabs(x), std::fabs(y)); } /** * @brief 上記2つの判定を1つに結合させる * @note * 数値の絶対値が1よりも大きい場合には、相対的な判定を用い、1よりも小さい場合には、絶対的な判定を用いる * @attention * この式はMax()が機械語による命令によって利用できない場合には高価な計算になる可能性がある */ static inline bool combined_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * std::max({std::fabs(x), std::fabs(y), static_cast<elem_t>(1.0)}); } /** * @brief COMBINED-TOLERANCE-COMPAREより少ない労力で行える近似的な判定 */ static inline bool approximate_combined_tolerance_compare(elem_t x, elem_t y) { return std::fabs(x - y) <= limits::eps * (std::fabs(x) + std::fabs(y) + 1.0); } /** * @brief pkが|pipjの端点の間にあるか否かを判定する * * @note この手続きは、pkが線分|pipjと同一直線上にあると仮定する */ bool on_segment(const point_t &pi, const point_t &pj, const point_t &pk) { elem_t xi = pi.real(), xj = pj.real(), xk = pk.real(); elem_t yi = pi.imag(), yj = pj.imag(), yk = pk.imag(); return std::min(xi, xj) <= xk && xk <= std::max(xi, xj) && std::min(yi, yj) <= yk && yk <= std::max(yi, yj); } /** * @brief 2本の線分の交差判定 * * @note * 2本の線分の交差性を判定するために、各線分が他方を含む直線を跨ぐか否か調べる * 線分|p1p2がある直線を跨ぐ(straddle)のは、点p1がこの直線の一方の側にあり、 * 点p2が他方の側にあるときである. * 境界となるのは、p1かp2が直線上にある場合である * 2本の線分が交差するための必要十分条件は次の条件の一方(あるいは両方)が成り立つときである * * 1. どちらの線分も他方を含む直線を跨ぐ * 2. * 一方の線分の端点が線分上にある(この条件は境界上にある場合から発生する) * * @note このアイデアを次の手続きで実現する. * SEGMENT-INTERSECTは、線分|p1p2と線分|p3p4が交差するときに * TRUEを返し、そうでないときはFALSEを返す. * この手続きは、サブルーチンDIRECTIONを呼び出して * クロス積法を用いて相対的な方向を求め、ON-SEGMENTを呼び出して、線分を含む直線上にあることが分かっている点が * この線分上にあるかどうかを判定する */ bool segment_intersect(const point_t &p1, const point_t &p2, const point_t &p3, const point_t &p4) { elem_t d1 = direction(p3, p4, p1); elem_t d2 = direction(p3, p4, p2); elem_t d3 = direction(p1, p2, p3); elem_t d4 = direction(p1, p2, p4); // 線分↑p1p2と線分↑p3p4が互いに他方の直線を跨ぐ場合 if (((d1 > limits::eps && d2 < -limits::eps) || (d1 < -limits::eps && d2 > limits::eps)) && ((d3 > limits::eps && d4 < -limits::eps) || (d3 < -limits::eps && d4 > limits::eps))) { // |p1p2が|p3p4を含む直線を跨ぐから、クロス積(p1-p3)x(p2-p1)と(p4-p2)x(p2-p3)の符号は異なる // |p3p4が|p1p2を含む直線を跨ぐから、クロス積(p3-p1)x(p2-p1)と(p4-p1)x(p2-p1)の符号は異なる return true; } // そうではないとき、これらの線分が互いに他方を跨ぐことはないが、端点が他方の線分上にある余地は残る // どの相対的な方向も0でなければこの可能性は消える // ある相対的方向dkが0のときには、pkは他方の線分と同一直線上にある // pkがこの線分上にあるための必要十分条件は、これがこの線分の端点の間にあることである // ON-SEGMENT呼び出しにおいて、この線分は、第一引数を端点とする線分と異なる方の線分である else if (approximate_combined_tolerance_compare(d1, 0) && on_segment(p3, p4, p1)) { return true; } else if (approximate_combined_tolerance_compare(d2, 0) && on_segment(p3, p4, p2)) { return true; } else if (approximate_combined_tolerance_compare(d3, 0) && on_segment(p1, p2, p3)) { return true; } else if (approximate_combined_tolerance_compare(d4, 0) && on_segment(p1, p2, p4)) { return true; } else { return false; // 0判定はすべて失敗し、FALSEを返す } } int main() { using namespace std; int q; cin >> q; const int points_num = 4; while (q) { geom_t p(points_num); int x, y; for (int i = 0; i < points_num; i++) { cin >> x >> y; p[i] = point_t(x, y); } cout << segment_intersect(p[0], p[1], p[2], p[3]) << endl; q--; } return 0; }
insert
257
257
257
258
TLE
p02295
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define each(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define chmin(a, b) a = min(a, b) #define chmax(a, b) a = max(a, b) #define pb push_back #define mp make_pair #define X real() #define Y imag() const double EPS = 1e-12; const double INF = 1e12; const double PI = acos(-1.0); double SQ(double a) { return a * a; } bool EQ(double a, double b) { return abs(a - b) < EPS; } bool LT(double a, double b) { return a - b < -EPS; } bool LEQ(double a, double b) { return a - b < EPS; } double toRad(double t) { return t / 180 * PI; } double toDeg(double t) { return t * 180 / PI; } typedef complex<double> Point; namespace std { bool operator<(const Point &a, const Point &b) { if (a.X != b.X) return a.X < b.X; return a.Y < b.Y; } istream &operator>>(istream &is, Point &a) { double x, y; is >> x >> y; a = Point(x, y); return is; } ostream &operator<<(ostream &os, Point a) { return os << a.X << " " << a.Y; } } // namespace std struct Line { Point p1, p2; }; typedef Line Segment; struct Circle { Point p; double r; }; typedef vector<Point> Polygon; double norm(Point p) { return SQ(p.X) + SQ(p.Y); } double dot(Point a, Point b) { return (conj(a) * b).X; } double cross(Point a, Point b) { return (conj(a) * b).Y; } enum CCW { COUNTER_CLOCKWISE = 1, CLOCKWISE = -1, ONLINE_BACK = 2, ONLINE_FRONT = -2, ON_SEGMENT = 0, }; int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (LT(norm(a), norm(b))) return ONLINE_FRONT; return ON_SEGMENT; } bool orthogonal(Line l1, Line l2) { return EQ(dot(l1.p2 - l1.p1, l2.p2 - l2.p1), 0.0); } bool parallel(Line l1, Line l2) { return EQ(cross(l1.p2 - l1.p1, l2.p2 - l2.p1), 0.0); } Point project(Line l, Point p) { Point base = l.p2 - l.p1; double r = dot(p - l.p1, base) / norm(base); return l.p1 + base * r; } Point reflect(Line &l, Point &p) { return p + (project(l, p) - p) * 2.0; } bool sameLine(Line l1, Line l2) { return abs(cross(l1.p2 - l1.p1, l2.p1 - l1.p1)) < EPS; } bool intersectLP(Line l, Point p) { return LEQ(abs(cross(l.p1 - p, l.p2 - p)), 0.0); } bool intersectLL(Line l1, Line l2) { return !parallel(l1, l2) || sameLine(l1, l2); } bool intersectLS(Line l, Segment s) { return cross(l.p2 - l.p1, s.p1 - l.p1) * cross(l.p2 - l.p1, s.p2 - l.p1) < -EPS; } bool intersectSP(Segment s, Point p) { return norm(s.p1 - p) + norm(s.p2 - p) - norm(s.p2 - s.p1) < EPS; } bool intersectSS(Segment s1, Segment s2) { return ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 && ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0; } double distanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1)) / abs(l.p2 - l.p1); } double distanceLL(Line l1, Line l2) { if (intersectLL(l1, l2)) return 0.0; return distanceLP(l1, l2.p1); } double distanceLS(Line l, Segment s) { if (intersectLS(l, s)) return 0.0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return distanceLP(s, p); } double distanceSS(Segment s1, Segment s2) { if (intersectSS(s1, s2)) return 0.0; return min(min(distanceSP(s1, s2.p1), distanceSP(s1, s2.p2)), min(distanceSP(s2, s1.p1), distanceSP(s2, s1.p2))); } Point crossPointLL(Line l1, Line l2) { assert(intersectLL(l1, l2)); assert(!sameLine(l1, l2)); Point base = l2.p2 - l2.p1; double d1 = abs(cross(base, l1.p1 - l2.p1)); double d2 = abs(cross(base, l1.p2 - l2.p1)); double t = d1 / (d1 + d2); return l1.p1 + (l1.p2 - l1.p1) * t; } Point crossPointLS(Line l, Segment s) { assert(intersectLS(l, s)); return crossPointLL(l, s); } Point crossPointSS(Segment s1, Segment s2) { assert(intersectSS(s1, s2)); return crossPointLL(s1, s2); } enum { IN = 2, ON = 1, OUT = 0, }; int contains(const Polygon &g, Point p) { int n = g.size(); bool x = false; rep(i, n) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON; if (a.Y > b.Y) swap(a, b); if (a.Y < EPS && EPS < b.Y && cross(a, b) > EPS) x = !x; } return x ? IN : OUT; } Polygon convexHull(Polygon &s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.pb(s[0]); u.pb(s[1]); l.pb(s[s.size() - 1]); l.pb(s[s.size() - 2]); for (int i = 2; i < s.size(); i++) { int n = u.size(); while (n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE) { u.pop_back(); n--; } u.pb(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { int n = l.size(); while (n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE) { l.pop_back(); n--; } l.pb(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.pb(u[i]); return l; } int main() { cout << fixed << setprecision(20); int n; cin >> n; rep(i, n) { Segment s[2]; rep(j, 2) cin >> s[j].p1 >> s[j].p2; cout << crossPointSS(s[0], s[1]) << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define each(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define chmin(a, b) a = min(a, b) #define chmax(a, b) a = max(a, b) #define pb push_back #define mp make_pair #define X real() #define Y imag() const double EPS = 1e-12; const double INF = 1e12; const double PI = acos(-1.0); double SQ(double a) { return a * a; } bool EQ(double a, double b) { return abs(a - b) < EPS; } bool LT(double a, double b) { return a - b < -EPS; } bool LEQ(double a, double b) { return a - b < EPS; } double toRad(double t) { return t / 180 * PI; } double toDeg(double t) { return t * 180 / PI; } typedef complex<double> Point; namespace std { bool operator<(const Point &a, const Point &b) { if (a.X != b.X) return a.X < b.X; return a.Y < b.Y; } istream &operator>>(istream &is, Point &a) { double x, y; is >> x >> y; a = Point(x, y); return is; } ostream &operator<<(ostream &os, Point a) { return os << a.X << " " << a.Y; } } // namespace std struct Line { Point p1, p2; }; typedef Line Segment; struct Circle { Point p; double r; }; typedef vector<Point> Polygon; double norm(Point p) { return SQ(p.X) + SQ(p.Y); } double dot(Point a, Point b) { return (conj(a) * b).X; } double cross(Point a, Point b) { return (conj(a) * b).Y; } enum CCW { COUNTER_CLOCKWISE = 1, CLOCKWISE = -1, ONLINE_BACK = 2, ONLINE_FRONT = -2, ON_SEGMENT = 0, }; int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (LT(norm(a), norm(b))) return ONLINE_FRONT; return ON_SEGMENT; } bool orthogonal(Line l1, Line l2) { return EQ(dot(l1.p2 - l1.p1, l2.p2 - l2.p1), 0.0); } bool parallel(Line l1, Line l2) { return EQ(cross(l1.p2 - l1.p1, l2.p2 - l2.p1), 0.0); } Point project(Line l, Point p) { Point base = l.p2 - l.p1; double r = dot(p - l.p1, base) / norm(base); return l.p1 + base * r; } Point reflect(Line &l, Point &p) { return p + (project(l, p) - p) * 2.0; } bool sameLine(Line l1, Line l2) { return abs(cross(l1.p2 - l1.p1, l2.p1 - l1.p1)) < EPS; } bool intersectLP(Line l, Point p) { return LEQ(abs(cross(l.p1 - p, l.p2 - p)), 0.0); } bool intersectLL(Line l1, Line l2) { return !parallel(l1, l2) || sameLine(l1, l2); } bool intersectLS(Line l, Segment s) { return cross(l.p2 - l.p1, s.p1 - l.p1) * cross(l.p2 - l.p1, s.p2 - l.p1) < -EPS; } bool intersectSP(Segment s, Point p) { return norm(s.p1 - p) + norm(s.p2 - p) - norm(s.p2 - s.p1) < EPS; } bool intersectSS(Segment s1, Segment s2) { return ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 && ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0; } double distanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1)) / abs(l.p2 - l.p1); } double distanceLL(Line l1, Line l2) { if (intersectLL(l1, l2)) return 0.0; return distanceLP(l1, l2.p1); } double distanceLS(Line l, Segment s) { if (intersectLS(l, s)) return 0.0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return distanceLP(s, p); } double distanceSS(Segment s1, Segment s2) { if (intersectSS(s1, s2)) return 0.0; return min(min(distanceSP(s1, s2.p1), distanceSP(s1, s2.p2)), min(distanceSP(s2, s1.p1), distanceSP(s2, s1.p2))); } Point crossPointLL(Line l1, Line l2) { assert(intersectLL(l1, l2)); Point base = l2.p2 - l2.p1; double d1 = abs(cross(base, l1.p1 - l2.p1)); double d2 = abs(cross(base, l1.p2 - l2.p1)); double t = d1 / (d1 + d2); return l1.p1 + (l1.p2 - l1.p1) * t; } Point crossPointLS(Line l, Segment s) { assert(intersectLS(l, s)); return crossPointLL(l, s); } Point crossPointSS(Segment s1, Segment s2) { assert(intersectSS(s1, s2)); return crossPointLL(s1, s2); } enum { IN = 2, ON = 1, OUT = 0, }; int contains(const Polygon &g, Point p) { int n = g.size(); bool x = false; rep(i, n) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON; if (a.Y > b.Y) swap(a, b); if (a.Y < EPS && EPS < b.Y && cross(a, b) > EPS) x = !x; } return x ? IN : OUT; } Polygon convexHull(Polygon &s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.pb(s[0]); u.pb(s[1]); l.pb(s[s.size() - 1]); l.pb(s[s.size() - 2]); for (int i = 2; i < s.size(); i++) { int n = u.size(); while (n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE) { u.pop_back(); n--; } u.pb(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { int n = l.size(); while (n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE) { l.pop_back(); n--; } l.pb(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.pb(u[i]); return l; } int main() { cout << fixed << setprecision(20); int n; cin >> n; rep(i, n) { Segment s[2]; rep(j, 2) cin >> s[j].p1 >> s[j].p2; cout << crossPointSS(s[0], s[1]) << endl; } }
delete
159
160
159
159
0
p02299
C++
Time Limit Exceeded
// be naame khodaa #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef complex<ld> PT; typedef vector<PT> Poly; typedef pair<PT, PT> LS; #define F first #define S second #define X real() #define Y imag() #define pb push_back inline int in() { int x, y; y = scanf("%d", &x); return x; } const int N = -1; const ld EPS = 1e-12; const int LEFT = 0, RIGHT = 1, BACK = 2, FRONT = 3, ON = 4, IN = 5, OUT = 6; inline bool Geq(ld a, ld b) { return a + EPS > b; } inline bool Grt(ld a, ld b) { return a > b + EPS; } inline bool Leq(ld a, ld b) { return a < b + EPS; } inline bool Lss(ld a, ld b) { return a + EPS < b; } inline bool Equ(ld a, ld b) { return Geq(a, b) && Geq(b, a); } istream &operator>>(istream &is, complex<ld> &p) { ld val; is >> val; p.real(val); is >> val; p.imag(val); return is; } ld dot(PT a, PT b) { return real(conj(a) * b); } ld cross(PT a, PT b) { return imag(conj(a) * b); } ld sqlen(PT a) { return dot(a, a); } ld len(PT a) { return sqrt(sqlen(a)); } PT proj(PT a, PT b, PT c) { b -= a, c -= a; return a + b * real(c / b); } PT reflect(PT a, PT b, PT c) { b -= a, c -= a; return a + conj(c / b) * b; } PT rotate(PT a, PT b, ld theta) { return (b - a) * polar<ld>(1, theta) + a; } int relpos(PT a, PT b, PT c) { b -= a, c -= a; c /= b; if (Grt(c.imag(), 0)) return LEFT; if (Lss(c.imag(), 0)) return RIGHT; if (Lss(c.real(), 0)) return BACK; if (Grt(c.real(), 1)) return FRONT; return ON; } int side(PT a, PT b, PT c) { b -= a, c -= a; ld cr = (c / b).Y; return Grt(cr, 0) ? 1 : (Lss(cr, 0) ? -1 : 0); } // If LineSegments Intersect bool intersect(PT a, PT b, PT c, PT d) { int as = side(c, d, a), bs = side(c, d, b), cs = side(a, b, c), ds = side(a, b, d); if (as && as == bs || cs && cs == ds) return false; else if (as || bs || cs || ds) return true; for (int j = 0; j < 2; j++, swap(a, c), swap(b, d)) { ld mx = min(a.X, b.X), Mx = max(a.X, b.X), my = min(a.Y, b.Y), My = max(a.Y, b.Y); for (int k = 0; k < 2; k++, swap(c, d)) if (Geq(c.X, mx) && Leq(c.X, Mx) && Geq(c.Y, my) && Leq(c.Y, My)) return true; } return false; } // LineSegments Intersection (intersect is true) PT intersection(PT a, PT b, PT c, PT d) { ld c1 = cross(b - a, c - a), c2 = cross(b - a, d - a); return (c1 * d - c2 * c) / (c1 - c2); } ld distLSP(PT a, PT b, PT c) { int rpos = relpos(a, b, proj(a, b, c)); if (rpos == BACK) return len(c - a); if (rpos == FRONT) return len(c - b); b -= a, c -= a; return abs(cross(b, c) / len(b)); } ld distLS(PT a, PT b, PT c, PT d) { if (intersect(a, b, c, d)) return 0; return min(min(distLSP(a, b, c), distLSP(a, b, d)), min(distLSP(c, d, a), distLSP(c, d, b))); } ld signedArea(Poly &po) { int n = po.size(); ld res = 0; for (int i = 0; i < n; i++) res += cross(po[i], po[(i + 1) % n]); return res / 2; } ld area(Poly &poly) { return abs(signedArea(poly)); } bool isConvex(Poly &po) { int n = po.size(); bool neg = false, pos = false; for (int i = 0; i < n; i++) { int rpos = relpos(po[i], po[(i + 1) % n], po[(i + 2) % n]); if (rpos == LEFT) pos = true; if (rpos == RIGHT) neg = true; } return (neg & pos) == false; } int crossingN(Poly &po, PT a) { int n = po.size(); PT b = a; for (PT p : po) b.real(max(b.X, p.X)); int cn = 0; for (int i = 0; i < n; i++) { PT p = po[i], q = po[(i + 1) % n]; if (intersect(a, b, p, q) && (side(a, b, p) == 1 || side(a, b, q) == 1)) cn++; } return cn; } int windingN(Poly &po, PT a) { int n = po.size(); PT b = a; for (PT p : po) b.real(max(b.X, p.X)); int wn = 0; for (int i = 0; i < n; i++) { PT p = po[i], q = po[(i + 1) % n]; if (intersect(a, b, p, q)) { int ps = side(a, b, p), qs = side(a, b, q); if (ps == qs) continue; wn -= ps; } } return wn; } int pointInPoly(Poly &po, PT a) { int n = po.size(); for (int i = 0; i < n; i++) if (relpos(po[i], po[(i + 1) % n], a) == ON) return ON; return (windingN(po, a) ? IN : OUT); return (crossingN(po, a) % 2 ? IN : OUT); } int main() { Poly poly; for (int i = in(); i; i--) { PT p; cin >> p; poly.pb(p); } for (int i = in(); i; i--) { PT p; cin >> p; int s = pointInPoly(poly, p); cout << (s == IN ? 2 : (s == ON ? 1 : 0)) << endl; } return 0; }
// be naame khodaa #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef complex<ld> PT; typedef vector<PT> Poly; typedef pair<PT, PT> LS; #define F first #define S second #define X real() #define Y imag() #define pb push_back inline int in() { int x, y; y = scanf("%d", &x); return x; } const int N = -1; const ld EPS = 1e-12; const int LEFT = 0, RIGHT = 1, BACK = 2, FRONT = 3, ON = 4, IN = 5, OUT = 6; inline bool Geq(ld a, ld b) { return a + EPS > b; } inline bool Grt(ld a, ld b) { return a > b + EPS; } inline bool Leq(ld a, ld b) { return a < b + EPS; } inline bool Lss(ld a, ld b) { return a + EPS < b; } inline bool Equ(ld a, ld b) { return Geq(a, b) && Geq(b, a); } istream &operator>>(istream &is, complex<ld> &p) { ld val; is >> val; p.real(val); is >> val; p.imag(val); return is; } ld dot(PT a, PT b) { return real(conj(a) * b); } ld cross(PT a, PT b) { return imag(conj(a) * b); } ld sqlen(PT a) { return dot(a, a); } ld len(PT a) { return sqrt(sqlen(a)); } PT proj(PT a, PT b, PT c) { b -= a, c -= a; return a + b * real(c / b); } PT reflect(PT a, PT b, PT c) { b -= a, c -= a; return a + conj(c / b) * b; } PT rotate(PT a, PT b, ld theta) { return (b - a) * polar<ld>(1, theta) + a; } int relpos(PT a, PT b, PT c) { b -= a, c -= a; c /= b; if (Grt(c.imag(), 0)) return LEFT; if (Lss(c.imag(), 0)) return RIGHT; if (Lss(c.real(), 0)) return BACK; if (Grt(c.real(), 1)) return FRONT; return ON; } int side(PT a, PT b, PT c) { b -= a, c -= a; ld cr = (c / b).Y; return Grt(cr, 0) ? 1 : (Lss(cr, 0) ? -1 : 0); } // If LineSegments Intersect bool intersect(PT a, PT b, PT c, PT d) { int as = side(c, d, a), bs = side(c, d, b), cs = side(a, b, c), ds = side(a, b, d); if (as && as == bs || cs && cs == ds) return false; else if (as || bs || cs || ds) return true; for (int j = 0; j < 2; j++, swap(a, c), swap(b, d)) { ld mx = min(a.X, b.X), Mx = max(a.X, b.X), my = min(a.Y, b.Y), My = max(a.Y, b.Y); for (int k = 0; k < 2; k++, swap(c, d)) if (Geq(c.X, mx) && Leq(c.X, Mx) && Geq(c.Y, my) && Leq(c.Y, My)) return true; } return false; } // LineSegments Intersection (intersect is true) PT intersection(PT a, PT b, PT c, PT d) { ld c1 = cross(b - a, c - a), c2 = cross(b - a, d - a); return (c1 * d - c2 * c) / (c1 - c2); } ld distLSP(PT a, PT b, PT c) { int rpos = relpos(a, b, proj(a, b, c)); if (rpos == BACK) return len(c - a); if (rpos == FRONT) return len(c - b); b -= a, c -= a; return abs(cross(b, c) / len(b)); } ld distLS(PT a, PT b, PT c, PT d) { if (intersect(a, b, c, d)) return 0; return min(min(distLSP(a, b, c), distLSP(a, b, d)), min(distLSP(c, d, a), distLSP(c, d, b))); } ld signedArea(Poly &po) { int n = po.size(); ld res = 0; for (int i = 0; i < n; i++) res += cross(po[i], po[(i + 1) % n]); return res / 2; } ld area(Poly &poly) { return abs(signedArea(poly)); } bool isConvex(Poly &po) { int n = po.size(); bool neg = false, pos = false; for (int i = 0; i < n; i++) { int rpos = relpos(po[i], po[(i + 1) % n], po[(i + 2) % n]); if (rpos == LEFT) pos = true; if (rpos == RIGHT) neg = true; } return (neg & pos) == false; } int crossingN(Poly &po, PT a) { int n = po.size(); PT b = a; for (PT p : po) b.real(max(b.X, p.X)); int cn = 0; for (int i = 0; i < n; i++) { PT p = po[i], q = po[(i + 1) % n]; if (intersect(a, b, p, q) && (side(a, b, p) == 1 || side(a, b, q) == 1)) cn++; } return cn; } int windingN(Poly &po, PT a) { int n = po.size(); PT b = a; for (PT p : po) b.real(max(b.X, p.X)); int wn = 0; for (int i = 0; i < n; i++) { PT p = po[i], q = po[(i + 1) % n]; if (intersect(a, b, p, q)) { int ps = side(a, b, p), qs = side(a, b, q); if (qs >= 0) wn++; if (ps >= 0) wn--; } } return wn; } int pointInPoly(Poly &po, PT a) { int n = po.size(); for (int i = 0; i < n; i++) if (relpos(po[i], po[(i + 1) % n], a) == ON) return ON; return (windingN(po, a) ? IN : OUT); return (crossingN(po, a) % 2 ? IN : OUT); } int main() { Poly poly; for (int i = in(); i; i--) { PT p; cin >> p; poly.pb(p); } for (int i = in(); i; i--) { PT p; cin >> p; int s = pointInPoly(poly, p); cout << (s == IN ? 2 : (s == ON ? 1 : 0)) << endl; } return 0; }
replace
168
171
168
172
TLE
p02299
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <vector> using namespace std; enum POSITION { ONLINE_BACK = -2, CCW = -1, ON_SEGMENT = 0, CW = 1, ONLINE_FRONT = 2 }; struct Point { long x; long y; Point(long x, long y) : x(x), y(y) {} Point() {} Point operator-(Point p) { return Point(x - p.x, y - p.y); } }; typedef Point Vector; double cross(Vector v1, Vector v2) { return v1.x * v2.y - v1.y * v2.x; } double dot(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y; } struct Segment { Point p1; Point p2; Segment(Point p1, Point p2) : p1(p1), p2(p2) {} Segment() {} }; typedef vector<Point> Polygon; POSITION get_position(Segment s, Point p) { double c = cross(s.p2 - s.p1, p - s.p1); if (c > 0) return CCW; if (c < 0) return CW; double d = dot(s.p2 - s.p1, p - s.p1); if (d < 0) return ONLINE_BACK; if (d > dot(s.p2 - s.p1, s.p2 - s.p1)) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Segment s1, Segment s2) { if (get_position(s1, s2.p1) * get_position(s1, s2.p2) != -1) { return false; } if (get_position(s2, s1.p1) * get_position(s2, s1.p2) != -1) { return false; } return true; } int determine_containment(Point p, Polygon poly) { Segment s_right(p, Point(p.x + 1e5, p.y)); int count = 0; for (int i = 0, n = poly.size(); i < n; i++) { if (get_position(Segment(poly[i], poly[(i + 1) % n]), p) == ON_SEGMENT) { return 1; } if (intersect(s_right, Segment(poly[i], poly[(i + 1) % n]))) { count++; } if (get_position(s_right, poly[i]) == ON_SEGMENT) { int i_pre = (i == 0 ? n - 1 : i - 1); POSITION pos_pre = get_position(s_right, poly[i_pre]); while (pos_pre != CCW && pos_pre != CW) { i_pre = (i_pre == 0 ? n - 1 : i_pre - 1); pos_pre = get_position(s_right, poly[i_pre]); } int i_aft = (i + 1) % n; POSITION pos_aft = get_position(s_right, poly[i_aft]); while (pos_aft != CCW && pos_aft != CW) { i_aft = (i_aft + 1) % n; POSITION pos_aft = get_position(s_right, poly[i_aft]); } if (get_position(s_right, poly[i_pre]) * get_position(s_right, poly[(i + 1) % n]) == -1) { count++; } } } if (count % 2) return 2; return 0; } int main() { int q; cin >> q; Polygon poly(q); for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; poly[i].x = x; poly[i].y = y; } int n; cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cout << determine_containment(Point(x, y), poly) << endl; } return 0; }
#include <cmath> #include <iostream> #include <vector> using namespace std; enum POSITION { ONLINE_BACK = -2, CCW = -1, ON_SEGMENT = 0, CW = 1, ONLINE_FRONT = 2 }; struct Point { long x; long y; Point(long x, long y) : x(x), y(y) {} Point() {} Point operator-(Point p) { return Point(x - p.x, y - p.y); } }; typedef Point Vector; double cross(Vector v1, Vector v2) { return v1.x * v2.y - v1.y * v2.x; } double dot(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y; } struct Segment { Point p1; Point p2; Segment(Point p1, Point p2) : p1(p1), p2(p2) {} Segment() {} }; typedef vector<Point> Polygon; POSITION get_position(Segment s, Point p) { double c = cross(s.p2 - s.p1, p - s.p1); if (c > 0) return CCW; if (c < 0) return CW; double d = dot(s.p2 - s.p1, p - s.p1); if (d < 0) return ONLINE_BACK; if (d > dot(s.p2 - s.p1, s.p2 - s.p1)) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Segment s1, Segment s2) { if (get_position(s1, s2.p1) * get_position(s1, s2.p2) != -1) { return false; } if (get_position(s2, s1.p1) * get_position(s2, s1.p2) != -1) { return false; } return true; } int determine_containment(Point p, Polygon poly) { Segment s_right(p, Point(p.x + 1e5, p.y)); int count = 0; for (int i = 0, n = poly.size(); i < n; i++) { if (get_position(Segment(poly[i], poly[(i + 1) % n]), p) == ON_SEGMENT) { return 1; } if (intersect(s_right, Segment(poly[i], poly[(i + 1) % n]))) { count++; } if (get_position(s_right, poly[i]) == ON_SEGMENT) { int i_pre = (i == 0 ? n - 1 : i - 1); POSITION pos_pre = get_position(s_right, poly[i_pre]); while (pos_pre != CCW && pos_pre != CW) { i_pre = (i_pre == 0 ? n - 1 : i_pre - 1); pos_pre = get_position(s_right, poly[i_pre]); } int i_aft = (i + 1) % n; POSITION pos_aft = get_position(s_right, poly[i_aft]); while (pos_aft != CCW && pos_aft != CW) { i_aft = (i_aft + 1) % n; pos_aft = get_position(s_right, poly[i_aft]); } if (get_position(s_right, poly[i_pre]) * get_position(s_right, poly[(i + 1) % n]) == -1) { count++; } } } if (count % 2) return 2; return 0; } int main() { int q; cin >> q; Polygon poly(q); for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; poly[i].x = x; poly[i].y = y; } int n; cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cout << determine_containment(Point(x, y), poly) << endl; } return 0; }
replace
77
78
77
78
TLE
p02300
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <vector> using namespace std; class Point { public: int x, y; Point(int a = 0, int b = 0) { int x = a; int y = b; }; bool operator<(const Point &p) const { return y < p.y || (y == p.y && x < p.x); } }; inline bool direct(Point &base, Point &a, Point &b) { return (a.x - base.x) * (b.y - base.y) - (a.y - base.y) * (b.x - base.x) < 0; } int main() { int n, i; int k = 2; scanf("%d", &n); vector<Point> P(n), H(n); for (i = 1; i <= n; ++i) { scanf("%d%d", &P[i - 1].x, &P[i - 1].y); } sort(P.begin(), P.end()); H[0] = P[0]; H[1] = P[1]; for (i = 0; i < n - 2; ++i) { while (k >= 2 && direct(H[k - 2], H[k - 1], P[i + 2])) k--; H[k++] = P[i + 2]; } H[k++] = P[n - 2]; for (i = n; i > 2; i--) { while (k >= 2 && direct(H[k - 2], H[k - 1], P[i - 3])) k--; H[k++] = P[i - 3]; } printf("%d\n", k - 1); for (i = 0; i < k - 1; ++i) { printf("%d %d\n", H[i].x, H[i].y); } return 0; }
#include <algorithm> #include <cstdio> #include <vector> using namespace std; class Point { public: int x, y; Point(int a = 0, int b = 0) { int x = a; int y = b; }; bool operator<(const Point &p) const { return y < p.y || (y == p.y && x < p.x); } }; inline bool direct(Point &base, Point &a, Point &b) { return (a.x - base.x) * (b.y - base.y) - (a.y - base.y) * (b.x - base.x) < 0; } int main() { int n, i; int k = 2; scanf("%d", &n); vector<Point> P(n), H(2 * n); for (i = 1; i <= n; ++i) { scanf("%d%d", &P[i - 1].x, &P[i - 1].y); } sort(P.begin(), P.end()); H[0] = P[0]; H[1] = P[1]; for (i = 0; i < n - 2; ++i) { while (k >= 2 && direct(H[k - 2], H[k - 1], P[i + 2])) k--; H[k++] = P[i + 2]; } H[k++] = P[n - 2]; for (i = n; i > 2; i--) { while (k >= 2 && direct(H[k - 2], H[k - 1], P[i - 3])) k--; H[k++] = P[i - 3]; } printf("%d\n", k - 1); for (i = 0; i < k - 1; ++i) { printf("%d %d\n", H[i].x, H[i].y); } return 0; }
replace
25
26
25
26
0
p02300
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // 节点从0到n-1 #define maxn 50006 struct node { double x, y; }; int n; // 有多少个点 int tot; // 凸包上的点 node p[maxn]; // 所有点数组从0开始 node P[maxn]; // 凸包上的点 const double pi = acos(-1.0); // 三点差积 double X(node A, node B, node C) { return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y); } // 两点距离 double len(node A, node B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } // 角度小的在前面,相同的话距离小的在前面 bool cmp(node A, node B) { double pp = X(p[0], A, B); if (pp > 0) return true; if (pp < 0) return false; if (pp == 0) return len(p[0], A) < len(p[0], B); } const double eps = 1e-9; // 三点点积 double dian(node A, node B, node C) { return (B.x - A.x) * (C.x - A.x) + (C.y - A.y) * (B.y - A.y); } // 建立凸包存到P里//以下是n>=3的情况,n=1,2要特判 void build() { // 找左下角的点 for (int i = 0; i < n; i++) { if (p[i].y < p[0].y) swap(p[i], p[0]); if (p[i].y == p[0].y && p[i].x < p[0].x) swap(p[i], p[0]); } sort(p + 1, p + n, cmp); P[0] = p[0]; P[1] = p[1]; tot = 1; // 初始两个点 for (int i = 2; i < n; i++) { // 如果需要边上的点 while (tot > 0 && X(P[tot - 1], P[tot], p[i]) < 0) tot--; // while (tot > 0 && X(P[tot - 1], P[tot], p[i]) <= //0)tot--;//P[tot]在左边 舍弃 tot++; P[tot] = p[i]; } // 如果需要边上点 要这块 node endd = P[tot]; for (int i = n - 1; i >= 0; i--) { if (abs(X(P[0], endd, p[i])) <= eps) { if (dian(p[i], P[0], endd) < 0) { tot++; P[tot] = p[i]; } } else break; } // 到这上面是需要注释掉的 tot++; } int main() { // freopen("a.txt","r",stdin); scanf("%d", &n); for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; build(); printf("%d\n", tot); for (int i = 0; i < tot; i++) { cout << P[i].x << ' ' << P[i].y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; // 节点从0到n-1 #define maxn 100006 struct node { double x, y; }; int n; // 有多少个点 int tot; // 凸包上的点 node p[maxn]; // 所有点数组从0开始 node P[maxn]; // 凸包上的点 const double pi = acos(-1.0); // 三点差积 double X(node A, node B, node C) { return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y); } // 两点距离 double len(node A, node B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } // 角度小的在前面,相同的话距离小的在前面 bool cmp(node A, node B) { double pp = X(p[0], A, B); if (pp > 0) return true; if (pp < 0) return false; if (pp == 0) return len(p[0], A) < len(p[0], B); } const double eps = 1e-9; // 三点点积 double dian(node A, node B, node C) { return (B.x - A.x) * (C.x - A.x) + (C.y - A.y) * (B.y - A.y); } // 建立凸包存到P里//以下是n>=3的情况,n=1,2要特判 void build() { // 找左下角的点 for (int i = 0; i < n; i++) { if (p[i].y < p[0].y) swap(p[i], p[0]); if (p[i].y == p[0].y && p[i].x < p[0].x) swap(p[i], p[0]); } sort(p + 1, p + n, cmp); P[0] = p[0]; P[1] = p[1]; tot = 1; // 初始两个点 for (int i = 2; i < n; i++) { // 如果需要边上的点 while (tot > 0 && X(P[tot - 1], P[tot], p[i]) < 0) tot--; // while (tot > 0 && X(P[tot - 1], P[tot], p[i]) <= //0)tot--;//P[tot]在左边 舍弃 tot++; P[tot] = p[i]; } // 如果需要边上点 要这块 node endd = P[tot]; for (int i = n - 1; i >= 0; i--) { if (abs(X(P[0], endd, p[i])) <= eps) { if (dian(p[i], P[0], endd) < 0) { tot++; P[tot] = p[i]; } } else break; } // 到这上面是需要注释掉的 tot++; } int main() { // freopen("a.txt","r",stdin); scanf("%d", &n); for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; build(); printf("%d\n", tot); for (int i = 0; i < tot; i++) { cout << P[i].x << ' ' << P[i].y << endl; } return 0; }
replace
3
4
3
4
0
p02300
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b)-1; i >= (a); --i) #define sz(a) (int)a.size() #define de(c) cout << #c << " = " << c << endl #define dd(c) cout << #c << " = " << c << " " #define all(a) a.begin(), a.end() #define pw(x) (1ll << (x)) #define endl "\n" typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef vector<int> vi; typedef db T; const db eps = 1e-9, pi = acosl(-1.); int sgn(T x) { return (x > eps) - (x < -eps); } struct P { T x, y; P() {} P(T x, T y) : x(x), y(y) {} P operator-(const P &b) const { return P(x - b.x, y - b.y); } P operator+(const P &b) const { return P(x + b.x, y + b.y); } T operator*(const P &b) const { return x * b.x + y * b.y; } T operator/(const P &b) const { return x * b.y - y * b.x; } P operator*(const T &k) const { return P(x * k, y * k); } P operator/(const T &k) const { return P(x / k, y / k); } bool operator<(const P &b) const { return sgn(x - b.x) ? x < b.x : y < b.y; } }; T norm(P a) { return a * a; } T abs(P a) { return sqrtl(norm(a)); } P proj(P p, P a, P b) { return (b - a) * ((p - a) * (b - a) / norm(b - a)) + a; } P reflect(P p, P a, P b) { return proj(p, a, b) * 2 - p; } T cross(P o, P a, P b) { return (a - o) / (b - o); } int crossOp(P o, P a, P b) { return sgn(cross(o, a, b)); } bool onPS(P p, P s, P t) { return sgn((t - s) / (p - s)) == 0 && sgn((p - s) * (p - t)) <= 0; } struct L { P s, t; L() {} L(P s, P t) : s(s), t(t) {} }; P insLL(L a, L b) { // line x line P s = a.s - b.s, v = a.t - a.s, w = b.t - b.s; db k1 = s / w, k2 = w / v; if (sgn(k2) == 0) return abs(b.s - a.s) < abs(b.t - a.s) ? b.s : b.t; return a.s + v * (k1 / k2); } bool isSS(L a, L b) { // seg x seg , replace x->y to accelerate T c1 = (a.t - a.s) / (b.s - a.s), c2 = (a.t - a.s) / (b.t - a.s); T c3 = (b.t - b.s) / (a.s - b.s), c4 = (b.t - b.s) / (a.t - b.s); return sgn(c1) * sgn(c2) <= 0 && sgn(c3) * sgn(c4) <= 0 && sgn(max(a.s.x, a.t.x) - min(b.s.x, b.t.x)) >= 0 && sgn(max(b.s.x, b.t.x) - min(a.s.x, a.t.x)) >= 0 && sgn(max(a.s.y, a.t.y) - min(b.s.y, b.t.y)) >= 0 && sgn(max(b.s.y, b.t.y) - min(a.s.y, a.t.y)) >= 0; } db disPL(P p, L a) { return fabs((a.t - a.s) / (p - a.s)) / abs(a.t - a.s); } db disPS(P p, L a) { // p x seg dis if (sgn((a.t - a.s) * (p - a.s)) == -1) return abs(p - a.s); if (sgn((a.s - a.t) * (p - a.t)) == -1) return abs(p - a.t); return disPL(p, a); } db disSS(L a, L b) { // seg x seg dis if (isSS(a, b)) return 0; return min(min(disPS(a.s, b), disPS(a.t, b)), min(disPS(b.s, a), disPS(b.t, a))); } typedef vector<P> polygon; polygon convex(polygon A) { // counter-clockwise , <= : <=180 , < : <180 int n = sz(A), m = 0; polygon B; B.resize(n + 1); sort(all(A)); rep(i, 0, n) { while (m > 1 && sgn((B[m - 1] - B[m - 2]) / (A[i] - B[m - 2])) < 0) --m; B[m++] = A[i]; } per(i, 0, n - 1) { while (m > 1 && sgn((B[m - 1] - B[m - 2]) / (A[i] - B[m - 2])) < 0) --m; B[m++] = A[i]; } B.resize(m); if (sz(B) > 1) B.pop_back(); return B; } T area(polygon A) { // multiple 2 with integer type T res = 0; rep(i, 0, sz(A)) res += A[i] / (A[(i + 1) % sz(A)]); return fabs(res) / 2; } bool isconvex(polygon A) { // counter-clockwise bool ok = 1; int n = sz(A); rep(i, 0, 2) A.pb(A[i]); rep(i, 0, n) ok &= ((A[i + 1] - A[i]) / (A[i + 2] - A[i])) >= 0; return ok; } int inPpolygon(P p, polygon A) { // -1 : on , 0 : out , 1 : in int res = 0; rep(i, 0, sz(A)) { P u = A[i], v = A[(i + 1) % sz(A)]; if (onPS(p, u, v)) return -1; T cross = sgn((v - u) / (p - u)), d1 = sgn(u.y - p.y), d2 = sgn(v.y - p.y); if (cross > 0 && d1 <= 0 && d2 > 0) ++res; if (cross < 0 && d2 <= 0 && d1 > 0) --res; } return res != 0; } P a, b, c, d; polygon poly; int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); int q; cin >> q; cout << setiosflags(ios::fixed); cout << setprecision(0); while (q--) { cin >> a.x >> a.y; poly.pb(a); } poly = convex(poly); cout << sz(poly) << endl; int p = 0; rep(i, 0, sz(poly)) if (poly[i].y < poly[p].y || (poly[i].y == poly[p].y && poly[i].x < poly[p].x)) p = i; rep(i, p, sz(poly)) cout << poly[i].x << " " << poly[i].y << endl; rep(i, 0, p) cout << poly[i].x << " " << poly[i].y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b)-1; i >= (a); --i) #define sz(a) (int)a.size() #define de(c) cout << #c << " = " << c << endl #define dd(c) cout << #c << " = " << c << " " #define all(a) a.begin(), a.end() #define pw(x) (1ll << (x)) #define endl "\n" typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef vector<int> vi; typedef db T; const db eps = 1e-9, pi = acosl(-1.); int sgn(T x) { return (x > eps) - (x < -eps); } struct P { T x, y; P() {} P(T x, T y) : x(x), y(y) {} P operator-(const P &b) const { return P(x - b.x, y - b.y); } P operator+(const P &b) const { return P(x + b.x, y + b.y); } T operator*(const P &b) const { return x * b.x + y * b.y; } T operator/(const P &b) const { return x * b.y - y * b.x; } P operator*(const T &k) const { return P(x * k, y * k); } P operator/(const T &k) const { return P(x / k, y / k); } bool operator<(const P &b) const { return sgn(x - b.x) ? x < b.x : y < b.y; } }; T norm(P a) { return a * a; } T abs(P a) { return sqrtl(norm(a)); } P proj(P p, P a, P b) { return (b - a) * ((p - a) * (b - a) / norm(b - a)) + a; } P reflect(P p, P a, P b) { return proj(p, a, b) * 2 - p; } T cross(P o, P a, P b) { return (a - o) / (b - o); } int crossOp(P o, P a, P b) { return sgn(cross(o, a, b)); } bool onPS(P p, P s, P t) { return sgn((t - s) / (p - s)) == 0 && sgn((p - s) * (p - t)) <= 0; } struct L { P s, t; L() {} L(P s, P t) : s(s), t(t) {} }; P insLL(L a, L b) { // line x line P s = a.s - b.s, v = a.t - a.s, w = b.t - b.s; db k1 = s / w, k2 = w / v; if (sgn(k2) == 0) return abs(b.s - a.s) < abs(b.t - a.s) ? b.s : b.t; return a.s + v * (k1 / k2); } bool isSS(L a, L b) { // seg x seg , replace x->y to accelerate T c1 = (a.t - a.s) / (b.s - a.s), c2 = (a.t - a.s) / (b.t - a.s); T c3 = (b.t - b.s) / (a.s - b.s), c4 = (b.t - b.s) / (a.t - b.s); return sgn(c1) * sgn(c2) <= 0 && sgn(c3) * sgn(c4) <= 0 && sgn(max(a.s.x, a.t.x) - min(b.s.x, b.t.x)) >= 0 && sgn(max(b.s.x, b.t.x) - min(a.s.x, a.t.x)) >= 0 && sgn(max(a.s.y, a.t.y) - min(b.s.y, b.t.y)) >= 0 && sgn(max(b.s.y, b.t.y) - min(a.s.y, a.t.y)) >= 0; } db disPL(P p, L a) { return fabs((a.t - a.s) / (p - a.s)) / abs(a.t - a.s); } db disPS(P p, L a) { // p x seg dis if (sgn((a.t - a.s) * (p - a.s)) == -1) return abs(p - a.s); if (sgn((a.s - a.t) * (p - a.t)) == -1) return abs(p - a.t); return disPL(p, a); } db disSS(L a, L b) { // seg x seg dis if (isSS(a, b)) return 0; return min(min(disPS(a.s, b), disPS(a.t, b)), min(disPS(b.s, a), disPS(b.t, a))); } typedef vector<P> polygon; polygon convex(polygon A) { // counter-clockwise , <= : <=180 , < : <180 int n = sz(A), m = 0; polygon B; B.resize(n << 1); sort(all(A)); rep(i, 0, n) { while (m > 1 && sgn((B[m - 1] - B[m - 2]) / (A[i] - B[m - 2])) < 0) --m; B[m++] = A[i]; } per(i, 0, n - 1) { while (m > 1 && sgn((B[m - 1] - B[m - 2]) / (A[i] - B[m - 2])) < 0) --m; B[m++] = A[i]; } B.resize(m); if (sz(B) > 1) B.pop_back(); return B; } T area(polygon A) { // multiple 2 with integer type T res = 0; rep(i, 0, sz(A)) res += A[i] / (A[(i + 1) % sz(A)]); return fabs(res) / 2; } bool isconvex(polygon A) { // counter-clockwise bool ok = 1; int n = sz(A); rep(i, 0, 2) A.pb(A[i]); rep(i, 0, n) ok &= ((A[i + 1] - A[i]) / (A[i + 2] - A[i])) >= 0; return ok; } int inPpolygon(P p, polygon A) { // -1 : on , 0 : out , 1 : in int res = 0; rep(i, 0, sz(A)) { P u = A[i], v = A[(i + 1) % sz(A)]; if (onPS(p, u, v)) return -1; T cross = sgn((v - u) / (p - u)), d1 = sgn(u.y - p.y), d2 = sgn(v.y - p.y); if (cross > 0 && d1 <= 0 && d2 > 0) ++res; if (cross < 0 && d2 <= 0 && d1 > 0) --res; } return res != 0; } P a, b, c, d; polygon poly; int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); int q; cin >> q; cout << setiosflags(ios::fixed); cout << setprecision(0); while (q--) { cin >> a.x >> a.y; poly.pb(a); } poly = convex(poly); cout << sz(poly) << endl; int p = 0; rep(i, 0, sz(poly)) if (poly[i].y < poly[p].y || (poly[i].y == poly[p].y && poly[i].x < poly[p].x)) p = i; rep(i, p, sz(poly)) cout << poly[i].x << " " << poly[i].y << endl; rep(i, 0, p) cout << poly[i].x << " " << poly[i].y << endl; return 0; }
replace
84
85
84
85
0
p02300
C++
Runtime Error
// 凸包 #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <cassert> #include <queue> using namespace std; // 浮動小数点のゼロ判定 #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) // 点を表すクラス class Point { public: double x, y; // コンストラクタ Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} // -----▼▼▼ 演算子のオーバーロード ▼▼▼----- Point operator+(Point &p) { return Point(x + p.x, y + p.y); } Point operator-(Point &p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator<=(const Point &p) const { return x != p.x ? x <= p.x : y <= p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } // -----▲▲▲ 演算子のオーバーロード ▲▲▲----- // 22点間の距離の算出 double distance() { return sqrt(norm()); } double norm() { return (x * x) + (y * y); } }; // 線分を表すクラス class Segment { public: Point p1, p2; // コンストラクタ Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; // 点とベクトルの表現は同じ typedef Point Vector; // 線分と直線の表現は同じ typedef Segment Line; // 多角形の表現 typedef vector<Point> Polygon; // 円を表すくらす class Circle { public: Point c; // 中心 double r; // 半径 Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {} }; double dot(Vector a, Vector b); double cross(Vector a, Vector b); double cross_point(Line line, Point pp); Vector projection(Vector a, Vector b, Vector p); Point project(Segment s, Point p); double getDistanceLP(Line l, Point p); int ccw(Point p0, Point p1, Point p2); Point getCrossPoint(Segment s1, Segment s2); pair<Point, Point> getCrossPoint(Circle circle, Line l); pair<Point, Point> getCrossPoint(Circle c1, Circle c2); int contains(Polygon g, Point p); Polygon AndrewScan(Polygon pg); int main(void) { int n, index; Point p, minPoint; Polygon poly, retPoly; cin >> n; for (int i = 0; i < n; i++) { // 各点の格納 cin >> p.x >> p.y; // 多角形の作成 poly.push_back(p); ; } retPoly = AndrewScan(poly); // -----▼▼▼ 表示のための処理 ▼▼▼----- // 凸多角形の頂点の一番下で、左端の点を探す minPoint = retPoly[0]; for (int i = 1; i < retPoly.size(); i++) { if (retPoly[i].y == minPoint.y) { if (retPoly[i].x < minPoint.x) { index = i; minPoint = retPoly[i]; } } else if (retPoly[i].y <= minPoint.y) { index = i; minPoint = retPoly[i]; } } cout << retPoly.size() << endl; for (int i = index; i < retPoly.size(); i++) { cout << retPoly[i].x << " " << retPoly[i].y << endl; } for (int i = 0; i < index; i++) { cout << retPoly[i].x << " " << retPoly[i].y << endl; } // -----▲▲▲ 表示のための処理 ▲▲▲----- return 0; } // 内積の計算をする関数 double dot(Vector a, Vector b) { return ((a.x * b.x) + (a.y * b.y)); } // 外積の計算をする関数 double cross(Vector a, Vector b) { return ((a.x * b.y) - (a.y * b.x)); } // 外積の計算をする関数 2 // 直線 : line に対する点pの外積の演算 double cross_point(Line line, Point pp) { return (line.p2.x - line.p1.x) * (pp.y - line.p1.y) - (pp.x - line.p1.x) * (line.p2.y - line.p1.y); } // 正射影ベクトルを求める関数 // p : 垂線の始点となる点 Vector projection(Vector a, Vector b, Vector p) { // 正射影ベクトルの考え方より // 正射影ベクトル = ( ( a, bベクトルの内積 ) / aベクトルの大きさの2乗 ) * // aベクトル return ((a * (dot(a, b) / pow(a.distance(), 2.0))) + p); } // 正射影ベクトルを求める関数 2 // p : 垂線の始点となる点 // 戻り値 : pからの垂線と直線:sの交点 Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / base.norm(); Point tp = base * r; return s.p1 + tp; } // 点と直線の距離を求める関数 double getDistanceLP(Line l, Point p) { if (dot(l.p2 - l.p1, p - l.p1) < 0.0) { // 内積の値が負の場合, 2つの線分のなす角が90°以上のため, // 距離は, p と l.p1 の距離そのものになる return (p - l.p1).distance(); } if (dot(l.p1 - l.p2, p - l.p2) < 0.0) { // 内積の値が負の場合, 2つの線分のなす角が90°以上のため, // 距離は, p と l.p2 の距離そのものになる return (p - l.p2).distance(); } // 2つの線分のなす角が90°以内の時 return abs(cross(l.p2 - l.p1, p - l.p1) / (l.p2 - l.p1).distance()); } static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; // 3点の関係を調査する関数 int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) { // P2が半時計回りの方向にいる return COUNTER_CLOCKWISE; } else if (cross(a, b) < -EPS) { // P2が時計回りの方向にいる return CLOCKWISE; } else if (dot(a, b) < -EPS) { // P2が, 直線:p0p1に対して, 180°反対方向にいる return ONLINE_BACK; } else if (a.norm() < b.norm()) { // p0, p1, p2 の順で同一直線上に点が並んでいる return ONLINE_FRONT; } // p0, p2, p1 の順で同一直線上に点が並んでいる return ON_SEGMENT; } // -----▼▼▼ 線分の交差判定を実施する関数群 ▼▼▼----- bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p1) <= 0); } // 線分の交点が存在するかの確認 bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } // 円の交点が存在するかの確認 bool intersect(Circle c1, Circle c2) { return ((c2.c - c1.c).distance() <= c1.r + c2.r); } // -----▲▲▲ 線分の交差判定を実施する関数群 ▲▲▲----- // 線分の交点を求める関数 Point getCrossPoint(Segment s1, Segment s2) { Point p0; double d1, d2, t; Vector base = s2.p2 - s2.p1; // 外積(平行四辺形の面積)から, 点と直線の距離を求める d1 = abs(cross(base, s1.p1 - s2.p1) / base.distance()); d2 = abs(cross(base, s1.p2 - s2.p1) / base.distance()); t = d1 / (d1 + d2); p0 = (s1.p2 - s1.p1) * t; return s1.p1 + p0; } // 円と直線の交点を求める関数 pair<Point, Point> getCrossPoint(Circle circle, Line l) { Point p0, p1; // 円の中心からの垂線と直線:lの交点:prを求める Vector pr = project(l, circle.c); if ((pr - circle.c).distance() > circle.r) { // 円と直線に交点が存在しない時 return make_pair(p0, p1); } // 直線:l上の単位ベクトルを求める Vector e = (l.p2 - l.p1) / (l.p2 - l.p1).distance(); // 交点:prと、円と直線の交点までの距離を求める double base = sqrt(circle.r * circle.r - (pr - circle.c).norm()); // 円と直線の交点を求める Point eb = e * base; p0 = pr - eb; p1 = pr + eb; if (p1 < p0) { // 点の大小関係の調整 p0 = pr + eb; p1 = pr - eb; } return make_pair(p0, p1); } // -----▼▼▼ 円の交点を求めるための関数一覧 ▼▼▼----- // 与えられたベクトルのx軸となす角を求める関数 double arg(Vector v) { return atan2(v.y, v.x); } // x軸上の点aを, x軸回りに角度θだけ回転させた点を求める関数 Point polar(double a, double theta) { return Point(cos(theta) * a, sin(theta) * a); } // 2つの円の交点を求める関数 pair<Point, Point> getCrossPoint(Circle c1, Circle c2) { Point p0, p1, pa, pb; // 円の交点が存在することの確認 assert(intersect(c1, c2)); // 円の中心間の距離を求める double d = (c2.c - c1.c).distance(); // 余弦定理より, c1の半径と円の中心間の直線:dのなす角を求める double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2.0 * c1.r * d)); // x軸とdがなす角を求める double t = arg(c2.c - c1.c); pa = polar(c1.r, t + a); p0 = c1.c + pa; pb = polar(c1.r, t - a); p1 = c1.c + pb; if (p1 < p0) { // 表示用の大小関係の確認 p1 = c1.c + pa; p0 = c1.c + pb; } return make_pair(p0, p1); } // -----▲▲▲ 円の交点を求めるための関数一覧 ▲▲▲----- // 指定された点が多角形に内包されるかを調べる関数 int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { // 多角形の各点を, 点:pが原点となるように平行移動する Point a = g[i] - p; Point b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) { // aベクトル, bベクトルが同一直線上で, 方向が反対 // 点:pが, 多角形上の辺にいる時 return 1; } if (a.y > b.y) { // aベクトルのyの値が小さくなるようにする swap(a, b); } if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) { // aベクトルとbベクトルの終点が半直線をまたいで反対方向にある // bベクトルがaベクトルに対して, 半時計回りの方向にある x = !x; } } return (x ? 2 : 0); } // 凸包を求めるアンドリューのアルゴリズム Polygon AndrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) { return s; } // 点の集合をx軸について, 昇順に並び替える sort(s.begin(), s.end()); // xが小さいものから, 2つ u に追加 u.push_back(s[0]); u.push_back(s[1]); // xが大きいものから, 2つ l に追加 l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); // ------------------------- // ----- 凸包の上部を作成 ----- // ------------------------- for (int i = 2; i < s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) { // 追加する点が, 直前の2点が作るベクトルに対して, // 半時計回りの方向にあるなら, 最後の点を削除する u.pop_back(); } u.push_back(s[i]); } // ------------------------- // ----- 凸包の下部を作成 ----- // ------------------------- for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) { // 追加する点が, 直前の2点が作るベクトルに対して, // 時計回りの方向にあるなら, 最後の点を削除する l.pop_back(); } l.push_back(s[i]); } // 時計回りになるように凸包の点の列を生成 reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) { // 凸包の下部の点と重ならないように追加する l.push_back(u[i]); } return l; }
// 凸包 #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <cassert> #include <queue> using namespace std; // 浮動小数点のゼロ判定 #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) // 点を表すクラス class Point { public: double x, y; // コンストラクタ Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} // -----▼▼▼ 演算子のオーバーロード ▼▼▼----- Point operator+(Point &p) { return Point(x + p.x, y + p.y); } Point operator-(Point &p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator<=(const Point &p) const { return x != p.x ? x <= p.x : y <= p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } // -----▲▲▲ 演算子のオーバーロード ▲▲▲----- // 22点間の距離の算出 double distance() { return sqrt(norm()); } double norm() { return (x * x) + (y * y); } }; // 線分を表すクラス class Segment { public: Point p1, p2; // コンストラクタ Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; // 点とベクトルの表現は同じ typedef Point Vector; // 線分と直線の表現は同じ typedef Segment Line; // 多角形の表現 typedef vector<Point> Polygon; // 円を表すくらす class Circle { public: Point c; // 中心 double r; // 半径 Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {} }; double dot(Vector a, Vector b); double cross(Vector a, Vector b); double cross_point(Line line, Point pp); Vector projection(Vector a, Vector b, Vector p); Point project(Segment s, Point p); double getDistanceLP(Line l, Point p); int ccw(Point p0, Point p1, Point p2); Point getCrossPoint(Segment s1, Segment s2); pair<Point, Point> getCrossPoint(Circle circle, Line l); pair<Point, Point> getCrossPoint(Circle c1, Circle c2); int contains(Polygon g, Point p); Polygon AndrewScan(Polygon pg); int main(void) { int n, index; Point p, minPoint; Polygon poly, retPoly; cin >> n; for (int i = 0; i < n; i++) { // 各点の格納 cin >> p.x >> p.y; // 多角形の作成 poly.push_back(p); ; } retPoly = AndrewScan(poly); // -----▼▼▼ 表示のための処理 ▼▼▼----- // 凸多角形の頂点の一番下で、左端の点を探す minPoint = retPoly[0]; index = 0; for (int i = 1; i < retPoly.size(); i++) { if (retPoly[i].y == minPoint.y) { if (retPoly[i].x < minPoint.x) { index = i; minPoint = retPoly[i]; } } else if (retPoly[i].y <= minPoint.y) { index = i; minPoint = retPoly[i]; } } cout << retPoly.size() << endl; for (int i = index; i < retPoly.size(); i++) { cout << retPoly[i].x << " " << retPoly[i].y << endl; } for (int i = 0; i < index; i++) { cout << retPoly[i].x << " " << retPoly[i].y << endl; } // -----▲▲▲ 表示のための処理 ▲▲▲----- return 0; } // 内積の計算をする関数 double dot(Vector a, Vector b) { return ((a.x * b.x) + (a.y * b.y)); } // 外積の計算をする関数 double cross(Vector a, Vector b) { return ((a.x * b.y) - (a.y * b.x)); } // 外積の計算をする関数 2 // 直線 : line に対する点pの外積の演算 double cross_point(Line line, Point pp) { return (line.p2.x - line.p1.x) * (pp.y - line.p1.y) - (pp.x - line.p1.x) * (line.p2.y - line.p1.y); } // 正射影ベクトルを求める関数 // p : 垂線の始点となる点 Vector projection(Vector a, Vector b, Vector p) { // 正射影ベクトルの考え方より // 正射影ベクトル = ( ( a, bベクトルの内積 ) / aベクトルの大きさの2乗 ) * // aベクトル return ((a * (dot(a, b) / pow(a.distance(), 2.0))) + p); } // 正射影ベクトルを求める関数 2 // p : 垂線の始点となる点 // 戻り値 : pからの垂線と直線:sの交点 Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / base.norm(); Point tp = base * r; return s.p1 + tp; } // 点と直線の距離を求める関数 double getDistanceLP(Line l, Point p) { if (dot(l.p2 - l.p1, p - l.p1) < 0.0) { // 内積の値が負の場合, 2つの線分のなす角が90°以上のため, // 距離は, p と l.p1 の距離そのものになる return (p - l.p1).distance(); } if (dot(l.p1 - l.p2, p - l.p2) < 0.0) { // 内積の値が負の場合, 2つの線分のなす角が90°以上のため, // 距離は, p と l.p2 の距離そのものになる return (p - l.p2).distance(); } // 2つの線分のなす角が90°以内の時 return abs(cross(l.p2 - l.p1, p - l.p1) / (l.p2 - l.p1).distance()); } static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; // 3点の関係を調査する関数 int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) { // P2が半時計回りの方向にいる return COUNTER_CLOCKWISE; } else if (cross(a, b) < -EPS) { // P2が時計回りの方向にいる return CLOCKWISE; } else if (dot(a, b) < -EPS) { // P2が, 直線:p0p1に対して, 180°反対方向にいる return ONLINE_BACK; } else if (a.norm() < b.norm()) { // p0, p1, p2 の順で同一直線上に点が並んでいる return ONLINE_FRONT; } // p0, p2, p1 の順で同一直線上に点が並んでいる return ON_SEGMENT; } // -----▼▼▼ 線分の交差判定を実施する関数群 ▼▼▼----- bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p1) <= 0); } // 線分の交点が存在するかの確認 bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } // 円の交点が存在するかの確認 bool intersect(Circle c1, Circle c2) { return ((c2.c - c1.c).distance() <= c1.r + c2.r); } // -----▲▲▲ 線分の交差判定を実施する関数群 ▲▲▲----- // 線分の交点を求める関数 Point getCrossPoint(Segment s1, Segment s2) { Point p0; double d1, d2, t; Vector base = s2.p2 - s2.p1; // 外積(平行四辺形の面積)から, 点と直線の距離を求める d1 = abs(cross(base, s1.p1 - s2.p1) / base.distance()); d2 = abs(cross(base, s1.p2 - s2.p1) / base.distance()); t = d1 / (d1 + d2); p0 = (s1.p2 - s1.p1) * t; return s1.p1 + p0; } // 円と直線の交点を求める関数 pair<Point, Point> getCrossPoint(Circle circle, Line l) { Point p0, p1; // 円の中心からの垂線と直線:lの交点:prを求める Vector pr = project(l, circle.c); if ((pr - circle.c).distance() > circle.r) { // 円と直線に交点が存在しない時 return make_pair(p0, p1); } // 直線:l上の単位ベクトルを求める Vector e = (l.p2 - l.p1) / (l.p2 - l.p1).distance(); // 交点:prと、円と直線の交点までの距離を求める double base = sqrt(circle.r * circle.r - (pr - circle.c).norm()); // 円と直線の交点を求める Point eb = e * base; p0 = pr - eb; p1 = pr + eb; if (p1 < p0) { // 点の大小関係の調整 p0 = pr + eb; p1 = pr - eb; } return make_pair(p0, p1); } // -----▼▼▼ 円の交点を求めるための関数一覧 ▼▼▼----- // 与えられたベクトルのx軸となす角を求める関数 double arg(Vector v) { return atan2(v.y, v.x); } // x軸上の点aを, x軸回りに角度θだけ回転させた点を求める関数 Point polar(double a, double theta) { return Point(cos(theta) * a, sin(theta) * a); } // 2つの円の交点を求める関数 pair<Point, Point> getCrossPoint(Circle c1, Circle c2) { Point p0, p1, pa, pb; // 円の交点が存在することの確認 assert(intersect(c1, c2)); // 円の中心間の距離を求める double d = (c2.c - c1.c).distance(); // 余弦定理より, c1の半径と円の中心間の直線:dのなす角を求める double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2.0 * c1.r * d)); // x軸とdがなす角を求める double t = arg(c2.c - c1.c); pa = polar(c1.r, t + a); p0 = c1.c + pa; pb = polar(c1.r, t - a); p1 = c1.c + pb; if (p1 < p0) { // 表示用の大小関係の確認 p1 = c1.c + pa; p0 = c1.c + pb; } return make_pair(p0, p1); } // -----▲▲▲ 円の交点を求めるための関数一覧 ▲▲▲----- // 指定された点が多角形に内包されるかを調べる関数 int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { // 多角形の各点を, 点:pが原点となるように平行移動する Point a = g[i] - p; Point b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) { // aベクトル, bベクトルが同一直線上で, 方向が反対 // 点:pが, 多角形上の辺にいる時 return 1; } if (a.y > b.y) { // aベクトルのyの値が小さくなるようにする swap(a, b); } if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) { // aベクトルとbベクトルの終点が半直線をまたいで反対方向にある // bベクトルがaベクトルに対して, 半時計回りの方向にある x = !x; } } return (x ? 2 : 0); } // 凸包を求めるアンドリューのアルゴリズム Polygon AndrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) { return s; } // 点の集合をx軸について, 昇順に並び替える sort(s.begin(), s.end()); // xが小さいものから, 2つ u に追加 u.push_back(s[0]); u.push_back(s[1]); // xが大きいものから, 2つ l に追加 l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); // ------------------------- // ----- 凸包の上部を作成 ----- // ------------------------- for (int i = 2; i < s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) { // 追加する点が, 直前の2点が作るベクトルに対して, // 半時計回りの方向にあるなら, 最後の点を削除する u.pop_back(); } u.push_back(s[i]); } // ------------------------- // ----- 凸包の下部を作成 ----- // ------------------------- for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) { // 追加する点が, 直前の2点が作るベクトルに対して, // 時計回りの方向にあるなら, 最後の点を削除する l.pop_back(); } l.push_back(s[i]); } // 時計回りになるように凸包の点の列を生成 reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) { // 凸包の下部の点と重ならないように追加する l.push_back(u[i]); } return l; }
insert
112
112
112
113
0
p02300
C++
Runtime Error
#include <algorithm> #include <complex> #include <iostream> #include <vector> #define push push_back #define sz stack.size() #define m make_pair #define N_MAX 10001 #define x second #define y first using namespace std; typedef complex<int> point; typedef pair<int, int> P; bool ccw(point p0, point p1, point p2) { point temp; p1 -= p0; p2 -= p0; temp = p1; p1 *= conj(temp); p2 *= conj(temp); if (abs(temp) == 0) return true; p1 /= abs(temp); if (p2.imag() > 0) return false; if (p2.imag() < 0) return true; p2 /= abs(temp); return true; } int main() { int n; P p[N_MAX]; vector<P> stack; cin >> n; for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; sort(p, p + n); stack.push(p[0]); stack.push(p[1]); for (int i = 2; i < n; i++) { point c = point(p[i].x, p[i].y); while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y), point(stack[sz - 1].x, stack[sz - 1].y), c)) stack.pop_back(); stack.push(p[i]); } for (int i = n - 2; i >= 0; i--) { point c = point(p[i].x, p[i].y); while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y), point(stack[sz - 1].x, stack[sz - 1].y), c)) stack.pop_back(); stack.push(p[i]); } stack.erase(stack.begin()); cout << stack.size() << endl; for (int i = sz - 1; i >= 0; i--) cout << stack[i].x << ' ' << stack[i].y << endl; return 0; }
#include <algorithm> #include <complex> #include <iostream> #include <vector> #define push push_back #define sz stack.size() #define m make_pair #define N_MAX 100001 #define x second #define y first using namespace std; typedef complex<int> point; typedef pair<int, int> P; bool ccw(point p0, point p1, point p2) { point temp; p1 -= p0; p2 -= p0; temp = p1; p1 *= conj(temp); p2 *= conj(temp); if (abs(temp) == 0) return true; p1 /= abs(temp); if (p2.imag() > 0) return false; if (p2.imag() < 0) return true; p2 /= abs(temp); return true; } int main() { int n; P p[N_MAX]; vector<P> stack; cin >> n; for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; sort(p, p + n); stack.push(p[0]); stack.push(p[1]); for (int i = 2; i < n; i++) { point c = point(p[i].x, p[i].y); while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y), point(stack[sz - 1].x, stack[sz - 1].y), c)) stack.pop_back(); stack.push(p[i]); } for (int i = n - 2; i >= 0; i--) { point c = point(p[i].x, p[i].y); while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y), point(stack[sz - 1].x, stack[sz - 1].y), c)) stack.pop_back(); stack.push(p[i]); } stack.erase(stack.begin()); cout << stack.size() << endl; for (int i = sz - 1; i >= 0; i--) cout << stack[i].x << ' ' << stack[i].y << endl; return 0; }
replace
7
8
7
8
0
p02301
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> #define EPS 1e-10 using namespace std; typedef long long ll; struct Point { double x, y; Point(int x = 0, int y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator-() { return Point(-x, -y); } Point operator*(double lambda) { return Point(x * lambda, y * lambda); } Point operator/(double lambda) { return Point(x / lambda, y / lambda); } Point rot(double theta) { return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y); } double norm() { return x * x + y * y; } double abs_() { return sqrt(norm()); } bool operator==(const Point p) const { return abs(x - p.x) < EPS && abs(y - p.y) < EPS; } bool operator<(const Point p) const { if (abs(x - p.x) < EPS) return y < p.y; return x < p.x; } }; typedef Point Vector; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double det(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } typedef vector<Point> Polygon; double diameter(Polygon po) { int n = po.size(), minp = 0, maxp = 0; for (int i = 1; i < n; i++) { if (po[i] < po[minp]) minp = i; if (po[maxp] < po[i]) maxp = i; } int i = minp, j = maxp; double ans = 0.0; while (i != maxp || j != minp) { ans = max(ans, (po[i] - po[j]).abs_()); if (det(po[(i + 1) % n] - po[i], po[(j + 1) % n] - po[j]) < EPS) { i = (i + 1) % n; } else { j = (j + 1) % n; } } return ans; } int n; Polygon g; int main() { scanf("%d", &n); g.resize(n); for (int i = 0; i < n; i++) scanf("%lf%lf", &g[i].x, &g[i].y); printf("%.10f\n", diameter(g)); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> #define EPS 1e-10 using namespace std; typedef long long ll; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator-() { return Point(-x, -y); } Point operator*(double lambda) { return Point(x * lambda, y * lambda); } Point operator/(double lambda) { return Point(x / lambda, y / lambda); } Point rot(double theta) { return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y); } double norm() { return x * x + y * y; } double abs_() { return sqrt(norm()); } bool operator==(const Point p) const { return abs(x - p.x) < EPS && abs(y - p.y) < EPS; } bool operator<(const Point p) const { if (abs(x - p.x) < EPS) return y < p.y; return x < p.x; } }; typedef Point Vector; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double det(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } typedef vector<Point> Polygon; double diameter(Polygon po) { int n = po.size(), minp = 0, maxp = 0; for (int i = 1; i < n; i++) { if (po[i] < po[minp]) minp = i; if (po[maxp] < po[i]) maxp = i; } int i = minp, j = maxp; double ans = 0.0; while (i != maxp || j != minp) { ans = max(ans, (po[i] - po[j]).abs_()); if (det(po[(i + 1) % n] - po[i], po[(j + 1) % n] - po[j]) < EPS) { i = (i + 1) % n; } else { j = (j + 1) % n; } } return ans; } int n; Polygon g; int main() { scanf("%d", &n); g.resize(n); for (int i = 0; i < n; i++) scanf("%lf%lf", &g[i].x, &g[i].y); printf("%.10f\n", diameter(g)); return 0; }
replace
11
12
11
12
TLE
p02301
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef vector<P> vec; namespace std { bool operator<(const P &a, const P &b) { return (a.imag() == b.imag() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std double eps = 1e-8; double PI = acos(-1); bool eq(double a, double b) { return (b - a < eps && a - b < eps); } bool eq(P a, P b) { return (abs(a - b) < eps); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } P project(P a, P b, P c) { b -= a; c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a; c -= a; return a + conj(c / b) * b; } int ccw(P a, P b, P c) { b -= a, c -= a, a = c * conj(b); if (a.imag() > eps) return 1; // ccw if (a.imag() < -eps) return -1; // cw if (a.real() < -eps) return 2; // ob if (abs(b) + eps < abs(c)) return -2; // of return 0; // os } // segment ab , point c double dist(P a, P b, P c) { if (dot(b - a, c - a) < 0) return abs(c - a); if (dot(a - b, c - b) < 0) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } bool isintersect(P a, P b, P c, P d) { return ((ccw(a, b, c) * ccw(a, b, d) <= 0) && (ccw(c, d, a) * ccw(c, d, b) <= 0)); } // segment ab , segment cd P intersect(P a, P b, P c, P d) { a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } double dist(P a, P b, P c, P d) { if (isintersect(a, b, c, d)) return 0; double ab = min(dist(a, b, c), dist(a, b, d)); double cd = min(dist(c, d, a), dist(c, d, b)); return min(ab, cd); } double calcArea(vec &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) res += cross(t[i], t[(i + 1 == n ? 0 : i + 1)]); return abs(res / 2.0); } double Arg(P a, P b, P c) { b -= a, c -= a; return arg(c * conj(b)); } int inPolygon(vec &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; // on sum += Arg(p, a, b); } if (abs(sum) < eps) return 0; // out else return 2; // in } typedef pair<double, P> Pair; vec andrewScan(vec &t) { int N = t.size(), C = 0; vec R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vec res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vec ConvexHull2(vec t) { sort(t.begin(), t.end()); vec u = andrewScan(t); reverse(t.begin(), t.end()); vec l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vec ConvexHull(vec t) { vector<Pair> u; vec R; int N = t.size(), K = 0, C = 0; for (int i = 1; i < N; i++) if (t[i] < t[K]) K = i; for (int i = 0; i < N; i++) u.push_back(Pair(arg(t[i] - t[K]), t[i])); R.push_back(t[K]), C++; sort(u.begin(), u.end()); for (int i = 0; i < N; i++) { P p = u[i].second; if (2 <= C && ccw(R[C - 2], R[C - 1], p) == 0) continue; if (2 <= C && ccw(R[C - 2], R[C - 1], p) == 2) continue; while (2 <= C && ccw(R[C - 2], R[C - 1], p) != 1) R.pop_back(), C--; R.push_back(p), C++; } return R; } int main() { int n; cin >> n; vec t; double x, y; for (int i = 0; i < n; i++) { cin >> x >> y; t.push_back(P(x, y)); } vec ans = ConvexHull2(t); int m = ans.size(); double maxm = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { maxm = max(maxm, abs(ans[i] - ans[j])); } } printf("%.8f\n", maxm); return 0; }
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef vector<P> vec; namespace std { bool operator<(const P &a, const P &b) { return (a.imag() == b.imag() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std double eps = 1e-8; double PI = acos(-1); bool eq(double a, double b) { return (b - a < eps && a - b < eps); } bool eq(P a, P b) { return (abs(a - b) < eps); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } P project(P a, P b, P c) { b -= a; c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a; c -= a; return a + conj(c / b) * b; } int ccw(P a, P b, P c) { b -= a, c -= a, a = c * conj(b); if (a.imag() > eps) return 1; // ccw if (a.imag() < -eps) return -1; // cw if (a.real() < -eps) return 2; // ob if (abs(b) + eps < abs(c)) return -2; // of return 0; // os } // segment ab , point c double dist(P a, P b, P c) { if (dot(b - a, c - a) < 0) return abs(c - a); if (dot(a - b, c - b) < 0) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } bool isintersect(P a, P b, P c, P d) { return ((ccw(a, b, c) * ccw(a, b, d) <= 0) && (ccw(c, d, a) * ccw(c, d, b) <= 0)); } // segment ab , segment cd P intersect(P a, P b, P c, P d) { a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } double dist(P a, P b, P c, P d) { if (isintersect(a, b, c, d)) return 0; double ab = min(dist(a, b, c), dist(a, b, d)); double cd = min(dist(c, d, a), dist(c, d, b)); return min(ab, cd); } double calcArea(vec &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) res += cross(t[i], t[(i + 1 == n ? 0 : i + 1)]); return abs(res / 2.0); } double Arg(P a, P b, P c) { b -= a, c -= a; return arg(c * conj(b)); } int inPolygon(vec &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; // on sum += Arg(p, a, b); } if (abs(sum) < eps) return 0; // out else return 2; // in } typedef pair<double, P> Pair; vec andrewScan(vec &t) { int N = t.size(), C = 0; vec R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vec res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vec ConvexHull2(vec t) { sort(t.begin(), t.end()); vec u = andrewScan(t); reverse(t.begin(), t.end()); vec l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vec ConvexHull(vec t) { vector<Pair> u; vec R; int N = t.size(), K = 0, C = 0; for (int i = 1; i < N; i++) if (t[i] < t[K]) K = i; for (int i = 0; i < N; i++) u.push_back(Pair(arg(t[i] - t[K]), t[i])); R.push_back(t[K]), C++; sort(u.begin(), u.end()); for (int i = 0; i < N; i++) { P p = u[i].second; if (2 <= C && ccw(R[C - 2], R[C - 1], p) == 0) continue; if (2 <= C && ccw(R[C - 2], R[C - 1], p) == 2) continue; while (2 <= C && ccw(R[C - 2], R[C - 1], p) != 1) R.pop_back(), C--; R.push_back(p), C++; } return R; } int main() { int n; cin >> n; vec t; double x, y; for (int i = 0; i < n; i++) { cin >> x >> y; t.push_back(P(x, y)); } vec ans = ConvexHull(t); int m = ans.size(); double maxm = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { maxm = max(maxm, abs(ans[i] - ans[j])); } } printf("%.8f\n", maxm); return 0; }
replace
159
160
159
160
TLE
p02302
C++
Runtime Error
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <vector> using Number = double; const Number EPS = 1e-10; const Number INF = 1e10; const Number PI = acos(-1.0); inline int sign(Number x) { return (x < -EPS) ? -1 : (x > EPS) ? +1 : 0; } inline bool equal(Number a, Number b) { return sign(a - b) == 0; } // change between degree and radian inline Number to_radian(const Number degree) { return degree * PI / 180.0; } inline Number to_degree(const Number radian) { return radian * 180.0 / PI; } /** * Point in two dimensional */ class Point { public: Number x, y; Point() {} Point(Number x, Number y) : x(x), y(y) {} // Arithmetic operator between points Point operator+(const Point &rhs) const { return Point(this->x + rhs.x, this->y + rhs.y); } Point operator-(const Point &rhs) const { return Point(this->x - rhs.x, this->y - rhs.y); } Point operator*(const Point &rhs) const { // cross product between points return Point(this->x * rhs.x - this->y * rhs.y, this->x * rhs.y + this->y * rhs.x); } // Unary operator and compound assignment operator Point operator-() const { return Point(-this->x, -this->y); } Point &operator+=(const Point &rhs) { return *this = *this + rhs; } Point &operator-=(const Point &rhs) { return *this = *this - rhs; } // Arithmetic operator between point and number Point operator*(Number rhs) const { return Point(this->x * rhs, this->y * rhs); } Point operator/(Number rhs) const { return Point(this->x / rhs, this->y / rhs); } // Comparison operation bool operator==(const Point &rhs) const { return sign(this->x - rhs.x) == 0 && sign(this->y - rhs.y) == 0; } bool operator<(const Point &rhs) const { return (this->x < rhs.x) || (this->x == rhs.x && this->y < rhs.y); } // Other operator Number abs(void) const { return sqrt(this->x * this->x + this->y * this->y); } Number abs2(void) const { return this->x * this->x + this->y * this->y; } Number arg(void) const { return atan2(this->y, this->x); } Number dot(const Point &rhs) { return this->x * rhs.x + this->y * rhs.y; } Point rotate90(void) const { return Point(-this->y, this->x); } Point rotate(double angle) const { return Point(cos(angle) * this->x - sin(angle) * this->y, sin(angle) * this->x + cos(angle) * this->y); } }; inline Number dot(const Point &p1, const Point &p2) { return p1.x * p2.x + p1.y * p2.y; } inline Number abs_cross(const Point &p1, const Point &p2) { return p1.x * p2.y - p1.y * p2.x; } // Output of a point std::ostream &operator<<(std::ostream &os, const Point &p) { return os << p.x << ' ' << p.y; } // Input of a point std::istream &operator>>(std::istream &is, Point &p) { return is >> p.x >> p.y; } // Counter-Clockwise predicate (a, b, c) enum CCW { COUNTER_CLOCKWISE = 1, // counter clockwise CLOCKWISE = -1, // clockwise ONLINE_FRONT = 2, // a--c--b on line ONLINE_BACK = -2, // c--a--b on line ON_SEGMENT = 0, // a--b--c on line OTHER = -3, }; CCW ccw(const Point &a, Point b, Point c) { b -= a; c -= a; if (sign(abs_cross(b, c)) == 1) return COUNTER_CLOCKWISE; if (sign(abs_cross(b, c)) == -1) return CLOCKWISE; if (sign(dot(b, c)) == -1) return ONLINE_BACK; if (sign(b.abs2() - c.abs2()) == -1) return ONLINE_FRONT; return ON_SEGMENT; } /** * Line in two dimensional */ class Line : public std::array<Point, 2> { public: Line() {} Line(const Point &p1, const Point &p2) { (*this)[0] = p1; (*this)[1] = p2; } }; // Input of a line std::istream &operator>>(std::istream &is, Line &l) { return is >> l[0] >> l[1]; } // Output of a line std::ostream &operator<<(std::ostream &os, const Line &l) { return os << l[0] << ' ' << l[1]; } inline CCW ccw(const Line &l, const Point &p) { return ccw(l[0], l[1], p); } /** * Segment in two dimensional */ class Segment : public Line { public: Segment() {} Segment(const Point &p1, const Point &p2) : Line(p1, p2) {} }; /** * Circle in two dimensional */ class Circle : public Point { public: Number r; Circle() {} Circle(const Point &p, Number r = 0.0) : Point(p), r(r) {} }; // Input of a circle std::istream &operator>>(std::istream &is, Circle &c) { return is >> c.x >> c.y >> c.r; } /** * Intersection testing */ Point Projection(const Line &l, const Point &p) { Point dir = l[1] - l[0]; Number t = dot(p - l[0], dir) / dir.abs2(); return l[0] + dir * t; } inline Point Reflection(const Line &l, const Point &p) { return Projection(l, p) * 2.0 - p; } inline bool IsOrthogonal(const Line &l1, const Line &l2) { return equal(dot(l1[0] - l1[1], l2[0] - l2[1]), 0.0); } inline bool IsParallel(const Line &l1, const Line &l2) { return equal(abs_cross(l1[0] - l1[1], l2[0] - l2[1]), 0.0); } inline bool IsIntersect(const Line &l, const Point &p) { return abs(ccw(l[0], l[1], p)) != 1; } inline bool IsIntersect(const Segment &s, const Point &p) { return ccw(s[0], s[1], p) == ON_SEGMENT; } inline bool IsIntersect(const Line &l1, const Line l2) { return !IsParallel(l1, l2) || IsParallel(l1, Line(l1[0], l2[0])); } inline bool IsIntersect(const Line &l, const Segment &s) { return sign(abs_cross(l[1] - l[0], s[0] - l[0]) * abs_cross(l[1] - l[0], s[1] - l[0])) <= 0; } inline bool IsIntersect(const Segment &s1, const Segment &s2) { return ccw(s1[0], s1[1], s2[0]) * ccw(s1[0], s1[1], s2[1]) <= 0 && ccw(s2[0], s2[1], s1[0]) * ccw(s2[0], s2[1], s1[1]) <= 0; } inline bool IsIntersect(const Circle &c, const Point &p) { // p is in interior or boundary return (c - p).abs() <= c.r + EPS; } inline bool IsIntersect(const Circle &c, const Line &l) { return IsIntersect(c, Projection(l, c)); } inline bool IsIntersect(const Circle &c1, const Circle &c2) { return sign(c1.r + c2.r - (c1 - c2).abs()) >= 0 && sign((c1 - c2).abs() - abs(c1.r - c2.r) >= 0); } /** * Distance and Intersection point */ inline Number Distance(const Point &p1, const Point &p2) { return (p1 - p2).abs(); } inline Number Distance(const Line &l, const Point &p) { return (p - Projection(l, p)).abs(); } inline Number Distance(const Segment &s, const Point &p) { if (sign(dot(s[1] - s[0], p - s[0])) == -1) return (p - s[0]).abs(); if (sign(dot(s[0] - s[1], p - s[1])) == -1) return (p - s[1]).abs(); return (p - Projection(s, p)).abs(); } inline Number Distance(const Line &l1, const Line &l2) { return IsIntersect(l1, l2) ? 0 : Distance(l1, l2[0]); } inline Number Distance(const Line &l, const Segment &s) { if (IsIntersect(l, s)) return 0.0; return std::min(Distance(l, s[0]), Distance(l, s[1])); } inline Number Distance(const Segment &s1, const Segment &s2) { if (IsIntersect(s1, s2)) return 0.0; return std::min({Distance(s1, s2[0]), Distance(s1, s2[1]), Distance(s2, s1[0]), Distance(s2, s1[1])}); } Point CrossPoint(const Line &l1, const Line &l2) { Number A = abs_cross(l1[1] - l1[0], l2[1] - l2[0]); Number B = abs_cross(l1[1] - l1[0], l1[1] - l2[0]); if (sign(abs(A)) == -1 && sign(abs(B)) == -1) return l2[0]; if (sign(abs(A)) == -1) assert(false); return l2[0] + (l2[1] - l2[0]) * B / A; } std::vector<Point> CrossPoint(const Circle &c, const Line &l) { if (!IsIntersect(c, l)) return std::vector<Point>(); Point mid = Projection(l, c); if (equal((c - mid).abs(), c.r)) return {mid}; Point e = (l[1] - l[0]) / (l[1] - l[0]).abs(); Number len = sqrt(c.r * c.r - (mid - c).abs2()); return {mid + e * len, mid - e * len}; } std::vector<Point> CrossPoint(const Circle &c1, const Circle &c2) { if (!IsIntersect(c1, c2)) return std::vector<Point>(); Number d = Distance(c1, c2); Number r1_cos = (d * d + c1.r * c1.r - c2.r * c2.r) / (2.0 * d); Number h = sqrt(c1.r * c1.r - r1_cos * r1_cos); Point base = c1 + (c2 - c1) * r1_cos / d; Point dir = (c2 - c1).rotate90() * h / d; if (dir == Point(0, 0)) return {base}; return {base + dir, base - dir}; } // the tangent line from a point to a circle std::vector<Point> TangentPoint(const Circle &c, const Point &p) { Number x = (p - c).abs2(); Number d = x - c.r * c.r; if (sign(d) == -1) // no point return std::vector<Point>(); d = std::max(d, 0.0); Point q1 = (p - c) * (c.r * c.r / x); Point q2 = ((p - c) * (-c.r * sqrt(d) / x)).rotate90(); if (q2 == Point(0, 0)) return {c + q1}; return {c + q1 - q2, c + q1 + q2}; } // common tangent lines to two circles std::vector<Line> CommonTangent(const Circle &c1, const Circle &c2) { // two circle contact one point internally if (equal(Distance(c1, c2), abs(c1.r - c2.r))) { // | $ $| Point cross_point = CrossPoint(c1, c2)[0]; Point up = (cross_point - c1).rotate90(); return {Line(cross_point + up, cross_point - up)}; } std::vector<Line> list; // caluculate outer tangent if (equal(c1.r, c2.r)) { Point dir = c2 - c1; dir = (dir * (c1.r / dir.abs())).rotate90(); list.emplace_back(Line(c1 + dir, c2 + dir)); list.emplace_back(Line(c1 - dir, c2 - dir)); } else { Point p = (c1 * (-c2.r)) + (c2 * c1.r); p = p * (1 / (c1.r - c2.r)); std::vector<Point> ps = TangentPoint(c1, p); std::vector<Point> qs = TangentPoint(c2, p); const int N = std::min(ps.size(), qs.size()); for (int i = 0; i < N; ++i) list.emplace_back(Line(ps[i], qs[i])); } // caluculate inner tangent if (equal(Distance(c1, c2), c1.r + c2.r)) { // two circle contact one point outernally | |$ $ Point cross_point = CrossPoint(c1, c2)[0]; Point up = (cross_point - c1).rotate90(); list.emplace_back(Line(cross_point + up, cross_point - up)); } else { // | | $ $ Point p = (c1 * c2.r) + (c2 * c1.r); p = p * (1 / (c1.r + c2.r)); std::vector<Point> ps = TangentPoint(c1, p); std::vector<Point> qs = TangentPoint(c2, p); const int N = std::min(ps.size(), qs.size()); for (int i = 0; i < N; ++i) list.emplace_back(Line(ps[i], qs[i])); } return list; } /** * Polygon */ class Polygon : public std::vector<Point> { public: Polygon() {} Polygon(int size) : std::vector<Point>(size) {} Polygon(std::initializer_list<Point> p) : std::vector<Point>(p) {} Number Area() const; // area of polygon : O(n) bool IsConvex() const; // Test whether it's convex polygon : O(n) std::vector<Point> ConvexHull() const; // Andrew's Monotone Chain Algorithm : O(n * log n) Number ConvexDiameter() const; // rotating calipers algorithm : O(n) Polygon ConvexCut( const Line &l) const; // cut the polygon by line and return the left: O(n) // Is p contained or on segment or otherwise? : O(n) enum { OUT, ON, IN, }; int Contain(const Point &p) const; // convex version : O(log n) int ConvexContain(const Point &p) const; }; // Output of a polygon std::ostream &operator<<(std::ostream &os, const Polygon &poly) { for (auto p : poly) os << p << ", "; return os; } Number Polygon::Area() const { const int n = (*this).size(); assert(1 < n); Number area = abs_cross((*this)[n - 1], (*this)[0]); for (int i = 0; i < n - 1; ++i) area += abs_cross((*this)[i], (*this)[i + 1]); return 0.5 * area; } bool Polygon::IsConvex() const { // if given polygon is not simple polygon we should check for all (i-1, i, // i+1) (p[i].y <= p[i-1].y && p[i].y < p[i+1].y) doesn't happen without // first index which y is the lowest const int n = (*this).size(); CCW diff = OTHER; for (int i = 0; i < n; ++i) { CCW cur = ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]); if (diff == OTHER && (cur == CLOCKWISE || cur == COUNTER_CLOCKWISE)) diff = static_cast<CCW>(-cur); else if (cur == diff) return false; } return true; } int Polygon::Contain(const Point &p) const { const int n = (*this).size(); bool count = false; for (int i = 0; i < n; ++i) { if (IsIntersect(Segment((*this)[i], (*this)[(i + 1) % n]), p)) return ON; Point up = (*this)[i] - p, down = (*this)[(i + 1) % n] - p; if (up.y < down.y) std::swap(up, down); if (sign(down.y) <= 0 && sign(up.y) == 1 && sign(abs_cross(up, down)) == 1) count = !count; } return count ? IN : OUT; } int Polygon::ConvexContain(const Point &p) const { const int n = (*this).size(); Point g = ((*this)[0] + (*this)[n / 3] + (*this)[2 * n / 3]) / 3.0; // inner point int a = 0, b = n; while (a + 1 < b) { // invariant : c is in fan g-poly[a]-poly[b] int c = (a + b) * 0.5; if (sign(abs_cross((*this)[a] - g, (*this)[c] - g)) == 1) { // angle < 180 deg if (sign(abs_cross((*this)[a] - g, p - g)) >= 0 && sign(abs_cross((*this)[c] - g, p - g)) == -1) b = c; else a = c; } else { if (sign(abs_cross((*this)[a] - g, p - g)) == -1 && sign(abs_cross((*this)[c] - g, p - g)) == 1) a = c; else b = c; } } // Assume that points in polygon are in the order of counter-clockwise b %= n; int res = sign(abs_cross((*this)[a] - p, (*this)[b] - p)); return (res == -1 ? OUT : (res == 1 ? IN : ON)); } std::vector<Point> Polygon::ConvexHull() const { if ((*this).size() < 3) return (*this); Polygon poly = (*this); const int n = poly.size(); int size = 0; std::vector<Point> chain(2 * n); std::sort(poly.begin(), poly.end()); for (int i = 0; i < n; chain[size++] = poly[i++]) // lower hull while (size >= 2 && ccw(chain[size - 2], chain[size - 1], poly[i]) <= 0) --size; for (int i = n - 2, j = size + 1; 0 <= i; chain[size++] = poly[i--]) // upper hull while (size >= j && ccw(chain[size - 2], chain[size - 1], poly[i]) <= 0) --size; chain.resize(size - 1); return chain; } Number Polygon::ConvexDiameter() const { const int n = (*this).size(); const Polygon &poly = (*this); std::pair<int, int> s; // first is min, second is max for (int i = 1; i < n; ++i) { if (poly[i].y < poly[s.first].y) s.first = i; if (poly[i].y > poly[s.second].y) s.second = i; } Number max_d = Distance(poly[s.first], poly[s.second]); std::pair<int, int> cur(s), max_p(s); // Assume that points in polygon are in the order of counter-clockwise do { Point v1 = poly[cur.second] - poly[(cur.second + 1) % n] + poly[cur.first]; if (ccw(poly[cur.first], poly[(cur.first + 1) % n], v1) == COUNTER_CLOCKWISE) cur.first = (cur.first + 1) % n; else cur.second = (cur.second + 1) % n; Number cur_d = Distance(poly[cur.first], poly[cur.second]); if (max_d < cur_d) { max_p = cur; max_d = cur_d; } } while (cur != s); return max_d; } Polygon Polygon::ConvexCut(const Line &l) const { const int n = (*this).size(); const Polygon &p = (*this); Polygon q; // left side polygon cutted by line for (int i = 0; i < n; ++i) { Point cur(p[i]), next(p[(i + 1) % n]); if (ccw(l, cur) != CLOCKWISE) q.emplace_back(cur); if (ccw(l, cur) * ccw(l, next) == -1) q.emplace_back(CrossPoint(l, Line(cur, next))); } return q; } int main() { std::cout << std::fixed << std::setprecision(10); std::cin.tie(0); std::ios::sync_with_stdio(false); int n, q; Line l; // Input std::cin >> n; Polygon g(n); for (int i = 0; i < n; ++i) std::cin >> g[i]; std::cin >> q; while (q--) { std::cin >> l; std::cout << (g.ConvexCut(l)).Area() << '\n'; } return 0; }
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <vector> using Number = double; const Number EPS = 1e-10; const Number INF = 1e10; const Number PI = acos(-1.0); inline int sign(Number x) { return (x < -EPS) ? -1 : (x > EPS) ? +1 : 0; } inline bool equal(Number a, Number b) { return sign(a - b) == 0; } // change between degree and radian inline Number to_radian(const Number degree) { return degree * PI / 180.0; } inline Number to_degree(const Number radian) { return radian * 180.0 / PI; } /** * Point in two dimensional */ class Point { public: Number x, y; Point() {} Point(Number x, Number y) : x(x), y(y) {} // Arithmetic operator between points Point operator+(const Point &rhs) const { return Point(this->x + rhs.x, this->y + rhs.y); } Point operator-(const Point &rhs) const { return Point(this->x - rhs.x, this->y - rhs.y); } Point operator*(const Point &rhs) const { // cross product between points return Point(this->x * rhs.x - this->y * rhs.y, this->x * rhs.y + this->y * rhs.x); } // Unary operator and compound assignment operator Point operator-() const { return Point(-this->x, -this->y); } Point &operator+=(const Point &rhs) { return *this = *this + rhs; } Point &operator-=(const Point &rhs) { return *this = *this - rhs; } // Arithmetic operator between point and number Point operator*(Number rhs) const { return Point(this->x * rhs, this->y * rhs); } Point operator/(Number rhs) const { return Point(this->x / rhs, this->y / rhs); } // Comparison operation bool operator==(const Point &rhs) const { return sign(this->x - rhs.x) == 0 && sign(this->y - rhs.y) == 0; } bool operator<(const Point &rhs) const { return (this->x < rhs.x) || (this->x == rhs.x && this->y < rhs.y); } // Other operator Number abs(void) const { return sqrt(this->x * this->x + this->y * this->y); } Number abs2(void) const { return this->x * this->x + this->y * this->y; } Number arg(void) const { return atan2(this->y, this->x); } Number dot(const Point &rhs) { return this->x * rhs.x + this->y * rhs.y; } Point rotate90(void) const { return Point(-this->y, this->x); } Point rotate(double angle) const { return Point(cos(angle) * this->x - sin(angle) * this->y, sin(angle) * this->x + cos(angle) * this->y); } }; inline Number dot(const Point &p1, const Point &p2) { return p1.x * p2.x + p1.y * p2.y; } inline Number abs_cross(const Point &p1, const Point &p2) { return p1.x * p2.y - p1.y * p2.x; } // Output of a point std::ostream &operator<<(std::ostream &os, const Point &p) { return os << p.x << ' ' << p.y; } // Input of a point std::istream &operator>>(std::istream &is, Point &p) { return is >> p.x >> p.y; } // Counter-Clockwise predicate (a, b, c) enum CCW { COUNTER_CLOCKWISE = 1, // counter clockwise CLOCKWISE = -1, // clockwise ONLINE_FRONT = 2, // a--c--b on line ONLINE_BACK = -2, // c--a--b on line ON_SEGMENT = 0, // a--b--c on line OTHER = -3, }; CCW ccw(const Point &a, Point b, Point c) { b -= a; c -= a; if (sign(abs_cross(b, c)) == 1) return COUNTER_CLOCKWISE; if (sign(abs_cross(b, c)) == -1) return CLOCKWISE; if (sign(dot(b, c)) == -1) return ONLINE_BACK; if (sign(b.abs2() - c.abs2()) == -1) return ONLINE_FRONT; return ON_SEGMENT; } /** * Line in two dimensional */ class Line : public std::array<Point, 2> { public: Line() {} Line(const Point &p1, const Point &p2) { (*this)[0] = p1; (*this)[1] = p2; } }; // Input of a line std::istream &operator>>(std::istream &is, Line &l) { return is >> l[0] >> l[1]; } // Output of a line std::ostream &operator<<(std::ostream &os, const Line &l) { return os << l[0] << ' ' << l[1]; } inline CCW ccw(const Line &l, const Point &p) { return ccw(l[0], l[1], p); } /** * Segment in two dimensional */ class Segment : public Line { public: Segment() {} Segment(const Point &p1, const Point &p2) : Line(p1, p2) {} }; /** * Circle in two dimensional */ class Circle : public Point { public: Number r; Circle() {} Circle(const Point &p, Number r = 0.0) : Point(p), r(r) {} }; // Input of a circle std::istream &operator>>(std::istream &is, Circle &c) { return is >> c.x >> c.y >> c.r; } /** * Intersection testing */ Point Projection(const Line &l, const Point &p) { Point dir = l[1] - l[0]; Number t = dot(p - l[0], dir) / dir.abs2(); return l[0] + dir * t; } inline Point Reflection(const Line &l, const Point &p) { return Projection(l, p) * 2.0 - p; } inline bool IsOrthogonal(const Line &l1, const Line &l2) { return equal(dot(l1[0] - l1[1], l2[0] - l2[1]), 0.0); } inline bool IsParallel(const Line &l1, const Line &l2) { return equal(abs_cross(l1[0] - l1[1], l2[0] - l2[1]), 0.0); } inline bool IsIntersect(const Line &l, const Point &p) { return abs(ccw(l[0], l[1], p)) != 1; } inline bool IsIntersect(const Segment &s, const Point &p) { return ccw(s[0], s[1], p) == ON_SEGMENT; } inline bool IsIntersect(const Line &l1, const Line l2) { return !IsParallel(l1, l2) || IsParallel(l1, Line(l1[0], l2[0])); } inline bool IsIntersect(const Line &l, const Segment &s) { return sign(abs_cross(l[1] - l[0], s[0] - l[0]) * abs_cross(l[1] - l[0], s[1] - l[0])) <= 0; } inline bool IsIntersect(const Segment &s1, const Segment &s2) { return ccw(s1[0], s1[1], s2[0]) * ccw(s1[0], s1[1], s2[1]) <= 0 && ccw(s2[0], s2[1], s1[0]) * ccw(s2[0], s2[1], s1[1]) <= 0; } inline bool IsIntersect(const Circle &c, const Point &p) { // p is in interior or boundary return (c - p).abs() <= c.r + EPS; } inline bool IsIntersect(const Circle &c, const Line &l) { return IsIntersect(c, Projection(l, c)); } inline bool IsIntersect(const Circle &c1, const Circle &c2) { return sign(c1.r + c2.r - (c1 - c2).abs()) >= 0 && sign((c1 - c2).abs() - abs(c1.r - c2.r) >= 0); } /** * Distance and Intersection point */ inline Number Distance(const Point &p1, const Point &p2) { return (p1 - p2).abs(); } inline Number Distance(const Line &l, const Point &p) { return (p - Projection(l, p)).abs(); } inline Number Distance(const Segment &s, const Point &p) { if (sign(dot(s[1] - s[0], p - s[0])) == -1) return (p - s[0]).abs(); if (sign(dot(s[0] - s[1], p - s[1])) == -1) return (p - s[1]).abs(); return (p - Projection(s, p)).abs(); } inline Number Distance(const Line &l1, const Line &l2) { return IsIntersect(l1, l2) ? 0 : Distance(l1, l2[0]); } inline Number Distance(const Line &l, const Segment &s) { if (IsIntersect(l, s)) return 0.0; return std::min(Distance(l, s[0]), Distance(l, s[1])); } inline Number Distance(const Segment &s1, const Segment &s2) { if (IsIntersect(s1, s2)) return 0.0; return std::min({Distance(s1, s2[0]), Distance(s1, s2[1]), Distance(s2, s1[0]), Distance(s2, s1[1])}); } Point CrossPoint(const Line &l1, const Line &l2) { Number A = abs_cross(l1[1] - l1[0], l2[1] - l2[0]); Number B = abs_cross(l1[1] - l1[0], l1[1] - l2[0]); if (sign(abs(A)) == -1 && sign(abs(B)) == -1) return l2[0]; if (sign(abs(A)) == -1) assert(false); return l2[0] + (l2[1] - l2[0]) * B / A; } std::vector<Point> CrossPoint(const Circle &c, const Line &l) { if (!IsIntersect(c, l)) return std::vector<Point>(); Point mid = Projection(l, c); if (equal((c - mid).abs(), c.r)) return {mid}; Point e = (l[1] - l[0]) / (l[1] - l[0]).abs(); Number len = sqrt(c.r * c.r - (mid - c).abs2()); return {mid + e * len, mid - e * len}; } std::vector<Point> CrossPoint(const Circle &c1, const Circle &c2) { if (!IsIntersect(c1, c2)) return std::vector<Point>(); Number d = Distance(c1, c2); Number r1_cos = (d * d + c1.r * c1.r - c2.r * c2.r) / (2.0 * d); Number h = sqrt(c1.r * c1.r - r1_cos * r1_cos); Point base = c1 + (c2 - c1) * r1_cos / d; Point dir = (c2 - c1).rotate90() * h / d; if (dir == Point(0, 0)) return {base}; return {base + dir, base - dir}; } // the tangent line from a point to a circle std::vector<Point> TangentPoint(const Circle &c, const Point &p) { Number x = (p - c).abs2(); Number d = x - c.r * c.r; if (sign(d) == -1) // no point return std::vector<Point>(); d = std::max(d, 0.0); Point q1 = (p - c) * (c.r * c.r / x); Point q2 = ((p - c) * (-c.r * sqrt(d) / x)).rotate90(); if (q2 == Point(0, 0)) return {c + q1}; return {c + q1 - q2, c + q1 + q2}; } // common tangent lines to two circles std::vector<Line> CommonTangent(const Circle &c1, const Circle &c2) { // two circle contact one point internally if (equal(Distance(c1, c2), abs(c1.r - c2.r))) { // | $ $| Point cross_point = CrossPoint(c1, c2)[0]; Point up = (cross_point - c1).rotate90(); return {Line(cross_point + up, cross_point - up)}; } std::vector<Line> list; // caluculate outer tangent if (equal(c1.r, c2.r)) { Point dir = c2 - c1; dir = (dir * (c1.r / dir.abs())).rotate90(); list.emplace_back(Line(c1 + dir, c2 + dir)); list.emplace_back(Line(c1 - dir, c2 - dir)); } else { Point p = (c1 * (-c2.r)) + (c2 * c1.r); p = p * (1 / (c1.r - c2.r)); std::vector<Point> ps = TangentPoint(c1, p); std::vector<Point> qs = TangentPoint(c2, p); const int N = std::min(ps.size(), qs.size()); for (int i = 0; i < N; ++i) list.emplace_back(Line(ps[i], qs[i])); } // caluculate inner tangent if (equal(Distance(c1, c2), c1.r + c2.r)) { // two circle contact one point outernally | |$ $ Point cross_point = CrossPoint(c1, c2)[0]; Point up = (cross_point - c1).rotate90(); list.emplace_back(Line(cross_point + up, cross_point - up)); } else { // | | $ $ Point p = (c1 * c2.r) + (c2 * c1.r); p = p * (1 / (c1.r + c2.r)); std::vector<Point> ps = TangentPoint(c1, p); std::vector<Point> qs = TangentPoint(c2, p); const int N = std::min(ps.size(), qs.size()); for (int i = 0; i < N; ++i) list.emplace_back(Line(ps[i], qs[i])); } return list; } /** * Polygon */ class Polygon : public std::vector<Point> { public: Polygon() {} Polygon(int size) : std::vector<Point>(size) {} Polygon(std::initializer_list<Point> p) : std::vector<Point>(p) {} Number Area() const; // area of polygon : O(n) bool IsConvex() const; // Test whether it's convex polygon : O(n) std::vector<Point> ConvexHull() const; // Andrew's Monotone Chain Algorithm : O(n * log n) Number ConvexDiameter() const; // rotating calipers algorithm : O(n) Polygon ConvexCut( const Line &l) const; // cut the polygon by line and return the left: O(n) // Is p contained or on segment or otherwise? : O(n) enum { OUT, ON, IN, }; int Contain(const Point &p) const; // convex version : O(log n) int ConvexContain(const Point &p) const; }; // Output of a polygon std::ostream &operator<<(std::ostream &os, const Polygon &poly) { for (auto p : poly) os << p << ", "; return os; } Number Polygon::Area() const { const int n = (*this).size(); assert(1 < n); Number area = abs_cross((*this)[n - 1], (*this)[0]); for (int i = 0; i < n - 1; ++i) area += abs_cross((*this)[i], (*this)[i + 1]); return 0.5 * area; } bool Polygon::IsConvex() const { // if given polygon is not simple polygon we should check for all (i-1, i, // i+1) (p[i].y <= p[i-1].y && p[i].y < p[i+1].y) doesn't happen without // first index which y is the lowest const int n = (*this).size(); CCW diff = OTHER; for (int i = 0; i < n; ++i) { CCW cur = ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]); if (diff == OTHER && (cur == CLOCKWISE || cur == COUNTER_CLOCKWISE)) diff = static_cast<CCW>(-cur); else if (cur == diff) return false; } return true; } int Polygon::Contain(const Point &p) const { const int n = (*this).size(); bool count = false; for (int i = 0; i < n; ++i) { if (IsIntersect(Segment((*this)[i], (*this)[(i + 1) % n]), p)) return ON; Point up = (*this)[i] - p, down = (*this)[(i + 1) % n] - p; if (up.y < down.y) std::swap(up, down); if (sign(down.y) <= 0 && sign(up.y) == 1 && sign(abs_cross(up, down)) == 1) count = !count; } return count ? IN : OUT; } int Polygon::ConvexContain(const Point &p) const { const int n = (*this).size(); Point g = ((*this)[0] + (*this)[n / 3] + (*this)[2 * n / 3]) / 3.0; // inner point int a = 0, b = n; while (a + 1 < b) { // invariant : c is in fan g-poly[a]-poly[b] int c = (a + b) * 0.5; if (sign(abs_cross((*this)[a] - g, (*this)[c] - g)) == 1) { // angle < 180 deg if (sign(abs_cross((*this)[a] - g, p - g)) >= 0 && sign(abs_cross((*this)[c] - g, p - g)) == -1) b = c; else a = c; } else { if (sign(abs_cross((*this)[a] - g, p - g)) == -1 && sign(abs_cross((*this)[c] - g, p - g)) == 1) a = c; else b = c; } } // Assume that points in polygon are in the order of counter-clockwise b %= n; int res = sign(abs_cross((*this)[a] - p, (*this)[b] - p)); return (res == -1 ? OUT : (res == 1 ? IN : ON)); } std::vector<Point> Polygon::ConvexHull() const { if ((*this).size() < 3) return (*this); Polygon poly = (*this); const int n = poly.size(); int size = 0; std::vector<Point> chain(2 * n); std::sort(poly.begin(), poly.end()); for (int i = 0; i < n; chain[size++] = poly[i++]) // lower hull while (size >= 2 && ccw(chain[size - 2], chain[size - 1], poly[i]) <= 0) --size; for (int i = n - 2, j = size + 1; 0 <= i; chain[size++] = poly[i--]) // upper hull while (size >= j && ccw(chain[size - 2], chain[size - 1], poly[i]) <= 0) --size; chain.resize(size - 1); return chain; } Number Polygon::ConvexDiameter() const { const int n = (*this).size(); const Polygon &poly = (*this); std::pair<int, int> s; // first is min, second is max for (int i = 1; i < n; ++i) { if (poly[i].y < poly[s.first].y) s.first = i; if (poly[i].y > poly[s.second].y) s.second = i; } Number max_d = Distance(poly[s.first], poly[s.second]); std::pair<int, int> cur(s), max_p(s); // Assume that points in polygon are in the order of counter-clockwise do { Point v1 = poly[cur.second] - poly[(cur.second + 1) % n] + poly[cur.first]; if (ccw(poly[cur.first], poly[(cur.first + 1) % n], v1) == COUNTER_CLOCKWISE) cur.first = (cur.first + 1) % n; else cur.second = (cur.second + 1) % n; Number cur_d = Distance(poly[cur.first], poly[cur.second]); if (max_d < cur_d) { max_p = cur; max_d = cur_d; } } while (cur != s); return max_d; } Polygon Polygon::ConvexCut(const Line &l) const { const int n = (*this).size(); const Polygon &p = (*this); Polygon q; // left side polygon cutted by line for (int i = 0; i < n; ++i) { Point cur(p[i]), next(p[(i + 1) % n]); if (ccw(l, cur) != CLOCKWISE) q.emplace_back(cur); if (ccw(l, cur) * ccw(l, next) == -1) q.emplace_back(CrossPoint(l, Line(cur, next))); } return q; } int main() { std::cout << std::fixed << std::setprecision(10); std::cin.tie(0); std::ios::sync_with_stdio(false); int n, q; Line l; // Input std::cin >> n; Polygon g(n); for (int i = 0; i < n; ++i) std::cin >> g[i]; std::cin >> q; while (q--) { std::cin >> l; auto poly = g.ConvexCut(l); if (poly.size() > 2) std::cout << poly.Area() << '\n'; else std::cout << "0.0\n"; } return 0; }
replace
516
517
516
521
0
p02302
C++
Runtime Error
//{{{ #include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef pair<int, int> pii; // input bool SR(int &_x) { return scanf("%d", &_x) == 1; } bool SR(ll &_x) { return scanf("%lld", &_x) == 1; } bool SR(double &_x) { return scanf("%lf", &_x) == 1; } bool SR(char *_s) { return scanf("%s", _s) == 1; } bool RI() { return true; } template <typename I, typename... T> bool RI(I &_x, T &..._tail) { return SR(_x) && RI(_tail...); } // output void SP(const int _x) { printf("%d", _x); } void SP(const ll _x) { printf("%lld", _x); } void SP(const double _x) { printf("%.16lf", _x); } void SP(const char *s) { printf("%s", s); } void PL() { puts(""); } template <typename I, typename... T> void PL(const I _x, const T... _tail) { SP(_x); if (sizeof...(_tail)) putchar(' '); PL(_tail...); } // macro #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < int(n); i++) #define REP1(i, a, b) for (int i = (a); i <= int(b); i++) #define PER1(i, a, b) for (int i = (a); i >= int(b); i--) #define pb push_back #define mkp make_pair #define F first #define S second // debug #ifdef darry140 template <typename A, typename B> ostream &operator<<(ostream &_s, const pair<A, B> &_p) { return _s << "(" << _p.F << "," << _p.S << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _b, It _e) // container { _s << "{"; for (auto _it = _b; _it != _e; _it++) _s << (_it == _b ? "" : " ") << *_it; _s << "}"; return _s; } template <typename A, typename B> ostream &operator<<(ostream &_s, const map<A, B> &_c) { return _OUTC(_s, ALL(_c)); } template <typename T> ostream &operator<<(ostream &_s, const set<T> &_c) { return _OUTC(_s, ALL(_c)); } template <typename T> ostream &operator<<(ostream &_s, const vector<T> &_c) { return _OUTC(_s, ALL(_c)); } template <typename I> void _DOING(const char *_s, I &&_x) { cerr << _s << "=" << _x << endl; } // without ',' template <typename I, typename... T> void _DOING(const char *_s, I &&_x, T &&..._tail) // with ',' { int _c = 0; static const char _bra[] = "({["; static const char _ket[] = ")}]"; while (*_s != ',' || _c != 0) // eg. mkp(a,b) { if (strchr(_bra, *_s)) _c++; if (strchr(_ket, *_s)) _c--; cerr << *_s++; } cerr << "=" << _x << ", "; _DOING(_s + 1, _tail...); } #define debug(...) \ do { \ fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \ _DOING(#__VA_ARGS__, __VA_ARGS__); \ } while (0) #else #define debug(...) #endif //}}} typedef long double ld; const ld global_eps = 1e-8; #if 0 enum cases{insi,bord,outs};//inside, border, outside enum cases{para,coin,inte};//parallel, coincide, intersect #else enum Case { insi, bord, outs, // inside, border, outside para, coin, inte, // parallel, coincide, intersect tang, tang_in, tang_out // tangent }; #endif int sign(ld x, ld eps = global_eps) { return (x >= eps) - (x <= -eps); } int cmp(ld a, ld b, ld eps = global_eps) { return sign(a - b, eps); } struct Point { ld x, y; Point() : x(0), y(0) {} Point(ld _x, ld _y) : x(_x), y(_y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } ld operator*(const Point &p) const { return x * p.x + y * p.y; } ld operator%(const Point &p) const { return x * p.y - y * p.x; } bool operator<(const Point &p) const { return tie(y, x) < tie(p.y, p.x); } bool operator==(const Point &p) const { return tie(y, x) == tie(p.y, p.x); } Point operator/(const ld &l) const { return Point(x / l, y / l); } Point operator*(const ld &l) const { return Point(x * l, y * l); } ld len() const { return hypot(x, y); } ld len2() const { return x * x + y * y; } ld dis(const Point &a) const { return (*this - a).len(); } ld dis2(const Point &a) const { return (*this - a).len2(); } Point unit() const { return *this / len(); } Point resize(const ld &l) const { return unit() * l; } }; struct Line { Point p1, p2; Line() {} Line(Point a, Point b) : p1(a), p2(b) {} Point way() const { return p2 - p1; } Point norm() const { return {p1.y - p2.y, p2.x - p1.x}; } bool online(const Point &p) const { return sign((p - p1) % way()) == 0; } bool onsegment(const Point &p) const { return online(p) && sign((p - p1) * (p2 - p)) >= 0; } pair<int, Point> intersect(const Line &I) const { if (sign(I.way() % way()) == 0) return {online(I.p1) ? coin : para, {0, 0}}; const Point &p3 = I.p1, &p4 = I.p2; auto _143 = (p1 - p4) % (p4 - p3), _342 = (p3 - p4) % (p4 - p2); return {inte, (p1 * _342 + p2 * _143) / (_143 + _342)}; } ld linedistance(const Point &p) const { return abs((p - p1) % way()) / way().len(); } ld signedlinedistance(const Point &p) const { return ((p - p1) % way()) / way().len(); } Point project(const Point &p) const { return intersect(Line(p, p + norm())).second; } ld segmentdistance(const Point &p) const { const auto &pro = project(p); if (onsegment(pro)) return p.dis(pro); return min(p.dis(p1), p.dis(p2)); } }; int main() { int n; RI(n); vector<Point> g(n); REP(i, n) cin >> g[i].x >> g[i].y; g.pb(g[0]); int q; RI(q); while (q--) { Line I; cin >> I.p1.x >> I.p1.y >> I.p2.x >> I.p2.y; vector<Point> v; REP(i, n) { if (I.signedlinedistance(g[i]) < 0) v.pb(g[i]); Point o = I.intersect(Line(g[i], g[i + 1])).S; if (I.online(o) && Line(g[i], g[i + 1]).onsegment(o)) v.pb(o); } int m = SZ(v); v.pb(v[0]); ld ans = 0; REP(i, m) ans += v[i] % v[i + 1]; ans = abs(ans / 2); PL((double)ans); } return 0; }
//{{{ #include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef pair<int, int> pii; // input bool SR(int &_x) { return scanf("%d", &_x) == 1; } bool SR(ll &_x) { return scanf("%lld", &_x) == 1; } bool SR(double &_x) { return scanf("%lf", &_x) == 1; } bool SR(char *_s) { return scanf("%s", _s) == 1; } bool RI() { return true; } template <typename I, typename... T> bool RI(I &_x, T &..._tail) { return SR(_x) && RI(_tail...); } // output void SP(const int _x) { printf("%d", _x); } void SP(const ll _x) { printf("%lld", _x); } void SP(const double _x) { printf("%.16lf", _x); } void SP(const char *s) { printf("%s", s); } void PL() { puts(""); } template <typename I, typename... T> void PL(const I _x, const T... _tail) { SP(_x); if (sizeof...(_tail)) putchar(' '); PL(_tail...); } // macro #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < int(n); i++) #define REP1(i, a, b) for (int i = (a); i <= int(b); i++) #define PER1(i, a, b) for (int i = (a); i >= int(b); i--) #define pb push_back #define mkp make_pair #define F first #define S second // debug #ifdef darry140 template <typename A, typename B> ostream &operator<<(ostream &_s, const pair<A, B> &_p) { return _s << "(" << _p.F << "," << _p.S << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _b, It _e) // container { _s << "{"; for (auto _it = _b; _it != _e; _it++) _s << (_it == _b ? "" : " ") << *_it; _s << "}"; return _s; } template <typename A, typename B> ostream &operator<<(ostream &_s, const map<A, B> &_c) { return _OUTC(_s, ALL(_c)); } template <typename T> ostream &operator<<(ostream &_s, const set<T> &_c) { return _OUTC(_s, ALL(_c)); } template <typename T> ostream &operator<<(ostream &_s, const vector<T> &_c) { return _OUTC(_s, ALL(_c)); } template <typename I> void _DOING(const char *_s, I &&_x) { cerr << _s << "=" << _x << endl; } // without ',' template <typename I, typename... T> void _DOING(const char *_s, I &&_x, T &&..._tail) // with ',' { int _c = 0; static const char _bra[] = "({["; static const char _ket[] = ")}]"; while (*_s != ',' || _c != 0) // eg. mkp(a,b) { if (strchr(_bra, *_s)) _c++; if (strchr(_ket, *_s)) _c--; cerr << *_s++; } cerr << "=" << _x << ", "; _DOING(_s + 1, _tail...); } #define debug(...) \ do { \ fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \ _DOING(#__VA_ARGS__, __VA_ARGS__); \ } while (0) #else #define debug(...) #endif //}}} typedef long double ld; const ld global_eps = 1e-8; #if 0 enum cases{insi,bord,outs};//inside, border, outside enum cases{para,coin,inte};//parallel, coincide, intersect #else enum Case { insi, bord, outs, // inside, border, outside para, coin, inte, // parallel, coincide, intersect tang, tang_in, tang_out // tangent }; #endif int sign(ld x, ld eps = global_eps) { return (x >= eps) - (x <= -eps); } int cmp(ld a, ld b, ld eps = global_eps) { return sign(a - b, eps); } struct Point { ld x, y; Point() : x(0), y(0) {} Point(ld _x, ld _y) : x(_x), y(_y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } ld operator*(const Point &p) const { return x * p.x + y * p.y; } ld operator%(const Point &p) const { return x * p.y - y * p.x; } bool operator<(const Point &p) const { return tie(y, x) < tie(p.y, p.x); } bool operator==(const Point &p) const { return tie(y, x) == tie(p.y, p.x); } Point operator/(const ld &l) const { return Point(x / l, y / l); } Point operator*(const ld &l) const { return Point(x * l, y * l); } ld len() const { return hypot(x, y); } ld len2() const { return x * x + y * y; } ld dis(const Point &a) const { return (*this - a).len(); } ld dis2(const Point &a) const { return (*this - a).len2(); } Point unit() const { return *this / len(); } Point resize(const ld &l) const { return unit() * l; } }; struct Line { Point p1, p2; Line() {} Line(Point a, Point b) : p1(a), p2(b) {} Point way() const { return p2 - p1; } Point norm() const { return {p1.y - p2.y, p2.x - p1.x}; } bool online(const Point &p) const { return sign((p - p1) % way()) == 0; } bool onsegment(const Point &p) const { return online(p) && sign((p - p1) * (p2 - p)) >= 0; } pair<int, Point> intersect(const Line &I) const { if (sign(I.way() % way()) == 0) return {online(I.p1) ? coin : para, {0, 0}}; const Point &p3 = I.p1, &p4 = I.p2; auto _143 = (p1 - p4) % (p4 - p3), _342 = (p3 - p4) % (p4 - p2); return {inte, (p1 * _342 + p2 * _143) / (_143 + _342)}; } ld linedistance(const Point &p) const { return abs((p - p1) % way()) / way().len(); } ld signedlinedistance(const Point &p) const { return ((p - p1) % way()) / way().len(); } Point project(const Point &p) const { return intersect(Line(p, p + norm())).second; } ld segmentdistance(const Point &p) const { const auto &pro = project(p); if (onsegment(pro)) return p.dis(pro); return min(p.dis(p1), p.dis(p2)); } }; int main() { int n; RI(n); vector<Point> g(n); REP(i, n) cin >> g[i].x >> g[i].y; g.pb(g[0]); int q; RI(q); while (q--) { Line I; cin >> I.p1.x >> I.p1.y >> I.p2.x >> I.p2.y; vector<Point> v; REP(i, n) { if (I.signedlinedistance(g[i]) < 0) v.pb(g[i]); Point o = I.intersect(Line(g[i], g[i + 1])).S; if (I.online(o) && Line(g[i], g[i + 1]).onsegment(o)) v.pb(o); } int m = SZ(v); if (SZ(v) == 0) { PL(0); continue; } v.pb(v[0]); ld ans = 0; REP(i, m) ans += v[i] % v[i + 1]; ans = abs(ans / 2); PL((double)ans); } return 0; }
insert
183
183
183
187
0
p02302
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template <class T> struct Point { typedef Point P; T x, y; explicit Point(T x = 0, T y = 0) : x(x), y(y) {} bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); } bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); } P operator+(P p) const { return P(x + p.x, y + p.y); } P operator-(P p) const { return P(x - p.x, y - p.y); } P operator*(T d) const { return P(x * d, y * d); } P operator/(T d) const { return P(x / d, y / d); } T dot(P p) const { return x * p.x + y * p.y; } T cross(P p) const { return x * p.y - y * p.x; } T cross(P a, P b) const { return (a - *this).cross(b - *this); } T dist2() const { return x * x + y * y; } double dist() const { return sqrt((double)dist2()); } // angle to x-axis in interval [-pi, pi] double angle() const { return atan2(y, x); } P unit() const { return *this / dist(); } // makes dist()=1 P perp() const { return P(-y, x); } // rotates +90 degrees P normal() const { return perp().unit(); } // returns point rotated 'a' radians ccw around the origin P rotate(double a) const { return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); } }; using P = Point<double>; template <class P> int lineIntersection(const P &s1, const P &e1, const P &s2, const P &e2, P &r) { if ((e1 - s1).cross(e2 - s2)) { // if not parallell r = s2 - (e2 - s2) * (e1 - s1).cross(s2 - s1) / (e1 - s1).cross(e2 - s2); return 1; } else return -((e1 - s1).cross(s2 - s1) == 0 || s2 == e2); } vector<P> polygonCut(const vector<P> &poly, P s, P e) { vector<P> res; rep(i, 0, sz(poly)) { P cur = poly[i], prev = i ? poly[i - 1] : poly.back(); bool side = s.cross(e, cur) < 0; if (side != (s.cross(e, prev) < 0)) { res.emplace_back(); lineIntersection(s, e, cur, prev, res.back()); } if (side) res.push_back(cur); } return res; } template <class T> T polygonArea2(vector<Point<T>> &v) { T a = v.back().cross(v[0]); rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]); return a; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int n; cin >> n; vector<P> a(n); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } int q; cin >> q; for (int i = 0; i < q; i++) { P p1, p2; cin >> p1.x >> p1.y >> p2.x >> p2.y; vector<P> poly = polygonCut(a, p2, p1); cout << polygonArea2(poly) / 2.0 << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template <class T> struct Point { typedef Point P; T x, y; explicit Point(T x = 0, T y = 0) : x(x), y(y) {} bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); } bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); } P operator+(P p) const { return P(x + p.x, y + p.y); } P operator-(P p) const { return P(x - p.x, y - p.y); } P operator*(T d) const { return P(x * d, y * d); } P operator/(T d) const { return P(x / d, y / d); } T dot(P p) const { return x * p.x + y * p.y; } T cross(P p) const { return x * p.y - y * p.x; } T cross(P a, P b) const { return (a - *this).cross(b - *this); } T dist2() const { return x * x + y * y; } double dist() const { return sqrt((double)dist2()); } // angle to x-axis in interval [-pi, pi] double angle() const { return atan2(y, x); } P unit() const { return *this / dist(); } // makes dist()=1 P perp() const { return P(-y, x); } // rotates +90 degrees P normal() const { return perp().unit(); } // returns point rotated 'a' radians ccw around the origin P rotate(double a) const { return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); } }; using P = Point<double>; template <class P> int lineIntersection(const P &s1, const P &e1, const P &s2, const P &e2, P &r) { if ((e1 - s1).cross(e2 - s2)) { // if not parallell r = s2 - (e2 - s2) * (e1 - s1).cross(s2 - s1) / (e1 - s1).cross(e2 - s2); return 1; } else return -((e1 - s1).cross(s2 - s1) == 0 || s2 == e2); } vector<P> polygonCut(const vector<P> &poly, P s, P e) { vector<P> res; rep(i, 0, sz(poly)) { P cur = poly[i], prev = i ? poly[i - 1] : poly.back(); bool side = s.cross(e, cur) < 0; if (side != (s.cross(e, prev) < 0)) { res.emplace_back(); lineIntersection(s, e, cur, prev, res.back()); } if (side) res.push_back(cur); } return res; } template <class T> T polygonArea2(vector<Point<T>> &v) { if (v.empty()) return 0; T a = v.back().cross(v[0]); rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]); return a; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int n; cin >> n; vector<P> a(n); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } int q; cin >> q; for (int i = 0; i < q; i++) { P p1, p2; cin >> p1.x >> p1.y >> p2.x >> p2.y; vector<P> poly = polygonCut(a, p2, p1); cout << polygonArea2(poly) / 2.0 << '\n'; } }
insert
64
64
64
66
0
p02303
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <random> #include <unordered_map> #include <utility> using namespace std; struct Point { double x, y; Point *next; }; uint64_t toKey(int x, int y) { union Key { uint64_t key; int x[2]; }; Key K; K.x[0] = x, K.x[1] = y; return K.key; } void build(unordered_map<uint64_t, Point *> &Grid, double d, int i, Point *P) { Grid.clear(); for (int j = 0; j <= i; j++) { int x = floor(P[j].x / d), y = floor(P[j].y / d); uint64_t key = toKey(x, y); if (Grid.count(key) != 0) { P[j].next = Grid[key]; } Grid[key] = &P[j]; } } double distance(Point &a, Point &b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } double search(unordered_map<uint64_t, Point *> &Grid, double d, Point &P) { int x = floor(P.x / d), y = floor(P.y / d); uint64_t key; double minimum = 1e10; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { key = toKey(x + dx, y + dy); if (Grid.count(key)) { Point *p = Grid[key]; while (p != NULL) { if (minimum > distance(*p, P)) minimum = distance(*p, P); p = p->next; } } } } return minimum; } void insert(unordered_map<uint64_t, Point *> &Grid, double d, Point &P) { int x = floor(P.x / d), y = floor(P.y / d); uint64_t key = toKey(x, y); if (Grid.count(key) != 0) { P.next = Grid[key]; } Grid[key] = &P; } int main() { int n; cin >> n; Point P[n]; for (int i = 0; i < n; i++) { cin >> P[i].x >> P[i].y; P[i].next = NULL; } //??????????????£??????????????? random_device rd; mt19937 mt(rd()); //?????????????????§????????????????????????????????? uniform_int_distribution<int> dist(0, n - 1); for (int i = 0; i < n; i++) { int j = dist(mt); Point p = P[i]; P[i] = P[j]; P[j] = p; } unordered_map<uint64_t, Point *> Grid; double delta = distance(P[0], P[1]); build(Grid, delta, 1, P); for (int i = 2; i < n; i++) { double d2 = search(Grid, delta, P[i]); if (d2 > delta) { insert(Grid, delta, P[i]); } else { delta = d2; build(Grid, delta, i, P); } } cout.precision(6); cout << fixed; cout << delta << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <random> #include <unordered_map> #include <utility> using namespace std; struct Point { double x, y; Point *next; }; uint64_t toKey(int x, int y) { union Key { uint64_t key; int x[2]; }; Key K; K.x[0] = x, K.x[1] = y; return K.key; } void build(unordered_map<uint64_t, Point *> &Grid, double d, int i, Point *P) { Grid.clear(); for (int j = 0; j <= i; j++) { int x = floor(P[j].x / d), y = floor(P[j].y / d); uint64_t key = toKey(x, y); if (Grid.count(key) != 0) { P[j].next = Grid[key]; } Grid[key] = &P[j]; } } double distance(Point &a, Point &b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } double search(unordered_map<uint64_t, Point *> &Grid, double d, Point &P) { int x = floor(P.x / d), y = floor(P.y / d); uint64_t key; double minimum = 1e10; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { key = toKey(x + dx, y + dy); if (Grid.count(key)) { Point *p = Grid[key]; while (p != NULL) { if (minimum > distance(*p, P)) minimum = distance(*p, P); p = p->next; } } } } return minimum; } void insert(unordered_map<uint64_t, Point *> &Grid, double d, Point &P) { int x = floor(P.x / d), y = floor(P.y / d); uint64_t key = toKey(x, y); if (Grid.count(key) != 0) { P.next = Grid[key]; } Grid[key] = &P; } int main() { int n; cin >> n; Point P[n]; for (int i = 0; i < n; i++) { cin >> P[i].x >> P[i].y; P[i].next = NULL; } //??????????????£??????????????? random_device rd; mt19937 mt(rd()); //?????????????????§????????????????????????????????? uniform_int_distribution<int> dist(0, n - 1); for (int i = 0; i < n; i++) { int j = dist(mt); Point p = P[i]; P[i] = P[j]; P[j] = p; } unordered_map<uint64_t, Point *> Grid; double delta = distance(P[0], P[1]); build(Grid, delta, 1, P); for (int i = 2; i < n; i++) { double d2 = search(Grid, delta, P[i]); if (d2 >= delta) { insert(Grid, delta, P[i]); } else { delta = d2; build(Grid, delta, i, P); } } cout.precision(6); cout << fixed; cout << delta << endl; return 0; }
replace
92
93
92
93
TLE
p02303
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define MAX_N 100000 const double INF = 1e50; typedef pair<double, double> P; int N; P A[MAX_N]; bool compare_y(const P &a, const P &b) { return a.second < b.second; } double closest_pair(P *a, int n) { if (n <= 1) return INF; int m = n / 2; double x = a[m].first; double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (fabs(a[i].first - x >= d)) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = a[i].first - b[b.size() - j - 1].first; double dy = a[i].second - b[b.size() - j - 1].second; if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } void solve() { sort(A, A + N); printf("%.8f\n", closest_pair(A, N)); } int main() { cin >> N; for (int i = 0; i < N; i++) { scanf("%lf%lf", &A[i].first, &A[i].second); } solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX_N 100000 const double INF = 1e50; typedef pair<double, double> P; int N; P A[MAX_N]; bool compare_y(const P &a, const P &b) { return a.second < b.second; } double closest_pair(P *a, int n) { if (n <= 1) return INF; int m = n / 2; double x = a[m].first; double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (fabs(a[i].first - x) >= d) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = a[i].first - b[b.size() - j - 1].first; double dy = a[i].second - b[b.size() - j - 1].second; if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } void solve() { sort(A, A + N); printf("%.8f\n", closest_pair(A, N)); } int main() { cin >> N; for (int i = 0; i < N; i++) { scanf("%lf%lf", &A[i].first, &A[i].second); } solve(); return 0; }
replace
24
25
24
25
TLE
p02303
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <vector> using namespace std; typedef pair<double, double> pp; vector<pp> a; #define sqr(x) ((x) * (x)) double Plus(double x) { return x < 0 ? -x : x; } double dist(pp x, pp y) { return sqr(x.first - y.first) + sqr(x.second - y.second); } double go(int l, int r) { if (r - l == 1) return dist(a[l], a[r]); if (r - l == 2) return min(dist(a[l], a[l + 1]), min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2]))); int m = (l + r) / 2; double d = min(go(l, m), go(m + 1, r)); vector<pp> v; for (auto i = l; i <= r; i++) if (sqr(a[i].first - a[m].first) < d) v.emplace_back(a[i].second, a[i].first); sort(v.begin(), v.end()); for (auto i = v.begin(); i != v.end(); i++) for (auto j = i + 1; j != v.end(); j++) { d = min(d, dist(*i, *j)); } return d; } int main() { int i, n; double x, y; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lf%lf", &x, &y), a.emplace_back(x, y); sort(a.begin(), a.end()); printf("%lf ", sqrt(go(0, n - 1))); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <vector> using namespace std; typedef pair<double, double> pp; vector<pp> a; #define sqr(x) ((x) * (x)) double Plus(double x) { return x < 0 ? -x : x; } double dist(pp x, pp y) { return sqr(x.first - y.first) + sqr(x.second - y.second); } double go(int l, int r) { if (r - l == 1) return dist(a[l], a[r]); if (r - l == 2) return min(dist(a[l], a[l + 1]), min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2]))); int m = (l + r) / 2; double d = min(go(l, m), go(m + 1, r)); vector<pp> v; for (auto i = l; i <= r; i++) if (sqr(a[i].first - a[m].first) < d) v.emplace_back(a[i].second, a[i].first); sort(v.begin(), v.end()); for (auto i = v.begin(); i != v.end(); i++) for (auto j = i + 1; j != v.end(); j++) { if (sqr(j->first - i->first) > d) break; d = min(d, dist(*i, *j)); } return d; } int main() { int i, n; double x, y; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lf%lf", &x, &y), a.emplace_back(x, y); sort(a.begin(), a.end()); printf("%lf ", sqrt(go(0, n - 1))); }
insert
33
33
33
35
TLE
p02304
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; struct point { int x, y; point() {} point(int x, int y) : x(x), y(y) {} }; struct segment { point a, b; int c; // 用于排序,bottom>(left||right)>top segment(point a, point b, int c) : a(a), b(b), c(c) {} bool operator<(const segment &p) { if (a.y != p.a.y) return a.y < p.a.y; return c != p.c ? c < p.c : a.x < p.a.x; } }; vector<segment> v; int x[maxn]; int tree[maxn << 2]; void up(int p) { tree[p] = tree[p << 1] + tree[p << 1 | 1]; } void modify(int p, int l, int r, int x, int v) { if (l == r) { tree[p] += v; return; } int mid = l + r >> 1; if (x <= mid) { modify(p << 1, l, mid, x, v); } else { modify(p << 1 | 1, mid + 1, r, x, v); } up(p); } int query(int p, int l, int r, int x, int y) { if (x <= l && r <= y) { return tree[p]; } int mid = l + r >> 1; int res = 0; if (x <= mid) { res += query(p << 1, l, mid, x, y); } if (y > mid) { res += query(p << 1 | 1, mid + 1, r, x, y); } return res; } int main() { int n; scanf("%d", &n); int len = 1; for (int i = 0; i < n; i++) { point a, b; scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y); if (a.x != b.x) { if (a.x > b.x) swap(a, b); x[len++] = a.x; x[len++] = b.x; v.push_back(segment(a, b, 2)); } else { if (a.y > b.y) swap(a, b); x[len++] = a.x; v.push_back(segment(a, a, 1)); v.push_back(segment(b, b, 3)); } } len--; // printf("%d\n",len); sort(x + 1, x + len + 1); len = unique(x + 1, x + len + 1) - (x + 1); // for(int i=1;i<=len;i++){ // printf("%d ",x[i]); // } sort(v.begin(), v.end()); int vsize = v.size(); // for(int i=0;i<vsize;i++){ // printf("%d %d %d\n",v[i].a.x,v[i].a.y,v[i].c); // } int ans = 0; for (int i = 0; i < vsize; i++) { if (v[i].a.x == v[i].b.x) { int id = lower_bound(x + 1, x + len + 1, v[i].a.x) - x; // printf("--%d %d\n",v[i].a.x,id); int c = v[i].c == 3 ? -1 : 1; modify(1, 1, len, id, c); // printf("123123123\n"); } else { int id1 = lower_bound(x + 1, x + len + 1, v[i].a.x) - x; int id2 = lower_bound(x + 1, x + len + 1, v[i].b.x) - x; // printf("%d %d\n",v[i].a.x,id1); // printf("%d %d\n",v[i].b.x,id2); ans += query(1, 1, len, id1, id2); } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; struct point { int x, y; point() {} point(int x, int y) : x(x), y(y) {} }; struct segment { point a, b; int c; // 用于排序,bottom>(left||right)>top segment(point a, point b, int c) : a(a), b(b), c(c) {} bool operator<(const segment &p) { if (a.y != p.a.y) return a.y < p.a.y; return c != p.c ? c < p.c : a.x < p.a.x; } }; vector<segment> v; int x[maxn * 2]; int tree[maxn << 2]; void up(int p) { tree[p] = tree[p << 1] + tree[p << 1 | 1]; } void modify(int p, int l, int r, int x, int v) { if (l == r) { tree[p] += v; return; } int mid = l + r >> 1; if (x <= mid) { modify(p << 1, l, mid, x, v); } else { modify(p << 1 | 1, mid + 1, r, x, v); } up(p); } int query(int p, int l, int r, int x, int y) { if (x <= l && r <= y) { return tree[p]; } int mid = l + r >> 1; int res = 0; if (x <= mid) { res += query(p << 1, l, mid, x, y); } if (y > mid) { res += query(p << 1 | 1, mid + 1, r, x, y); } return res; } int main() { int n; scanf("%d", &n); int len = 1; for (int i = 0; i < n; i++) { point a, b; scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y); if (a.x != b.x) { if (a.x > b.x) swap(a, b); x[len++] = a.x; x[len++] = b.x; v.push_back(segment(a, b, 2)); } else { if (a.y > b.y) swap(a, b); x[len++] = a.x; v.push_back(segment(a, a, 1)); v.push_back(segment(b, b, 3)); } } len--; // printf("%d\n",len); sort(x + 1, x + len + 1); len = unique(x + 1, x + len + 1) - (x + 1); // for(int i=1;i<=len;i++){ // printf("%d ",x[i]); // } sort(v.begin(), v.end()); int vsize = v.size(); // for(int i=0;i<vsize;i++){ // printf("%d %d %d\n",v[i].a.x,v[i].a.y,v[i].c); // } int ans = 0; for (int i = 0; i < vsize; i++) { if (v[i].a.x == v[i].b.x) { int id = lower_bound(x + 1, x + len + 1, v[i].a.x) - x; // printf("--%d %d\n",v[i].a.x,id); int c = v[i].c == 3 ? -1 : 1; modify(1, 1, len, id, c); // printf("123123123\n"); } else { int id1 = lower_bound(x + 1, x + len + 1, v[i].a.x) - x; int id2 = lower_bound(x + 1, x + len + 1, v[i].b.x) - x; // printf("%d %d\n",v[i].a.x,id1); // printf("%d %d\n",v[i].b.x,id2); ans += query(1, 1, len, id1, id2); } } printf("%d\n", ans); return 0; }
replace
27
28
27
28
0
p02304
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <vector> using namespace std; struct Point { double x, y; }; struct Segment { Point p1, p2; }; #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 class EndPoint { public: Point p; int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { if (p.y == ep.p.y) { return st < ep.st; } else return p.y < ep.p.y; } }; EndPoint EP[2 * 100000]; int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.y > S[i].p2.x) swap(S[i].p1, S[i].p2); } else if (S[i].p1.y > S[i].p2.y) swap(S[i].p1, S[i].p2); if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } sort(EP, EP + (2 * n)); set<int> BT; BT.insert(1000000001); int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x); set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x); cnt += distance(b, e); } } return cnt; } int main() { int n; vector<Segment> s; scanf("%d", &n); for (int i = 0; i < n; i++) { Segment x; scanf("%lf %lf %lf %lf", &x.p1.x, &x.p1.y, &x.p2.x, &x.p2.y); s.push_back(x); } printf("%d\n", manhattanIntersection(s)); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <vector> using namespace std; struct Point { double x, y; }; struct Segment { Point p1, p2; }; #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 class EndPoint { public: Point p; int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { if (p.y == ep.p.y) { return st < ep.st; } else return p.y < ep.p.y; } }; EndPoint EP[2 * 100000]; int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.x > S[i].p2.x) swap(S[i].p1, S[i].p2); } else if (S[i].p1.y > S[i].p2.y) swap(S[i].p1, S[i].p2); if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } sort(EP, EP + (2 * n)); set<int> BT; BT.insert(1000000001); int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x); set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x); cnt += distance(b, e); } } return cnt; } int main() { int n; vector<Segment> s; scanf("%d", &n); for (int i = 0; i < n; i++) { Segment x; scanf("%lf %lf %lf %lf", &x.p1.x, &x.p1.y, &x.p2.x, &x.p2.y); s.push_back(x); } printf("%d\n", manhattanIntersection(s)); return 0; }
replace
43
44
43
44
TLE
p02304
C++
Runtime Error
// 線分の交差問題 #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <queue> #include <set> #include <vector> using namespace std; // 浮動小数点のゼロ判定 #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) // 点を表すクラス class Point { public: double x, y; // コンストラクタ Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} // -----▼▼▼ 演算子のオーバーロード ▼▼▼----- Point operator+(Point &p) { return Point(x + p.x, y + p.y); } Point operator-(Point &p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator<=(const Point &p) const { return x != p.x ? x <= p.x : y <= p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } // -----▲▲▲ 演算子のオーバーロード ▲▲▲----- // 2点間の距離の算出 double distance() { return sqrt(norm()); } double norm() { return (x * x) + (y * y); } }; // 線分を表すクラス class Segment { public: Point p1, p2; // コンストラクタ Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; // 端点の種類 #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 class EndPoint { public: Point p; // 入力線分のID, 端点の種類 int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { // y座標が小さい順に整列 if (p.y == ep.p.y) { // yが同一の場合は, 下端点, 左端点, 右端点, 上端点の順に並べる return st < ep.st; } else { return p.y < ep.p.y; } } }; // 端点のリスト // EndPoint EP[2 * 100000]; EndPoint EP[2 * 100]; // 線分交差問題 : マンハッタン幾何 int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { // 端点 p1, p2 が左下を基準に並ぶように調整 if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.x > S[i].p2.x) { swap(S[i].p1, S[i].p2); } } else if (S[i].p1.y > S[i].p2.y) { swap(S[i].p1, S[i].p2); } // 水平成分を端点リストに追加 if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { // 垂直成分を端点リストに追加 EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } // 端点の y 座標に関して昇順に整列 sort(EP, EP + (2 * n)); set<int> BT; // 二分探索木 BT.insert(1000000001); // 番兵を設置 int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { // 上端点を削除 BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { // 下端点を追加 BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { // O(log n) set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x); // O(log n) set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x); // b と e の距離(点の数)を加算, O(k) cnt += distance(b, e); } } return cnt; } int main(void) { int n, count; Point start, end; vector<Segment> v_segment; cin >> n; for (int i = 0; i < n; i++) { cin >> start.x >> start.y >> end.x >> end.y; // 線分の集合を作成 Segment seg(start, end); v_segment.push_back(seg); } // 結果の表示 count = manhattanIntersection(v_segment); printf("%d\n", count); return 0; }
// 線分の交差問題 #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <queue> #include <set> #include <vector> using namespace std; // 浮動小数点のゼロ判定 #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) // 点を表すクラス class Point { public: double x, y; // コンストラクタ Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} // -----▼▼▼ 演算子のオーバーロード ▼▼▼----- Point operator+(Point &p) { return Point(x + p.x, y + p.y); } Point operator-(Point &p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator<=(const Point &p) const { return x != p.x ? x <= p.x : y <= p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } // -----▲▲▲ 演算子のオーバーロード ▲▲▲----- // 2点間の距離の算出 double distance() { return sqrt(norm()); } double norm() { return (x * x) + (y * y); } }; // 線分を表すクラス class Segment { public: Point p1, p2; // コンストラクタ Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; // 端点の種類 #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 class EndPoint { public: Point p; // 入力線分のID, 端点の種類 int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { // y座標が小さい順に整列 if (p.y == ep.p.y) { // yが同一の場合は, 下端点, 左端点, 右端点, 上端点の順に並べる return st < ep.st; } else { return p.y < ep.p.y; } } }; // 端点のリスト EndPoint EP[2 * 100000]; // EndPoint EP[2 * 100]; // 線分交差問題 : マンハッタン幾何 int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { // 端点 p1, p2 が左下を基準に並ぶように調整 if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.x > S[i].p2.x) { swap(S[i].p1, S[i].p2); } } else if (S[i].p1.y > S[i].p2.y) { swap(S[i].p1, S[i].p2); } // 水平成分を端点リストに追加 if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { // 垂直成分を端点リストに追加 EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } // 端点の y 座標に関して昇順に整列 sort(EP, EP + (2 * n)); set<int> BT; // 二分探索木 BT.insert(1000000001); // 番兵を設置 int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { // 上端点を削除 BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { // 下端点を追加 BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { // O(log n) set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x); // O(log n) set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x); // b と e の距離(点の数)を加算, O(k) cnt += distance(b, e); } } return cnt; } int main(void) { int n, count; Point start, end; vector<Segment> v_segment; cin >> n; for (int i = 0; i < n; i++) { cin >> start.x >> start.y >> end.x >> end.y; // 線分の集合を作成 Segment seg(start, end); v_segment.push_back(seg); } // 結果の表示 count = manhattanIntersection(v_segment); printf("%d\n", count); return 0; }
replace
90
92
90
92
0
p02305
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define X first #define Y second #define eps 0.00000000001 #define prid(x) printf("%.15lf\n", x) double xmax, xmin, ymax, ymin; struct vec { double x, y; vec operator+(const vec &a) const { return (vec){x + a.x, y + a.y}; } vec operator-(const vec &a) const { return (vec){x - a.x, y - a.y}; } vec sca(double t) { return (vec){t * x, t * y}; } double dot(vec a) { return x * a.x + y * a.y; } double cross(vec a) { return x * a.y - y * a.x; } double norm() { return sqrt(x * x + y * y); } double norm2() { return x * x + y * y; } // double ppdist(vec p){ return sqrt( (p.x - x) * (p.x - x) + (p.y - y) * (p.y // - y) ); } double ppdist2(vec p){ return (p.x - x) * (p.x - x) + (p.y - y) * // (p.y - y); } }; struct line { vec a, b; vec getvec() { return b - a; } vec proj(vec p) { return a + (b - a).sca((p - a).dot(b - a) / (b - a).dot(b - a)); } vec vref(vec p) { return proj(p).sca(2.0) - p; } int ccw(vec p) { vec q = p - a, ba = b - a; if (ba.cross(q) > 0) return 1; // ccw if (ba.cross(q) < 0) return -1; // cw if (ba.dot(q) < 0) return -2; // back if (ba.dot(ba) < q.dot(q)) return 2; // front return 0; // on } bool paral(line l) { return abs(l.getvec().cross(getvec())) < eps; } bool orth(line l) { return abs(l.getvec().dot(getvec())) < eps; } bool intersec(line l) { bool res0 = (ccw(l.a) * ccw(l.b) == 4); // syukutai bool res1 = (getvec().cross(l.a - a) * getvec().cross(l.b - a)) <= eps; bool res2 = (l.getvec().cross(a - l.a) * l.getvec().cross(b - l.a)) <= eps; return !res0 && res1 && res2; } vec crosspoint(line l) { return a + getvec().sca((l.a - a).cross(l.getvec()) / getvec().cross(l.getvec())); } double pldist(vec p) { double res = min((a - p).norm2(), (b - p).norm2()); vec h = proj(p); if ((a - h).dot(b - h) < 0) res = min(res, (h - p).norm2()); return sqrt(res); } double lldist(line l) { if (intersec(l)) return 0.0; return min(min(pldist(l.a), pldist(l.b)), min(l.pldist(a), l.pldist(b))); } }; struct polygon { vector<vec> p; // ccw double area() { double res = 0.0; for (int i = 0; i < p.size(); ++i) res += p[i].cross(p[(i + 1) % p.size()]); return res / 2.0; } bool isconv() { for (int i = 0; i < p.size(); ++i) if ((p[(i + 1) % p.size()] - p[i]).cross(p[(i + 2) % p.size()] - p[i]) < -eps) return false; return true; } int isin(vec a) { line l = (line){a, a + (vec){xmax - xmin, 0.0}}; int cnt = 0, n = p.size(); for (int i = 0; i < n; ++i) { line tmp = (line){p[i], p[(i + 1) % n]}; if (tmp.ccw(a) == 0) return 1; // on line if (l.intersec(tmp)) { ++cnt; if (l.ccw(p[i]) == 0 && l.ccw(p[(i + 1) % n]) * l.ccw(p[(i + n - 1) % n]) == -1) ++cnt; if (l.paral(tmp)) { ++cnt; if (l.ccw(p[(i + 2) % n]) * l.ccw(p[(i + n - 1) % n]) == -1) ++cnt; } } } return (cnt & 1) * 2; // 2:in 0:out } }; struct circle { vec o; double r; int intersec(circle c) { double d = (o - c.o).norm(), rp = r + c.r, rm = abs(r - c.r); if (abs(rp - d) < eps) return 3; if (rp < d + eps) return 4; if (abs(rm - d) < eps) return 1; if (rm < d + eps) return 2; return 0; } }; signed main(void) { int i, j, k; for (;;) { circle c, d; cin >> c.o.x >> c.o.y >> c.r >> d.o.x >> d.o.y >> d.r; pri(c.intersec(d)); } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define X first #define Y second #define eps 0.00000000001 #define prid(x) printf("%.15lf\n", x) double xmax, xmin, ymax, ymin; struct vec { double x, y; vec operator+(const vec &a) const { return (vec){x + a.x, y + a.y}; } vec operator-(const vec &a) const { return (vec){x - a.x, y - a.y}; } vec sca(double t) { return (vec){t * x, t * y}; } double dot(vec a) { return x * a.x + y * a.y; } double cross(vec a) { return x * a.y - y * a.x; } double norm() { return sqrt(x * x + y * y); } double norm2() { return x * x + y * y; } // double ppdist(vec p){ return sqrt( (p.x - x) * (p.x - x) + (p.y - y) * (p.y // - y) ); } double ppdist2(vec p){ return (p.x - x) * (p.x - x) + (p.y - y) * // (p.y - y); } }; struct line { vec a, b; vec getvec() { return b - a; } vec proj(vec p) { return a + (b - a).sca((p - a).dot(b - a) / (b - a).dot(b - a)); } vec vref(vec p) { return proj(p).sca(2.0) - p; } int ccw(vec p) { vec q = p - a, ba = b - a; if (ba.cross(q) > 0) return 1; // ccw if (ba.cross(q) < 0) return -1; // cw if (ba.dot(q) < 0) return -2; // back if (ba.dot(ba) < q.dot(q)) return 2; // front return 0; // on } bool paral(line l) { return abs(l.getvec().cross(getvec())) < eps; } bool orth(line l) { return abs(l.getvec().dot(getvec())) < eps; } bool intersec(line l) { bool res0 = (ccw(l.a) * ccw(l.b) == 4); // syukutai bool res1 = (getvec().cross(l.a - a) * getvec().cross(l.b - a)) <= eps; bool res2 = (l.getvec().cross(a - l.a) * l.getvec().cross(b - l.a)) <= eps; return !res0 && res1 && res2; } vec crosspoint(line l) { return a + getvec().sca((l.a - a).cross(l.getvec()) / getvec().cross(l.getvec())); } double pldist(vec p) { double res = min((a - p).norm2(), (b - p).norm2()); vec h = proj(p); if ((a - h).dot(b - h) < 0) res = min(res, (h - p).norm2()); return sqrt(res); } double lldist(line l) { if (intersec(l)) return 0.0; return min(min(pldist(l.a), pldist(l.b)), min(l.pldist(a), l.pldist(b))); } }; struct polygon { vector<vec> p; // ccw double area() { double res = 0.0; for (int i = 0; i < p.size(); ++i) res += p[i].cross(p[(i + 1) % p.size()]); return res / 2.0; } bool isconv() { for (int i = 0; i < p.size(); ++i) if ((p[(i + 1) % p.size()] - p[i]).cross(p[(i + 2) % p.size()] - p[i]) < -eps) return false; return true; } int isin(vec a) { line l = (line){a, a + (vec){xmax - xmin, 0.0}}; int cnt = 0, n = p.size(); for (int i = 0; i < n; ++i) { line tmp = (line){p[i], p[(i + 1) % n]}; if (tmp.ccw(a) == 0) return 1; // on line if (l.intersec(tmp)) { ++cnt; if (l.ccw(p[i]) == 0 && l.ccw(p[(i + 1) % n]) * l.ccw(p[(i + n - 1) % n]) == -1) ++cnt; if (l.paral(tmp)) { ++cnt; if (l.ccw(p[(i + 2) % n]) * l.ccw(p[(i + n - 1) % n]) == -1) ++cnt; } } } return (cnt & 1) * 2; // 2:in 0:out } }; struct circle { vec o; double r; int intersec(circle c) { double d = (o - c.o).norm(), rp = r + c.r, rm = abs(r - c.r); if (abs(rp - d) < eps) return 3; if (rp < d + eps) return 4; if (abs(rm - d) < eps) return 1; if (rm < d + eps) return 2; return 0; } }; signed main(void) { int i, j, k; circle c, d; cin >> c.o.x >> c.o.y >> c.r >> d.o.x >> d.o.y >> d.r; pri(c.intersec(d)); return 0; }
replace
183
188
183
186
TLE
p02308
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; #define eps (1e-10) struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(Point p) { return Point(x * p.x - y * p.y, x * p.y + y * p.x); } Point operator*(double k) { return Point(x * k, y * k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } bool operator==(const Point &p) const { return fabs(x - p.x) < eps && fabs(y - p.y) < eps; } double arg() { return atan2(y, x); } double dot(Point p) { return x * p.x + y * p.y; } double det(Point p) { return x * p.y - y * p.x; } }; bool cmp_x(const Point &p, const Point &q) { if (p.x != q.x) return p.x < q.x; return p.y < q.y; } struct Line { Point p1, p2; Line() {} Line(Point p1, Point p2) : p1(p1), p2(p2) {} }; struct Circle { double r; Point p; Circle() {} Circle(Point p, double r) : p(p), r(r) {} }; int ccw(Point a, Point b, Point c) { Point t1 = b - a, t2 = c - a; if (t1.det(t2) > eps) return 1; // counter clockwise if (t1.det(t2) < -eps) return -1; // clockwise if (t1.dot(t2) < -eps) return 2; // c-a-b online if (t1.norm() < t2.norm()) return -2; // a-b-c online return 0; // a-c-b online } Point project(Line l, Point p) { Point base = l.p2 - l.p1; double r = (p - l.p1).dot(base) / base.norm(); return l.p1 + base * r; } double distance(Line l, Point p) { Point r = project(l, p); if (abs(ccw(l.p1, l.p2, r)) == 0) return (p - r).abs(); // projection not online else return min((p - l.p1).abs(), (p - l.p2).abs()); } bool isIntersectCC(Circle c1, Circle c2) { return (c1.p - c2.p).abs() <= c1.r + c2.r + eps; } bool isIntersectCL(Circle c, Line l) { return distance(l, c.p) <= c.r + eps; } pair<Point, Point> CrossPointsCC(Circle c1, Circle c2) { assert(isIntersectCC(c1, c2)); double d = (c1.p - c2.p).abs(); double k = acos((d * d + c1.r * c1.r - c2.r * c2.r) / (c1.r * d * 2)); return make_pair(c1.p + (c2.p - c1.p) * Point(cos(k), sin(k)) * (c1.r / d), c1.p + (c2.p - c1.p) * Point(cos(-k), sin(-k)) * (c1.r / d)); } pair<Point, Point> CrossPointsCL(Circle c, Line l) { assert(isIntersectCL(c, l)); Point p = project(l, c.p); double q = sqrt(c.r * c.r - (p - c.p).norm()); Point e = (l.p2 - l.p1) * (1.0 / (l.p2 - l.p1).abs()); return make_pair(p + e * q, p - e * q); } int main() { double cx, cy, r; double x1, y1, x2, y2; int q; cin >> cx >> cy >> r; Circle c(Point(cx, cy), r); cin >> q; rep(i, q) { cin >> x1 >> y1 >> x2 >> y2; Line l(Point(x1, y1), Point(x2, y2)); pair<Point, Point> pp = CrossPointsCL(c, l); if (!cmp_x(pp.first, pp.second)) swap(pp.first, pp.second); cout << fixed << setprecision(12) << pp.first.x << " " << pp.first.y << " " << pp.second.x << " " << pp.second.y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; #define eps (1e-10) struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(Point p) { return Point(x * p.x - y * p.y, x * p.y + y * p.x); } Point operator*(double k) { return Point(x * k, y * k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } bool operator==(const Point &p) const { return fabs(x - p.x) < eps && fabs(y - p.y) < eps; } double arg() { return atan2(y, x); } double dot(Point p) { return x * p.x + y * p.y; } double det(Point p) { return x * p.y - y * p.x; } }; bool cmp_x(const Point &p, const Point &q) { if (p.x != q.x) return p.x < q.x; return p.y < q.y; } struct Line { Point p1, p2; Line() {} Line(Point p1, Point p2) : p1(p1), p2(p2) {} }; struct Circle { double r; Point p; Circle() {} Circle(Point p, double r) : p(p), r(r) {} }; int ccw(Point a, Point b, Point c) { Point t1 = b - a, t2 = c - a; if (t1.det(t2) > eps) return 1; // counter clockwise if (t1.det(t2) < -eps) return -1; // clockwise if (t1.dot(t2) < -eps) return 2; // c-a-b online if (t1.norm() < t2.norm()) return -2; // a-b-c online return 0; // a-c-b online } Point project(Line l, Point p) { Point base = l.p2 - l.p1; double r = (p - l.p1).dot(base) / base.norm(); return l.p1 + base * r; } double distance(Line l, Point p) { Point r = project(l, p); return (p - r).abs(); /* if(abs(ccw(l.p1,l.p2,r))==0) return (p-r).abs();//projection not online else return min((p-l.p1).abs(),(p-l.p2).abs()); */ } bool isIntersectCC(Circle c1, Circle c2) { return (c1.p - c2.p).abs() <= c1.r + c2.r + eps; } bool isIntersectCL(Circle c, Line l) { return distance(l, c.p) <= c.r + eps; } pair<Point, Point> CrossPointsCC(Circle c1, Circle c2) { assert(isIntersectCC(c1, c2)); double d = (c1.p - c2.p).abs(); double k = acos((d * d + c1.r * c1.r - c2.r * c2.r) / (c1.r * d * 2)); return make_pair(c1.p + (c2.p - c1.p) * Point(cos(k), sin(k)) * (c1.r / d), c1.p + (c2.p - c1.p) * Point(cos(-k), sin(-k)) * (c1.r / d)); } pair<Point, Point> CrossPointsCL(Circle c, Line l) { assert(isIntersectCL(c, l)); Point p = project(l, c.p); double q = sqrt(c.r * c.r - (p - c.p).norm()); Point e = (l.p2 - l.p1) * (1.0 / (l.p2 - l.p1).abs()); return make_pair(p + e * q, p - e * q); } int main() { double cx, cy, r; double x1, y1, x2, y2; int q; cin >> cx >> cy >> r; Circle c(Point(cx, cy), r); cin >> q; rep(i, q) { cin >> x1 >> y1 >> x2 >> y2; Line l(Point(x1, y1), Point(x2, y2)); pair<Point, Point> pp = CrossPointsCL(c, l); if (!cmp_x(pp.first, pp.second)) swap(pp.first, pp.second); cout << fixed << setprecision(12) << pp.first.x << " " << pp.first.y << " " << pp.second.x << " " << pp.second.y << endl; } return 0; }
replace
63
67
63
68
0
p02308
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef complex<double> V; typedef vector<P> vecP; typedef pair<P, P> L; typedef pair<P, P> S; typedef pair<P, double> C; const double eps = 1e-8; const double PI = acos(-1); const double PI2 = PI * 2.0; namespace std { bool operator<(const P &a, const P &b) { return (a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std V normal(V a) { assert(abs(a) > 0); return a / abs(a); } double Sqrt(double x) { if (x < 0) return 0; else return sqrt(x); } P Vector(L a) { return a.second - a.first; } bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (eq(a.real(), b.real()) && eq(a.imag(), b.imag())); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } double getArg(P a, P b) { return arg(b * conj(a)); } double getTime(V a, V b) { assert(eq(cross(a, b), 0)); return (dot(a, b) < 0 ? -1.0 : 1.0) * abs(b) / abs(a); } P project(P a, P b, P c) { b -= a, c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a, c -= a; return a + b * conj(c / b); } int ccw(P a, P b, P c) { P ab = b - a, ac = c - a; P k = ac * conj(ab); if (k.imag() > 0) return 1; if (k.imag() < 0) return -1; if (k.real() < 0) return 2; if (abs(ab) < abs(ac)) return -2; return 0; } bool isParallel(P a, P b) { return eq(0, cross(a, b)); } bool isParallel(S a, S b) { return eq(0, cross(Vector(a), Vector(b))); } bool onLP(L l, P p) { P a = l.first, b = l.second; return eq(0, cross(b - a, p - a)); } bool onSP(S s, P p) { P a = s.first, b = s.second; return eq(abs(b - a), abs(a - p) + abs(b - p)); } bool isCrossSS(S s0, S s1) { P a = s0.first, b = s0.second; P c = s1.first, d = s1.second; int f0 = ccw(a, b, c) * ccw(a, b, d); int f1 = ccw(c, d, a) * ccw(c, d, b); return (f0 <= 0 && f1 <= 0); } bool isCrossLS(L l, S s) { P a = l.first, b = l.second; P c = s.first, d = s.second; return (ccw(a, b, c) * ccw(a, b, d) <= 0); } double distLP(L l, P p) { P a = l.first, b = l.second; double res = cross(b - a, p - a) / abs(b - a); return abs(res); } double distSP(S s, P p) { P a = s.first, b = s.second; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return distLP(s, p); } double distSS(S s0, S s1) { if (isCrossSS(s0, s1)) return 0; double res0 = min(distSP(s0, s1.first), distSP(s0, s1.second)); double res1 = min(distSP(s1, s0.first), distSP(s1, s0.second)); return min(res0, res1); } P getCrossLL(L l0, L l1) { P a = l0.first, b = l0.second; P c = l1.first, d = l1.second; a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } int inPolygon(vecP &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; sum += getArg(a - p, b - p); } if (abs(sum) < eps) return 0; else return 2; } vecP andrewScan(vecP &t) { int N = t.size(), C = 0; vecP R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vecP res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vecP convexHull(vecP &t) { sort(t.begin(), t.end()); vecP u = andrewScan(t); reverse(t.begin(), t.end()); vecP l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vecP cutConvex(vecP &t, L l) { P a = l.first, b = l.second; int N = t.size(); vecP res; for (int i = 0; i < N; i++) { P c = t[i], d = t[(i + 1) % N]; int C = ccw(a, b, c), D = ccw(a, b, d); if (C != -1) res.push_back(c); if (C == -D && abs(C) == 1) res.push_back(getCrossLL(l, L(c, d))); } return res; } P getVector(const vecP &t, int id) { int n = t.size(); return t[(id + 1) % n] - t[id % n]; } double convex_diameter(vecP &t) { int n = t.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(t[i]) > imag(t[is])) is = i; if (imag(t[i]) < imag(t[js])) js = i; } double maxd = norm(t[is] - t[js]); int i, maxi, j, maxj; i = maxi = is; j = maxj = js; do { if (cross(getVector(t, i), getVector(t, j)) >= 0) j = (j + 1) % n; else i = (i + 1) % n; if (norm(t[i] - t[j]) > maxd) { maxd = norm(t[i] - t[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxd); /* farthest pair is (maxi, maxj). */ } bool compare_y(const P &a, const P &b) { return a.imag() < b.imag(); } double closest_pair(P *a, int n) { if (n <= 1) return 1e30; int m = n / 2; double x = a[m].real(); double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (abs(a[i].real() - x) >= d) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = real(a[i] - b[b.size() - j - 1]); double dy = imag(a[i] - b[b.size() - j - 1]); if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } P _pool[200005]; double minDist(vecP &t) { int n = t.size(); for (int i = 0; i < n; i++) _pool[i] = t[i]; sort(_pool, _pool + n); return closest_pair(_pool, n); } int getStateCC(C a, C b) { double ar = a.second, br = b.second; double dist = abs(a.first - b.first); if (dist > ar + br + eps) return 4; if (dist > ar + br - eps) return 3; if (dist > abs(ar - br) + eps) return 2; if (dist > abs(ar - br) - eps) return 1; return 0; } P getCrossCC(C a, C b) { P p1 = a.first, p2 = a.second; double r1 = a.second, r2 = b.second; double cA = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * P(cA, Sqrt(1.0 - cA * cA)); } S getTangentCP(C a, P p) { P base = a.first - p; double ar = a.second; double w = Sqrt(norm(base) - ar * ar); P s = p + base * P(w, ar) / norm(base) * w; P t = p + base * P(w, -ar) / norm(base) * w; return S(s, t); } S getInTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double w = ar + br; double h = Sqrt(norm(base) - w * w); P k = base * P(w, h * flg) / norm(base); return S(ap + k * ar, bp - k * br); } S getOutTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double h = br - ar; double w = Sqrt(norm(base) - h * h); P k = base * P(w, h * flg) / norm(base) * P(0, flg); return S(ap + k * ar, bp + k * br); } vector<S> getTangent(C a, C b) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; vector<S> res; double dist = abs(ap - bp); if (dist > ar + br + eps) res.push_back(getInTangent(a, b, 1)); if (dist > ar + br - eps) res.push_back(getInTangent(a, b, -1)); if (dist > abs(ar - br) + eps) res.push_back(getOutTangent(a, b, 1)); if (dist > abs(ar - br) - eps) res.push_back(getOutTangent(a, b, -1)); return res; } vecP getCrossCL(C cir, L l) { P a = l.first, b = l.second; double cr = cir.second; P cp = cir.first; vecP res; P base = b - a, target = project(a, b, cp); double length = abs(base), h = abs(cp - target); base /= length; if (cr + eps < h) return res; double w = Sqrt(cr * cr - h * h); double L = getTime(normal(b - a), target - a) - w, R = L + w * 2.0; res.push_back(a + base * L); if (eq(L, R)) return res; res.push_back(a + base * R); return res; } vecP getCrossCS(C cir, S s) { vecP tmp = getCrossCL(cir, s); vecP res; for (int i = 0; i < (int)tmp.size(); i++) if (ccw(s.first, s.second, tmp[i]) == 0) res.push_back(tmp[i]); return res; } double getArea(C c, P a, P b) { P cp = c.first; double cr = c.second; P va = cp - a, vb = cp - b; double A = abs(va), B = abs(vb); double f = cross(va, vb), d = distSP(S(a, b), cp), res = 0; if (eq(0, f)) return 0; if (A < cr + eps && B < cr + eps) return f * 0.5; if (d > cr - eps) return cr * cr * PI * getArg(va, vb) / PI2; vecP u = getCrossCS(c, S(a, b)); assert(!u.empty()); u.insert(u.begin(), a), u.push_back(b); for (int i = 0; i + 1 < (int)u.size(); i++) res += getArea(c, u[i], u[i + 1]); return res; } double getCrossArea(vecP t, C c) { int n = t.size(); if (n < 3) return 0; double res = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1) % n]; res += getArea(c, a, b); } return res; } double calcArea(const vecP &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) { res += cross(t[(i + 1) % n], t[i]); } return abs(res) * 0.5; } P input() { double x, y; cin >> x >> y; return P(x, y); } void pr(P p, string str) { printf("%.10f %.10f", p.real(), p.imag()); cout << str; } int main() { C a, b; a.first = input(); cin >> a.second; int n; cin >> n; while (n--) { P s = input(); P t = input(); vecP ans = getCrossCS(a, L(s, t)); if (ans.size() == 1) ans.push_back(ans[0]); sort(ans.begin(), ans.end()); pr(ans[0], " "); pr(ans[1], "\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef complex<double> V; typedef vector<P> vecP; typedef pair<P, P> L; typedef pair<P, P> S; typedef pair<P, double> C; const double eps = 1e-8; const double PI = acos(-1); const double PI2 = PI * 2.0; namespace std { bool operator<(const P &a, const P &b) { return (a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std V normal(V a) { assert(abs(a) > 0); return a / abs(a); } double Sqrt(double x) { if (x < 0) return 0; else return sqrt(x); } P Vector(L a) { return a.second - a.first; } bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (eq(a.real(), b.real()) && eq(a.imag(), b.imag())); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } double getArg(P a, P b) { return arg(b * conj(a)); } double getTime(V a, V b) { assert(eq(cross(a, b), 0)); return (dot(a, b) < 0 ? -1.0 : 1.0) * abs(b) / abs(a); } P project(P a, P b, P c) { b -= a, c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a, c -= a; return a + b * conj(c / b); } int ccw(P a, P b, P c) { P ab = b - a, ac = c - a; P k = ac * conj(ab); if (k.imag() > 0) return 1; if (k.imag() < 0) return -1; if (k.real() < 0) return 2; if (abs(ab) < abs(ac)) return -2; return 0; } bool isParallel(P a, P b) { return eq(0, cross(a, b)); } bool isParallel(S a, S b) { return eq(0, cross(Vector(a), Vector(b))); } bool onLP(L l, P p) { P a = l.first, b = l.second; return eq(0, cross(b - a, p - a)); } bool onSP(S s, P p) { P a = s.first, b = s.second; return eq(abs(b - a), abs(a - p) + abs(b - p)); } bool isCrossSS(S s0, S s1) { P a = s0.first, b = s0.second; P c = s1.first, d = s1.second; int f0 = ccw(a, b, c) * ccw(a, b, d); int f1 = ccw(c, d, a) * ccw(c, d, b); return (f0 <= 0 && f1 <= 0); } bool isCrossLS(L l, S s) { P a = l.first, b = l.second; P c = s.first, d = s.second; return (ccw(a, b, c) * ccw(a, b, d) <= 0); } double distLP(L l, P p) { P a = l.first, b = l.second; double res = cross(b - a, p - a) / abs(b - a); return abs(res); } double distSP(S s, P p) { P a = s.first, b = s.second; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return distLP(s, p); } double distSS(S s0, S s1) { if (isCrossSS(s0, s1)) return 0; double res0 = min(distSP(s0, s1.first), distSP(s0, s1.second)); double res1 = min(distSP(s1, s0.first), distSP(s1, s0.second)); return min(res0, res1); } P getCrossLL(L l0, L l1) { P a = l0.first, b = l0.second; P c = l1.first, d = l1.second; a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } int inPolygon(vecP &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; sum += getArg(a - p, b - p); } if (abs(sum) < eps) return 0; else return 2; } vecP andrewScan(vecP &t) { int N = t.size(), C = 0; vecP R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vecP res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vecP convexHull(vecP &t) { sort(t.begin(), t.end()); vecP u = andrewScan(t); reverse(t.begin(), t.end()); vecP l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vecP cutConvex(vecP &t, L l) { P a = l.first, b = l.second; int N = t.size(); vecP res; for (int i = 0; i < N; i++) { P c = t[i], d = t[(i + 1) % N]; int C = ccw(a, b, c), D = ccw(a, b, d); if (C != -1) res.push_back(c); if (C == -D && abs(C) == 1) res.push_back(getCrossLL(l, L(c, d))); } return res; } P getVector(const vecP &t, int id) { int n = t.size(); return t[(id + 1) % n] - t[id % n]; } double convex_diameter(vecP &t) { int n = t.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(t[i]) > imag(t[is])) is = i; if (imag(t[i]) < imag(t[js])) js = i; } double maxd = norm(t[is] - t[js]); int i, maxi, j, maxj; i = maxi = is; j = maxj = js; do { if (cross(getVector(t, i), getVector(t, j)) >= 0) j = (j + 1) % n; else i = (i + 1) % n; if (norm(t[i] - t[j]) > maxd) { maxd = norm(t[i] - t[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxd); /* farthest pair is (maxi, maxj). */ } bool compare_y(const P &a, const P &b) { return a.imag() < b.imag(); } double closest_pair(P *a, int n) { if (n <= 1) return 1e30; int m = n / 2; double x = a[m].real(); double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (abs(a[i].real() - x) >= d) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = real(a[i] - b[b.size() - j - 1]); double dy = imag(a[i] - b[b.size() - j - 1]); if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } P _pool[200005]; double minDist(vecP &t) { int n = t.size(); for (int i = 0; i < n; i++) _pool[i] = t[i]; sort(_pool, _pool + n); return closest_pair(_pool, n); } int getStateCC(C a, C b) { double ar = a.second, br = b.second; double dist = abs(a.first - b.first); if (dist > ar + br + eps) return 4; if (dist > ar + br - eps) return 3; if (dist > abs(ar - br) + eps) return 2; if (dist > abs(ar - br) - eps) return 1; return 0; } P getCrossCC(C a, C b) { P p1 = a.first, p2 = a.second; double r1 = a.second, r2 = b.second; double cA = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * P(cA, Sqrt(1.0 - cA * cA)); } S getTangentCP(C a, P p) { P base = a.first - p; double ar = a.second; double w = Sqrt(norm(base) - ar * ar); P s = p + base * P(w, ar) / norm(base) * w; P t = p + base * P(w, -ar) / norm(base) * w; return S(s, t); } S getInTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double w = ar + br; double h = Sqrt(norm(base) - w * w); P k = base * P(w, h * flg) / norm(base); return S(ap + k * ar, bp - k * br); } S getOutTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double h = br - ar; double w = Sqrt(norm(base) - h * h); P k = base * P(w, h * flg) / norm(base) * P(0, flg); return S(ap + k * ar, bp + k * br); } vector<S> getTangent(C a, C b) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; vector<S> res; double dist = abs(ap - bp); if (dist > ar + br + eps) res.push_back(getInTangent(a, b, 1)); if (dist > ar + br - eps) res.push_back(getInTangent(a, b, -1)); if (dist > abs(ar - br) + eps) res.push_back(getOutTangent(a, b, 1)); if (dist > abs(ar - br) - eps) res.push_back(getOutTangent(a, b, -1)); return res; } vecP getCrossCL(C cir, L l) { P a = l.first, b = l.second; double cr = cir.second; P cp = cir.first; vecP res; P base = b - a, target = project(a, b, cp); double length = abs(base), h = abs(cp - target); base /= length; if (cr + eps < h) return res; double w = Sqrt(cr * cr - h * h); double L = getTime(normal(b - a), target - a) - w, R = L + w * 2.0; res.push_back(a + base * L); if (eq(L, R)) return res; res.push_back(a + base * R); return res; } vecP getCrossCS(C cir, S s) { vecP tmp = getCrossCL(cir, s); vecP res; for (int i = 0; i < (int)tmp.size(); i++) if (ccw(s.first, s.second, tmp[i]) == 0) res.push_back(tmp[i]); return res; } double getArea(C c, P a, P b) { P cp = c.first; double cr = c.second; P va = cp - a, vb = cp - b; double A = abs(va), B = abs(vb); double f = cross(va, vb), d = distSP(S(a, b), cp), res = 0; if (eq(0, f)) return 0; if (A < cr + eps && B < cr + eps) return f * 0.5; if (d > cr - eps) return cr * cr * PI * getArg(va, vb) / PI2; vecP u = getCrossCS(c, S(a, b)); assert(!u.empty()); u.insert(u.begin(), a), u.push_back(b); for (int i = 0; i + 1 < (int)u.size(); i++) res += getArea(c, u[i], u[i + 1]); return res; } double getCrossArea(vecP t, C c) { int n = t.size(); if (n < 3) return 0; double res = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1) % n]; res += getArea(c, a, b); } return res; } double calcArea(const vecP &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) { res += cross(t[(i + 1) % n], t[i]); } return abs(res) * 0.5; } P input() { double x, y; cin >> x >> y; return P(x, y); } void pr(P p, string str) { printf("%.10f %.10f", p.real(), p.imag()); cout << str; } int main() { C a, b; a.first = input(); cin >> a.second; int n; cin >> n; while (n--) { P s = input(); P t = input(); vecP ans = getCrossCL(a, L(s, t)); if (ans.size() == 1) ans.push_back(ans[0]); sort(ans.begin(), ans.end()); pr(ans[0], " "); pr(ans[1], "\n"); } return 0; }
replace
430
431
430
431
0
p02312
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8, PI = acos(-1); inline bool eq(double a, double b) { return fabs(b - a) < EPS; } using Point = complex<double>; Point operator*(const Point &p, const double &d) { return Point(real(p) * d, imag(p) * d); } istream &operator>>(istream &is, Point &p) { double a, b; is >> a >> b; p = Point(a, b); return is; } ostream &operator<<(ostream &os, Point &p) { os << fixed << setprecision(10) << p.real() << " " << p.imag(); } Point rotate(double theta, const Point &p) { return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag()); } double radian_to_degree(double r) { return (r * 180.0 / PI); } double degree_to_radian(double d) { return (d * PI / 180.0); } double get_angle(const Point &a, const Point &b, const Point &c) { const Point v(b - a), w(c - b); double alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real()); if (alpha > beta) swap(alpha, beta); double theta = (beta - alpha); return min(theta, 2 * acos(-1) - theta); } namespace std { bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } // namespace std struct Line { Point a, b; Line() {} Line(Point a, Point b) : a(a), b(b) {} Line(double A, double B, double C) // Ax + By = C { if (eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B); else if (eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1); else a = Point(0, C / B), b = Point(C / A, 0); } friend ostream &operator<<(ostream &os, Line &p) { return os << p.a << " to " << p.b; } friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; } }; struct Segment : Line { Segment() {} Segment(Point a, Point b) : Line(a, b) {} }; struct Circle { Point p; double r; Circle() {} Circle(Point p, double r) : p(p), r(r) {} }; using Points = vector<Point>; using Polygon = vector<Point>; using Segments = vector<Segment>; using Lines = vector<Line>; using Circles = vector<Circle>; double cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); } double dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C int ccw(const Point &a, Point b, Point c) { b = b - a, c = c - a; if (cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE" if (cross(b, c) < -EPS) return -1; // "CLOCKWISE" if (dot(b, c) < 0) return +2; // "ONLINE_BACK" if (norm(b) < norm(c)) return -2; // "ONLINE_FRONT" return 0; // "ON_SEGMENT" } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool parallel(const Line &a, const Line &b) { return abs(cross(a.b - a.a, b.b - b.a)) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool orthogonal(const Line &a, const Line &b) { return abs(dot(a.a - a.b, b.a - b.b)) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } Point projection(const Segment &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B Point reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; } bool Intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; } bool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; } bool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; } bool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; } double distance(const Line &l, const Point &p); bool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; } bool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B bool intersect(const Segment &s, const Segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } int intersect(const Circle &c, const Segment &l) { if (norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0; const double d1 = abs(c.p - l.a), d2 = abs(c.p - l.b); if (d1 < c.r + EPS && d2 < c.r + EPS) return 0; if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1; const Point h = projection(l, c.p); if (dot(l.a - h, l.b - h) < 0) return 2; return 0; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp int intersect(Circle c1, Circle c2) { if (c1.r < c2.r) swap(c1, c2); double d = abs(c1.p - c2.p); if (c1.r + c2.r < d) return 4; if (eq(c1.r + c2.r, d)) return 3; if (c1.r - c2.r < d) return 2; if (eq(c1.r - c2.r, d)) return 1; return 0; } double distance(const Point &a, const Point &b) { return abs(a - b); } double distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); } double distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); } double distance(const Segment &s, const Point &p) { Point r = projection(s, p); if (intersect(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D double distance(const Segment &a, const Segment &b) { if (intersect(a, b)) return 0; return min( {distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)}); } double distance(const Line &l, const Segment &s) { if (intersect(l, s)) return 0; return min(distance(l, s.a), distance(l, s.b)); } Point crosspoint(const Line &l, const Line &m) { double A = cross(l.b - l.a, m.b - m.a); double B = cross(l.b - l.a, l.b - m.a); if (abs(A) < EPS && abs(B) < EPS) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C Point crosspoint(const Segment &l, const Segment &m) { double A = cross(l.b - l.a, m.b - m.a); double B = cross(l.b - l.a, l.b - m.a); if (abs(A) < EPS && abs(B) < EPS) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D pair<Point, Point> crosspoint(const Circle &c, const Line l) { Point pr = projection(l, c.p); Point e = (l.b - l.a) / abs(l.b - l.a); if (eq(distance(l, c.p), c.r)) return {pr, pr}; double base = sqrt(c.r * c.r - norm(pr - c.p)); return {pr + e * base, pr - e * base}; } pair<Point, Point> crosspoint(const Circle &c, const Segment &l) { Line aa = Line(l.a, l.b); if (intersect(c, l) == 2) return crosspoint(c, aa); auto ret = crosspoint(c, aa); if (dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first; else ret.first = ret.second; return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) { double d = abs(c1.p - c2.p); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real()); Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r); Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r); return {p1, p2}; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F pair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G Lines tangent(Circle c1, Circle c2) { Lines ret; if (c1.r < c2.r) swap(c1, c2); double g = norm(c1.p - c2.p); if (eq(g, 0)) return ret; Point u = (c2.p - c1.p) / sqrt(g); Point v = rotate(PI * 0.5, u); for (int s : {-1, 1}) { double h = (c1.r + s * c2.r) / sqrt(g); if (eq(1 - h * h, 0)) { ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r); } else if (1 - h * h > 0) { Point uu = u * h, vv = v * sqrt(1 - h * h); ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s); ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s); } } return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B bool is_convex(const Polygon &p) { int n = (int)p.size(); for (int i = 0; i < n; i++) { if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false; } return true; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A Polygon convex_hull(Polygon &p) { int n = (int)p.size(), k = 0; if (n <= 2) return p; sort(p.begin(), p.end()); vector<Point> ch(2 * n); for (int i = 0; i < n; ch[k++] = p[i++]) { while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } ch.resize(k - 1); return ch; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C enum { OUT, ON, IN }; int contains(const Polygon &Q, const Point &p) { bool in = false; for (int i = 0; i < Q.size(); i++) { Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p; if (a.imag() > b.imag()) swap(a, b); if (a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in; if (cross(a, b) == 0 && dot(a, b) <= 0) return ON; } return in ? IN : OUT; } bool merge_if_able(Segment &s1, Segment s2) { if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false; if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false; if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false; s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b)); return true; } void merge_segments(vector<Segment> &segs) { for (int i = 0; i < segs.size(); i++) { if (segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b); } for (int i = 0; i < segs.size(); i++) { for (int j = i + 1; j < segs.size(); j++) { if (merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(), segs.pop_back(); } } } } vector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) { vector<vector<int>> g; int N = (int)segs.size(); for (int i = 0; i < N; i++) { ps.emplace_back(segs[i].a); ps.emplace_back(segs[i].b); for (int j = i + 1; j < N; j++) { const Point p1 = segs[i].b - segs[i].a; const Point p2 = segs[j].b - segs[j].a; if (cross(p1, p2) == 0) continue; if (intersect(segs[i], segs[j])) { ps.emplace_back(crosspoint(segs[i], segs[j])); } } } sort(begin(ps), end(ps)); ps.erase(unique(begin(ps), end(ps)), end(ps)); int M = (int)ps.size(); g.resize(M); for (int i = 0; i < N; i++) { vector<int> vec; for (int j = 0; j < M; j++) { if (intersect(segs[i], ps[j])) { vec.emplace_back(j); } } for (int j = 1; j < vec.size(); j++) { g[vec[j - 1]].push_back(vec[j]); g[vec[j]].push_back(vec[j - 1]); } } return (g); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C Polygon convex_cut(const Polygon &U, Line l) { Polygon ret; for (int i = 0; i < U.size(); i++) { Point now = U[i], nxt = U[(i + 1) % U.size()]; if (ccw(l.a, l.b, now) != -1) ret.push_back(now); if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) { ret.push_back(crosspoint(Line(now, nxt), l)); } } return (ret); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A double area2(const Polygon &p) { double A = 0; for (int i = 0; i < p.size(); ++i) { A += cross(p[i], p[(i + 1) % p.size()]); } return A; } double area2(const Polygon &p, const Circle &c) { if (p.size() < 3) return 0.0; function<double(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) { Point va = c.p - a, vb = c.p - b; auto f = cross(va, vb), ret = 0.0; if (eq(f, 0.0)) return ret; if (max(abs(va), abs(vb)) < c.r + EPS) return f; if (distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va)); auto u = crosspoint(c, Segment(a, b)); vector<Point> tot{a, u.first, u.second, b}; for (int i = 0; i + 1 < tot.size(); i++) { ret += cross_area(c, tot[i], tot[i + 1]); } return ret; }; double A = 0; for (int i = 0; i < p.size(); i++) { A += cross_area(c, p[i], p[(i + 1) % p.size()]); } return A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B double convex_diameter(const Polygon &p) { int N = (int)p.size(); int is = 0, js = 0; for (int i = 1; i < N; i++) { if (p[i].imag() > p[is].imag()) is = i; if (p[i].imag() < p[js].imag()) js = i; } double maxdis = norm(p[is] - p[js]); int maxi, maxj, i, j; i = maxi = is; j = maxj = js; do { if (cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) { j = (j + 1) % N; } else { i = (i + 1) % N; } if (norm(p[i] - p[j]) > maxdis) { maxdis = norm(p[i] - p[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxdis); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A double closest_pair(Points ps) { if (ps.size() <= 1) throw(0); auto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); }; vector<Point> beet(ps.size()); function<double(int, int)> rec = [&](int left, int right) { if (right - left <= 1) return 1e18; int mid = (left + right) >> 1; auto x = real(ps[mid]); auto ret = min(rec(left, mid), rec(mid, right)); inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y); int ptr = 0; for (int i = left; i < right; i++) { if (abs(real(ps[i]) - x) >= ret) continue; for (int j = 0; j < ptr; j++) { auto luz = ps[i] - beet[ptr - j - 1]; if (imag(luz) >= ret) break; ret = min(ret, abs(luz)); } beet[ptr++] = ps[i]; } return ret; }; return rec(0, (int)ps.size()); } int main() { int N; double R; cin >> N >> R; Circle c(Point(0, 0), R); Polygon p(N); for (auto &s : p) cin >> s; cout << fixed << setprecision(10) << area2(p, c) * 0.5 << endl; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8, PI = acos(-1); inline bool eq(double a, double b) { return fabs(b - a) < EPS; } using Point = complex<double>; Point operator*(const Point &p, const double &d) { return Point(real(p) * d, imag(p) * d); } istream &operator>>(istream &is, Point &p) { double a, b; is >> a >> b; p = Point(a, b); return is; } ostream &operator<<(ostream &os, Point &p) { os << fixed << setprecision(10) << p.real() << " " << p.imag(); } Point rotate(double theta, const Point &p) { return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag()); } double radian_to_degree(double r) { return (r * 180.0 / PI); } double degree_to_radian(double d) { return (d * PI / 180.0); } double get_angle(const Point &a, const Point &b, const Point &c) { const Point v(b - a), w(c - b); double alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real()); if (alpha > beta) swap(alpha, beta); double theta = (beta - alpha); return min(theta, 2 * acos(-1) - theta); } namespace std { bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } // namespace std struct Line { Point a, b; Line() {} Line(Point a, Point b) : a(a), b(b) {} Line(double A, double B, double C) // Ax + By = C { if (eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B); else if (eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1); else a = Point(0, C / B), b = Point(C / A, 0); } friend ostream &operator<<(ostream &os, Line &p) { return os << p.a << " to " << p.b; } friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; } }; struct Segment : Line { Segment() {} Segment(Point a, Point b) : Line(a, b) {} }; struct Circle { Point p; double r; Circle() {} Circle(Point p, double r) : p(p), r(r) {} }; using Points = vector<Point>; using Polygon = vector<Point>; using Segments = vector<Segment>; using Lines = vector<Line>; using Circles = vector<Circle>; double cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); } double dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C int ccw(const Point &a, Point b, Point c) { b = b - a, c = c - a; if (cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE" if (cross(b, c) < -EPS) return -1; // "CLOCKWISE" if (dot(b, c) < 0) return +2; // "ONLINE_BACK" if (norm(b) < norm(c)) return -2; // "ONLINE_FRONT" return 0; // "ON_SEGMENT" } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool parallel(const Line &a, const Line &b) { return abs(cross(a.b - a.a, b.b - b.a)) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool orthogonal(const Line &a, const Line &b) { return abs(dot(a.a - a.b, b.a - b.b)) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } Point projection(const Segment &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B Point reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; } bool Intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; } bool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; } bool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; } bool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; } double distance(const Line &l, const Point &p); bool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; } bool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B bool intersect(const Segment &s, const Segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } int intersect(const Circle &c, const Segment &l) { if (norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0; const double d1 = abs(c.p - l.a), d2 = abs(c.p - l.b); if (d1 < c.r + EPS && d2 < c.r + EPS) return 0; if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1; const Point h = projection(l, c.p); if (dot(l.a - h, l.b - h) < 0) return 2; return 0; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp int intersect(Circle c1, Circle c2) { if (c1.r < c2.r) swap(c1, c2); double d = abs(c1.p - c2.p); if (c1.r + c2.r < d) return 4; if (eq(c1.r + c2.r, d)) return 3; if (c1.r - c2.r < d) return 2; if (eq(c1.r - c2.r, d)) return 1; return 0; } double distance(const Point &a, const Point &b) { return abs(a - b); } double distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); } double distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); } double distance(const Segment &s, const Point &p) { Point r = projection(s, p); if (intersect(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D double distance(const Segment &a, const Segment &b) { if (intersect(a, b)) return 0; return min( {distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)}); } double distance(const Line &l, const Segment &s) { if (intersect(l, s)) return 0; return min(distance(l, s.a), distance(l, s.b)); } Point crosspoint(const Line &l, const Line &m) { double A = cross(l.b - l.a, m.b - m.a); double B = cross(l.b - l.a, l.b - m.a); if (abs(A) < EPS && abs(B) < EPS) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C Point crosspoint(const Segment &l, const Segment &m) { double A = cross(l.b - l.a, m.b - m.a); double B = cross(l.b - l.a, l.b - m.a); if (abs(A) < EPS && abs(B) < EPS) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D pair<Point, Point> crosspoint(const Circle &c, const Line l) { Point pr = projection(l, c.p); Point e = (l.b - l.a) / abs(l.b - l.a); if (eq(distance(l, c.p), c.r)) return {pr, pr}; double base = sqrt(c.r * c.r - norm(pr - c.p)); return {pr - e * base, pr + e * base}; } pair<Point, Point> crosspoint(const Circle &c, const Segment &l) { Line aa = Line(l.a, l.b); if (intersect(c, l) == 2) return crosspoint(c, aa); auto ret = crosspoint(c, aa); if (dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first; else ret.first = ret.second; return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) { double d = abs(c1.p - c2.p); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real()); Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r); Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r); return {p1, p2}; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F pair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G Lines tangent(Circle c1, Circle c2) { Lines ret; if (c1.r < c2.r) swap(c1, c2); double g = norm(c1.p - c2.p); if (eq(g, 0)) return ret; Point u = (c2.p - c1.p) / sqrt(g); Point v = rotate(PI * 0.5, u); for (int s : {-1, 1}) { double h = (c1.r + s * c2.r) / sqrt(g); if (eq(1 - h * h, 0)) { ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r); } else if (1 - h * h > 0) { Point uu = u * h, vv = v * sqrt(1 - h * h); ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s); ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s); } } return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B bool is_convex(const Polygon &p) { int n = (int)p.size(); for (int i = 0; i < n; i++) { if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false; } return true; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A Polygon convex_hull(Polygon &p) { int n = (int)p.size(), k = 0; if (n <= 2) return p; sort(p.begin(), p.end()); vector<Point> ch(2 * n); for (int i = 0; i < n; ch[k++] = p[i++]) { while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } ch.resize(k - 1); return ch; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C enum { OUT, ON, IN }; int contains(const Polygon &Q, const Point &p) { bool in = false; for (int i = 0; i < Q.size(); i++) { Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p; if (a.imag() > b.imag()) swap(a, b); if (a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in; if (cross(a, b) == 0 && dot(a, b) <= 0) return ON; } return in ? IN : OUT; } bool merge_if_able(Segment &s1, Segment s2) { if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false; if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false; if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false; s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b)); return true; } void merge_segments(vector<Segment> &segs) { for (int i = 0; i < segs.size(); i++) { if (segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b); } for (int i = 0; i < segs.size(); i++) { for (int j = i + 1; j < segs.size(); j++) { if (merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(), segs.pop_back(); } } } } vector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) { vector<vector<int>> g; int N = (int)segs.size(); for (int i = 0; i < N; i++) { ps.emplace_back(segs[i].a); ps.emplace_back(segs[i].b); for (int j = i + 1; j < N; j++) { const Point p1 = segs[i].b - segs[i].a; const Point p2 = segs[j].b - segs[j].a; if (cross(p1, p2) == 0) continue; if (intersect(segs[i], segs[j])) { ps.emplace_back(crosspoint(segs[i], segs[j])); } } } sort(begin(ps), end(ps)); ps.erase(unique(begin(ps), end(ps)), end(ps)); int M = (int)ps.size(); g.resize(M); for (int i = 0; i < N; i++) { vector<int> vec; for (int j = 0; j < M; j++) { if (intersect(segs[i], ps[j])) { vec.emplace_back(j); } } for (int j = 1; j < vec.size(); j++) { g[vec[j - 1]].push_back(vec[j]); g[vec[j]].push_back(vec[j - 1]); } } return (g); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C Polygon convex_cut(const Polygon &U, Line l) { Polygon ret; for (int i = 0; i < U.size(); i++) { Point now = U[i], nxt = U[(i + 1) % U.size()]; if (ccw(l.a, l.b, now) != -1) ret.push_back(now); if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) { ret.push_back(crosspoint(Line(now, nxt), l)); } } return (ret); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A double area2(const Polygon &p) { double A = 0; for (int i = 0; i < p.size(); ++i) { A += cross(p[i], p[(i + 1) % p.size()]); } return A; } double area2(const Polygon &p, const Circle &c) { if (p.size() < 3) return 0.0; function<double(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) { Point va = c.p - a, vb = c.p - b; auto f = cross(va, vb), ret = 0.0; if (eq(f, 0.0)) return ret; if (max(abs(va), abs(vb)) < c.r + EPS) return f; if (distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va)); auto u = crosspoint(c, Segment(a, b)); vector<Point> tot{a, u.first, u.second, b}; for (int i = 0; i + 1 < tot.size(); i++) { ret += cross_area(c, tot[i], tot[i + 1]); } return ret; }; double A = 0; for (int i = 0; i < p.size(); i++) { A += cross_area(c, p[i], p[(i + 1) % p.size()]); } return A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B double convex_diameter(const Polygon &p) { int N = (int)p.size(); int is = 0, js = 0; for (int i = 1; i < N; i++) { if (p[i].imag() > p[is].imag()) is = i; if (p[i].imag() < p[js].imag()) js = i; } double maxdis = norm(p[is] - p[js]); int maxi, maxj, i, j; i = maxi = is; j = maxj = js; do { if (cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) { j = (j + 1) % N; } else { i = (i + 1) % N; } if (norm(p[i] - p[j]) > maxdis) { maxdis = norm(p[i] - p[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxdis); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A double closest_pair(Points ps) { if (ps.size() <= 1) throw(0); auto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); }; vector<Point> beet(ps.size()); function<double(int, int)> rec = [&](int left, int right) { if (right - left <= 1) return 1e18; int mid = (left + right) >> 1; auto x = real(ps[mid]); auto ret = min(rec(left, mid), rec(mid, right)); inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y); int ptr = 0; for (int i = left; i < right; i++) { if (abs(real(ps[i]) - x) >= ret) continue; for (int j = 0; j < ptr; j++) { auto luz = ps[i] - beet[ptr - j - 1]; if (imag(luz) >= ret) break; ret = min(ret, abs(luz)); } beet[ptr++] = ps[i]; } return ret; }; return rec(0, (int)ps.size()); } int main() { int N; double R; cin >> N >> R; Circle c(Point(0, 0), R); Polygon p(N); for (auto &s : p) cin >> s; cout << fixed << setprecision(10) << area2(p, c) * 0.5 << endl; }
replace
260
261
260
261
0
p02312
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef complex<double> V; typedef vector<P> vecP; typedef pair<P, P> L; typedef pair<P, P> S; typedef pair<P, double> C; const double eps = 1e-8; const double PI = acos(-1); const double PI2 = PI * 2.0; namespace std { bool operator<(const P &a, const P &b) { return (a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std V normal(V a) { assert(abs(a) > 0); return a / abs(a); } double Sqrt(double x) { if (x < 0) return 0; else return sqrt(x); } P Vector(L a) { return a.second - a.first; } bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (eq(a.real(), b.real()) && eq(a.imag(), b.imag())); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } double getArg(P a, P b) { return arg(b * conj(a)); } double getTime(V a, V b) { assert(eq(cross(a, b), 0)); return (dot(a, b) < 0 ? -1.0 : 1.0) * abs(b) / abs(a); } P project(P a, P b, P c) { b -= a, c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a, c -= a; return a + b * conj(c / b); } int ccw(P a, P b, P c) { P ab = b - a, ac = c - a; P k = ac * conj(ab); if (k.imag() > 0) return 1; if (k.imag() < 0) return -1; if (k.real() < 0) return 2; if (abs(ab) < abs(ac)) return -2; return 0; } bool isParallel(P a, P b) { return eq(0, cross(a, b)); } bool isParallel(S a, S b) { return eq(0, cross(Vector(a), Vector(b))); } bool onLP(L l, P p) { P a = l.first, b = l.second; return eq(0, cross(b - a, p - a)); } bool onSP(S s, P p) { P a = s.first, b = s.second; return eq(abs(b - a), abs(a - p) + abs(b - p)); } bool isCrossSS(S s0, S s1) { P a = s0.first, b = s0.second; P c = s1.first, d = s1.second; int f0 = ccw(a, b, c) * ccw(a, b, d); int f1 = ccw(c, d, a) * ccw(c, d, b); return (f0 <= 0 && f1 <= 0); } bool isCrossLS(L l, S s) { P a = l.first, b = l.second; P c = s.first, d = s.second; return (ccw(a, b, c) * ccw(a, b, d) <= 0); } double distLP(L l, P p) { P a = l.first, b = l.second; double res = cross(b - a, p - a) / abs(b - a); return abs(res); } double distSP(S s, P p) { P a = s.first, b = s.second; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return distLP(s, p); } double distSS(S s0, S s1) { if (isCrossSS(s0, s1)) return 0; double res0 = min(distSP(s0, s1.first), distSP(s0, s1.second)); double res1 = min(distSP(s1, s0.first), distSP(s1, s0.second)); return min(res0, res1); } P getCrossLL(L l0, L l1) { P a = l0.first, b = l0.second; P c = l1.first, d = l1.second; a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } int inPolygon(vecP &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; sum += getArg(a - p, b - p); } if (abs(sum) < eps) return 0; else return 2; } vecP andrewScan(vecP &t) { int N = t.size(), C = 0; vecP R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vecP res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vecP convexHull(vecP &t) { sort(t.begin(), t.end()); vecP u = andrewScan(t); reverse(t.begin(), t.end()); vecP l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vecP cutConvex(vecP &t, L l) { P a = l.first, b = l.second; int N = t.size(); vecP res; for (int i = 0; i < N; i++) { P c = t[i], d = t[(i + 1) % N]; int C = ccw(a, b, c), D = ccw(a, b, d); if (C != -1) res.push_back(c); if (C == -D && abs(C) == 1) res.push_back(getCrossLL(l, L(c, d))); } return res; } P getVector(const vecP &t, int id) { int n = t.size(); return t[(id + 1) % n] - t[id % n]; } double convex_diameter(vecP &t) { int n = t.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(t[i]) > imag(t[is])) is = i; if (imag(t[i]) < imag(t[js])) js = i; } double maxd = norm(t[is] - t[js]); int i, maxi, j, maxj; i = maxi = is; j = maxj = js; do { if (cross(getVector(t, i), getVector(t, j)) >= 0) j = (j + 1) % n; else i = (i + 1) % n; if (norm(t[i] - t[j]) > maxd) { maxd = norm(t[i] - t[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxd); /* farthest pair is (maxi, maxj). */ } bool compare_y(const P &a, const P &b) { return a.imag() < b.imag(); } double closest_pair(P *a, int n) { if (n <= 1) return 1e30; int m = n / 2; double x = a[m].real(); double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (abs(a[i].real() - x) >= d) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = real(a[i] - b[b.size() - j - 1]); double dy = imag(a[i] - b[b.size() - j - 1]); if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } P _pool[200005]; double minDist(vecP &t) { int n = t.size(); for (int i = 0; i < n; i++) _pool[i] = t[i]; sort(_pool, _pool + n); return closest_pair(_pool, n); } int getStateCC(C a, C b) { double ar = a.second, br = b.second; double dist = abs(a.first - b.first); if (dist > ar + br + eps) return 4; if (dist > ar + br - eps) return 3; if (dist > abs(ar - br) + eps) return 2; if (dist > abs(ar - br) - eps) return 1; return 0; } P getCrossCC(C a, C b) { P p1 = a.first, p2 = b.first; double r1 = a.second, r2 = b.second; double cA = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * P(cA, Sqrt(1.0 - cA * cA)); } P getTangentCP_(C a, P p, int flg) { P base = a.first - p; double ar = a.second; double w = Sqrt(norm(base) - ar * ar); P s = p + base * P(w, ar * flg) / norm(base) * w; return s; } vector<S> getTangentCP(C a, P p) { vector<S> res; P s = getTangentCP_(a, p, 1); P t = getTangentCP_(a, p, -1); if (eq(s, t)) { res.push_back(S(s, s + (a.first - p) * P(0, 1))); } else { res.push_back(S(p, s)); res.push_back(S(p, t)); } return res; } S getInTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double w = ar + br; double h = Sqrt(norm(base) - w * w); P k = base * P(w, h * flg) / norm(base); return S(ap + k * ar, bp - k * br); } S getOutTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double h = br - ar; double w = Sqrt(norm(base) - h * h); P k = base * P(w, h * flg) / norm(base) * P(0, flg); return S(ap + k * ar, bp + k * br); } vector<S> getTangentCC(C a, C b) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; vector<S> res; double dist = abs(ap - bp); if (dist > ar + br + eps) res.push_back(getInTangent(a, b, 1)); if (dist > ar + br - eps) res.push_back(getInTangent(a, b, -1)); if (dist > abs(ar - br) + eps) res.push_back(getOutTangent(a, b, 1)); if (dist > abs(ar - br) - eps) res.push_back(getOutTangent(a, b, -1)); return res; } vecP getCrossCL(C cir, L l) { P a = l.first, b = l.second; double cr = cir.second; P cp = cir.first; vecP res; P base = b - a, target = project(a, b, cp); double length = abs(base), h = abs(cp - target); base /= length; if (cr + eps < h) return res; double w = Sqrt(cr * cr - h * h); double L = getTime(normal(b - a), target - a) - w, R = L + w * 2.0; res.push_back(a + base * L); if (eq(L, R)) return res; res.push_back(a + base * R); return res; } vecP getCrossCS(C cir, S s) { vecP tmp = getCrossCL(cir, s); vecP res; for (int i = 0; i < (int)tmp.size(); i++) if (ccw(s.first, s.second, tmp[i]) == 0) res.push_back(tmp[i]); return res; } double getArea(C c, P a, P b) { P cp = c.first; double cr = c.second; P va = cp - a, vb = cp - b; double A = abs(va), B = abs(vb); double f = cross(va, vb), d = distSP(S(a, b), cp), res = 0; if (eq(0, f)) return 0; if (A < cr + eps && B < cr + eps) return f * 0.5; if (d > cr - eps) return cr * cr * PI * getArg(va, vb) / PI2; vecP u = getCrossCS(c, S(a, b)); assert(!u.empty()); u.insert(u.begin(), a), u.push_back(b); for (int i = 0; i + 1 < (int)u.size(); i++) res += getArea(c, u[i], u[i + 1]); return res; } double getCrossArea(vecP t, C c) { int n = t.size(); if (n < 3) return 0; double res = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1) % n]; res += getArea(c, a, b); } return res; } double calcPolygonArea(const vecP &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) { res += cross(t[(i + 1) % n], t[i]); } return abs(res) * 0.5; } P input() { double x, y; cin >> x >> y; return P(x, y); } void pr(P p, string str = "\n") { printf("%.10f %.10f", p.real(), p.imag()); cout << str; } int main() { int n; double r; cin >> n >> r; C a = C(P(0, 0), r); vecP t(n); for (int i = 0; i < n; i++) t[i] = input(); printf("%.10f\n", getCrossArea(t, a)); return 0; }
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; typedef complex<double> V; typedef vector<P> vecP; typedef pair<P, P> L; typedef pair<P, P> S; typedef pair<P, double> C; const double eps = 1e-8; const double PI = acos(-1); const double PI2 = PI * 2.0; namespace std { bool operator<(const P &a, const P &b) { return (a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag()); } }; // namespace std V normal(V a) { assert(abs(a) > 0); return a / abs(a); } double Sqrt(double x) { if (x < 0) return 0; else return sqrt(x); } P Vector(L a) { return a.second - a.first; } bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (eq(a.real(), b.real()) && eq(a.imag(), b.imag())); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } double getArg(P a, P b) { return arg(b * conj(a)); } double getTime(V a, V b) { assert(eq(cross(a, b), 0)); return (dot(a, b) < 0 ? -1.0 : 1.0) * abs(b) / abs(a); } P project(P a, P b, P c) { b -= a, c -= a; return a + b * real(c / b); } P reflect(P a, P b, P c) { b -= a, c -= a; return a + b * conj(c / b); } int ccw(P a, P b, P c) { P ab = b - a, ac = c - a; P k = ac * conj(ab); if (k.imag() > 0) return 1; if (k.imag() < 0) return -1; if (k.real() < 0) return 2; if (abs(ab) < abs(ac)) return -2; return 0; } bool isParallel(P a, P b) { return eq(0, cross(a, b)); } bool isParallel(S a, S b) { return eq(0, cross(Vector(a), Vector(b))); } bool onLP(L l, P p) { P a = l.first, b = l.second; return eq(0, cross(b - a, p - a)); } bool onSP(S s, P p) { P a = s.first, b = s.second; return eq(abs(b - a), abs(a - p) + abs(b - p)); } bool isCrossSS(S s0, S s1) { P a = s0.first, b = s0.second; P c = s1.first, d = s1.second; int f0 = ccw(a, b, c) * ccw(a, b, d); int f1 = ccw(c, d, a) * ccw(c, d, b); return (f0 <= 0 && f1 <= 0); } bool isCrossLS(L l, S s) { P a = l.first, b = l.second; P c = s.first, d = s.second; return (ccw(a, b, c) * ccw(a, b, d) <= 0); } double distLP(L l, P p) { P a = l.first, b = l.second; double res = cross(b - a, p - a) / abs(b - a); return abs(res); } double distSP(S s, P p) { P a = s.first, b = s.second; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return distLP(s, p); } double distSS(S s0, S s1) { if (isCrossSS(s0, s1)) return 0; double res0 = min(distSP(s0, s1.first), distSP(s0, s1.second)); double res1 = min(distSP(s1, s0.first), distSP(s1, s0.second)); return min(res0, res1); } P getCrossLL(L l0, L l1) { P a = l0.first, b = l0.second; P c = l1.first, d = l1.second; a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } int inPolygon(vecP &t, P p) { int n = t.size(); double sum = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1 == n ? 0 : i + 1)]; if (ccw(a, b, p) == 0) return 1; sum += getArg(a - p, b - p); } if (abs(sum) < eps) return 0; else return 2; } vecP andrewScan(vecP &t) { int N = t.size(), C = 0; vecP R(N); for (int i = 0; i < N; i++) { while (2 <= C && ccw(R[C - 2], R[C - 1], t[i]) == -1) C--; R[C++] = t[i]; } vecP res(C); for (int i = 0; i < C; i++) res[i] = R[i]; return res; } vecP convexHull(vecP &t) { sort(t.begin(), t.end()); vecP u = andrewScan(t); reverse(t.begin(), t.end()); vecP l = andrewScan(t); for (int i = 1; i + 1 < (int)l.size(); i++) u.push_back(l[i]); return u; } vecP cutConvex(vecP &t, L l) { P a = l.first, b = l.second; int N = t.size(); vecP res; for (int i = 0; i < N; i++) { P c = t[i], d = t[(i + 1) % N]; int C = ccw(a, b, c), D = ccw(a, b, d); if (C != -1) res.push_back(c); if (C == -D && abs(C) == 1) res.push_back(getCrossLL(l, L(c, d))); } return res; } P getVector(const vecP &t, int id) { int n = t.size(); return t[(id + 1) % n] - t[id % n]; } double convex_diameter(vecP &t) { int n = t.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(t[i]) > imag(t[is])) is = i; if (imag(t[i]) < imag(t[js])) js = i; } double maxd = norm(t[is] - t[js]); int i, maxi, j, maxj; i = maxi = is; j = maxj = js; do { if (cross(getVector(t, i), getVector(t, j)) >= 0) j = (j + 1) % n; else i = (i + 1) % n; if (norm(t[i] - t[j]) > maxd) { maxd = norm(t[i] - t[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxd); /* farthest pair is (maxi, maxj). */ } bool compare_y(const P &a, const P &b) { return a.imag() < b.imag(); } double closest_pair(P *a, int n) { if (n <= 1) return 1e30; int m = n / 2; double x = a[m].real(); double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (abs(a[i].real() - x) >= d) continue; for (int j = 0; j < (int)b.size(); j++) { double dx = real(a[i] - b[b.size() - j - 1]); double dy = imag(a[i] - b[b.size() - j - 1]); if (dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } P _pool[200005]; double minDist(vecP &t) { int n = t.size(); for (int i = 0; i < n; i++) _pool[i] = t[i]; sort(_pool, _pool + n); return closest_pair(_pool, n); } int getStateCC(C a, C b) { double ar = a.second, br = b.second; double dist = abs(a.first - b.first); if (dist > ar + br + eps) return 4; if (dist > ar + br - eps) return 3; if (dist > abs(ar - br) + eps) return 2; if (dist > abs(ar - br) - eps) return 1; return 0; } P getCrossCC(C a, C b) { P p1 = a.first, p2 = b.first; double r1 = a.second, r2 = b.second; double cA = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * P(cA, Sqrt(1.0 - cA * cA)); } P getTangentCP_(C a, P p, int flg) { P base = a.first - p; double ar = a.second; double w = Sqrt(norm(base) - ar * ar); P s = p + base * P(w, ar * flg) / norm(base) * w; return s; } vector<S> getTangentCP(C a, P p) { vector<S> res; P s = getTangentCP_(a, p, 1); P t = getTangentCP_(a, p, -1); if (eq(s, t)) { res.push_back(S(s, s + (a.first - p) * P(0, 1))); } else { res.push_back(S(p, s)); res.push_back(S(p, t)); } return res; } S getInTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double w = ar + br; double h = Sqrt(norm(base) - w * w); P k = base * P(w, h * flg) / norm(base); return S(ap + k * ar, bp - k * br); } S getOutTangent(C a, C b, double flg = 1.0) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; P base = bp - ap; double h = br - ar; double w = Sqrt(norm(base) - h * h); P k = base * P(w, h * flg) / norm(base) * P(0, flg); return S(ap + k * ar, bp + k * br); } vector<S> getTangentCC(C a, C b) { P ap = a.first, bp = b.first; double ar = a.second, br = b.second; vector<S> res; double dist = abs(ap - bp); if (dist > ar + br + eps) res.push_back(getInTangent(a, b, 1)); if (dist > ar + br - eps) res.push_back(getInTangent(a, b, -1)); if (dist > abs(ar - br) + eps) res.push_back(getOutTangent(a, b, 1)); if (dist > abs(ar - br) - eps) res.push_back(getOutTangent(a, b, -1)); return res; } vecP getCrossCL(C cir, L l) { P a = l.first, b = l.second; double cr = cir.second; P cp = cir.first; vecP res; P base = b - a, target = project(a, b, cp); double length = abs(base), h = abs(cp - target); base /= length; if (cr + eps < h) return res; double w = Sqrt(cr * cr - h * h); double L = getTime(normal(b - a), target - a) - w, R = L + w * 2.0; res.push_back(a + base * L); if (eq(L, R)) return res; res.push_back(a + base * R); return res; } vecP getCrossCS(C cir, S s) { vecP tmp = getCrossCL(cir, s); vecP res; for (int i = 0; i < (int)tmp.size(); i++) // if( ccw(s.first,s.second, tmp[i] ) == 0) if (onSP(s, tmp[i])) res.push_back(tmp[i]); return res; } double getArea(C c, P a, P b) { P cp = c.first; double cr = c.second; P va = cp - a, vb = cp - b; double A = abs(va), B = abs(vb); double f = cross(va, vb), d = distSP(S(a, b), cp), res = 0; if (eq(0, f)) return 0; if (A < cr + eps && B < cr + eps) return f * 0.5; if (d > cr - eps) return cr * cr * PI * getArg(va, vb) / PI2; vecP u = getCrossCS(c, S(a, b)); assert(!u.empty()); u.insert(u.begin(), a), u.push_back(b); for (int i = 0; i + 1 < (int)u.size(); i++) res += getArea(c, u[i], u[i + 1]); return res; } double getCrossArea(vecP t, C c) { int n = t.size(); if (n < 3) return 0; double res = 0; for (int i = 0; i < n; i++) { P a = t[i], b = t[(i + 1) % n]; res += getArea(c, a, b); } return res; } double calcPolygonArea(const vecP &t) { double res = 0; int n = t.size(); for (int i = 0; i < n; i++) { res += cross(t[(i + 1) % n], t[i]); } return abs(res) * 0.5; } P input() { double x, y; cin >> x >> y; return P(x, y); } void pr(P p, string str = "\n") { printf("%.10f %.10f", p.real(), p.imag()); cout << str; } int main() { int n; double r; cin >> n >> r; C a = C(P(0, 0), r); vecP t(n); for (int i = 0; i < n; i++) t[i] = input(); printf("%.10f\n", getCrossArea(t, a)); return 0; }
replace
370
371
370
372
-6
9360ca0c-b548-4ded-a6b1-a6b7f2e3d558.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02312/C++/s412921055.cpp:380: double getArea(C, P, P): Assertion `!u.empty()' failed.
p02312
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; typedef ld Weight; struct Edge { int src, dest; int cap, rev; Weight weight; bool operator<(const Edge &rhs) const { return weight > rhs.weight; } }; /* ??????????????¬ */ #include <complex> typedef complex<ld> Point; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std // ????????\??? Point input_Point() { ld x, y; cin >> x >> y; return Point(x, y); } // ???????????????????????? bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } // ?????? ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } // ?????? ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } // ??´???????????? class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; // ???????????? class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // ccw // 1: a,b,c??????????¨???¨?????????????????¶ //-1: a,b,c???????¨???¨?????????????????¶ // 2: c,a,b???????????´???????????¶ //-2: a,b,c???????????´???????????¶ // 0: a,c,b???????????´???????????¶ int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶ if (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶ if (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶ if (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶ return 0; // a,c,b???????????´???????????¶ } /* ???????????? */ // ??´?????¨??´?????????????????? bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } // ??´?????¨????????????????????? bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } // ????????¨????????????????????? bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } // ????????´???????????? bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } // ????????????????????? bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // ??????????¶? Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b); return l.a + t * (l.b - l.a); } //???????±?????????????????????? Point reflect(const Line &l, const Point &p) { Point pr = proj(l, p); return pr * 2.l - p; } // ??´?????¨??´???????????? Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } // ??´?????¨??´???????????? vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } // ????????¨??????????????? //???????????£????????¨???????????¨assert(false) Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { //??????isis_ss????????? assert(false); return Point(0, 0); } } // ????????¨??????????????? vector<Point> is_ss2(const Line &s, const Line &t) { vector<Point> kouho(is_ll2(s, t)); vector<Point> ans; for (auto p : kouho) { if (isis_sp(s, p) && isis_sp(t, p)) ans.emplace_back(p); } return ans; } // ??´?????¨???????????¢ ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } //??´?????¨??´???????????¢ ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } // ??´?????¨??????????????¢ ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } // ????????¨???????????¢ ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } // ????????¨??????????????¢ ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } //??´?????¨??´????????????????????????????????? Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } // a,b:?????? // c:????????§?????? //???????????´?????????????????¢?????????????±?????????? vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } /* ??? */ // ?????¨???????????? vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } //??????????????????????????? /* 0 => out 1 => on 2 => in*/ int is_in_Circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } //???lc??????rc?????????????????? /*0 => out 1 => on 2 => in*/ int Circle_in_Circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } // ?????¨??´???????????? vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // ?????¨??????????????? vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } // ?????¨????????\??? vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } // ?????¨????????\??? vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), all(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } //??????????????????????????¢??? ld two_Circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } /* ????§???¢ */ typedef vector<Point> Polygon; // ??¢??? ld get_area(const Polygon &p) { ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } //????§???¢????????¢?????? bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); rep(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } // ?????????????????? /*0 => out 1 => on 2 => in*/ int is_in_Polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); rep(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } //??????????????????2????????? enum { out, on, in }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } // ?????? //???????????????????????¨????????????????????§??¨??? Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } //???????????? vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon q; Polygon r; rep(i, n) { Point a = ps[i], b = ps[(i + 1) % n]; Line m = Line(a, b); if (ccw(l.a, l.b, a) != -1) q.push_back(a); if (ccw(l.a, l.b, a) != 1) r.push_back(a); if (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) { q.push_back(is_ll(l, m)); r.push_back(is_ll(l, m)); } } const vector<Polygon> polys{q, r}; return polys; } // 0~2?? //????????????a ?????????????¨?????????? ?? ????????¨???b???????????? ld gettheta(const Point &a, const Point &b) { ld adot = dot(a, b) / abs(a) / abs(b); if (adot > 1) adot -= eps; if (adot < -1) adot += eps; ld theta = acos(adot); ld dd = cross(a, b); return cross(a, b) > -eps ? theta : 2 * pi - theta; } int n, r; ld aget(const Circle &c, Polygon poly) { if (get_area(poly) < eps) return 0; if (poly.size() > 3) { ld ans = 0; int aa = 0, ab = 1, ac = poly.size() - 1; bool flag = true; while (ab != ac) { Polygon apo = Polygon{poly[aa], poly[ab], poly[ac]}; if (is_counter_clockwise(apo)) { ans += aget(c, apo); } else { swap(apo[0], apo[1]); ans -= aget(c, apo); } if (flag) { aa = ab; ab = ab + 1; } else { aa = ac; ac = ac - 1; } flag = !flag; } return ans; } else if (poly.size() == 3) { const int in_circle_num = count_if(poly.begin(), poly.end(), [=](const Point &p) { return is_in_Circle(c, p); }); if (in_circle_num == 1) { while (!is_in_Circle(c, poly[0])) { rotate(poly.begin(), poly.begin() + 1, poly.end()); } } else if (in_circle_num == 2) { while (is_in_Circle(c, poly[2])) { rotate(poly.begin(), poly.begin() + 1, poly.end()); } } vector<Point> cas = is_sc(c, Line(poly[0], poly[1])); vector<Point> cbs = is_sc(c, Line(poly[1], poly[2])); vector<Point> ccs = is_sc(c, Line(poly[2], poly[0])); switch (in_circle_num) { case 0: { ld ans; if (cas.empty() && cbs.empty() && ccs.empty()) { if (is_in_Polygon(poly, c.p)) { ans = c.r * c.r * pi; } else { ans = 0; } } else { if (!cas.empty() && cbs.empty() && ccs.empty()) { rotate(poly.begin(), poly.begin() + 2, poly.end()); vector<Point> ccc = ccs; ccs = cbs; cbs = cas; cas = ccc; } if (cas.empty() && cbs.empty() && !ccs.empty()) { rotate(poly.begin(), poly.begin() + 1, poly.end()); vector<Point> ccc = cas; cas = cbs; cbs = ccs; ccs = ccc; } if (cas.empty() && !cbs.empty() && ccs.empty()) { Point cb1 = cbs[0], cb2 = cbs[1]; { ld theta = gettheta(cb2 - c.p, cb1 - c.p); if (theta < eps && is_in_Polygon(poly, c.p)) theta += 2 * pi; ans = c.r * c.r * theta / 2; } Polygon anspoly{cb1, cb2, c.p}; ans += get_area(anspoly); } else { if (cas.empty() && !cbs.empty() && !ccs.empty()) { rotate(poly.begin(), poly.begin() + 2, poly.end()); vector<Point> ccc = ccs; ccs = cbs; cbs = cas; cas = ccc; } if (!cas.empty() && !cbs.empty() && ccs.empty()) { rotate(poly.begin(), poly.begin() + 1, poly.end()); vector<Point> ccc = cas; cas = cbs; cbs = ccs; ccs = ccc; } if (!cas.empty() && cbs.empty() && !ccs.empty()) { Point ca1 = cas[0], ca2 = cas[1]; Point cc1 = ccs[0], cc2 = ccs[1]; { ld theta1 = gettheta(ca2 - c.p, cc1 - c.p); ld theta2 = gettheta(cc2 - c.p, ca1 - c.p); assert(theta1 > eps && theta2 > eps); assert(abs(ca1 - ca2) > eps && abs(cc1 - cc2) > eps); ans = c.r * c.r * theta1 / 2; ans += c.r * c.r * theta2 / 2; } Polygon anspoly1{ca1, ca2, c.p}; Polygon anspoly2{cc1, cc2, c.p}; ans += get_area(anspoly1) + get_area(anspoly2); } else { Point ca1 = cas[0], ca2 = cas[1]; Point cb1 = cbs[0], cb2 = cbs[1]; Point cc1 = ccs[0], cc2 = ccs[1]; { ld theta1 = gettheta(ca2, cb1); ld theta2 = gettheta(cb2, cc1); ld theta3 = gettheta(cc2, ca1); ans = c.r * c.r * theta1 / 2; ans += c.r * c.r * theta2 / 2; ans += c.r * c.r * theta3 / 2; } Polygon anspoly1{ca1, ca2, c.p}; Polygon anspoly2{cb1, cb2, c.p}; Polygon anspoly3{cc1, cc2, c.p}; ans += get_area(anspoly1) + get_area(anspoly2) + get_area(anspoly3); } } } return ans; } break; case 1: { ld ans = 0; if (cbs.empty()) { Point ca = cas.back(), cc = ccs[0]; { ld theta = gettheta(ca - c.p, cc - c.p); ans += c.r * c.r * theta / 2; } Polygon anspoly{poly[0], ca, c.p, cc}; ans += get_area(anspoly); } else { Point ca = cas.back(), cb1 = cbs[0], cb2 = cbs[1], cc = ccs[0]; Polygon anspoly1{poly[0], ca, c.p, cc}; Polygon anspoly2{cb1, cb2, c.p}; ans = get_area(anspoly1) + get_area(anspoly2); { ld theta1 = gettheta(ca - c.p, cb1 - c.p); ans += c.r * c.r * theta1 / 2; ld theta2 = gettheta(cb2 - c.p, cc - c.p); ans += c.r * c.r * theta2 / 2; } } return ans; } break; case 2: { ld ans; Point cb = cbs.back(), cc = ccs[0]; Polygon anspo{poly[0], poly[1], cb, c.p, cc}; ans = get_area(anspo); { ld theta = gettheta(cb - c.p, cc - c.p); ans += c.r * c.r * theta / 2; } return ans; } break; case 3: return get_area(poly); break; } } } int main() { cin >> n >> r; vector<Point> poly(n); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; poly[i] = Point(x, y); } Circle c(Point(0, 0), r); ld ans = aget(c, poly); cout << setprecision(10) << fixed << ans << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; typedef ld Weight; struct Edge { int src, dest; int cap, rev; Weight weight; bool operator<(const Edge &rhs) const { return weight > rhs.weight; } }; /* ??????????????¬ */ #include <complex> typedef complex<ld> Point; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std // ????????\??? Point input_Point() { ld x, y; cin >> x >> y; return Point(x, y); } // ???????????????????????? bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } // ?????? ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } // ?????? ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } // ??´???????????? class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; // ???????????? class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // ccw // 1: a,b,c??????????¨???¨?????????????????¶ //-1: a,b,c???????¨???¨?????????????????¶ // 2: c,a,b???????????´???????????¶ //-2: a,b,c???????????´???????????¶ // 0: a,c,b???????????´???????????¶ int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶ if (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶ if (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶ if (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶ return 0; // a,c,b???????????´???????????¶ } /* ???????????? */ // ??´?????¨??´?????????????????? bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } // ??´?????¨????????????????????? bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } // ????????¨????????????????????? bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } // ????????´???????????? bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } // ????????????????????? bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // ??????????¶? Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b); return l.a + t * (l.b - l.a); } //???????±?????????????????????? Point reflect(const Line &l, const Point &p) { Point pr = proj(l, p); return pr * 2.l - p; } // ??´?????¨??´???????????? Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } // ??´?????¨??´???????????? vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } // ????????¨??????????????? //???????????£????????¨???????????¨assert(false) Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { //??????isis_ss????????? assert(false); return Point(0, 0); } } // ????????¨??????????????? vector<Point> is_ss2(const Line &s, const Line &t) { vector<Point> kouho(is_ll2(s, t)); vector<Point> ans; for (auto p : kouho) { if (isis_sp(s, p) && isis_sp(t, p)) ans.emplace_back(p); } return ans; } // ??´?????¨???????????¢ ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } //??´?????¨??´???????????¢ ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } // ??´?????¨??????????????¢ ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } // ????????¨???????????¢ ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } // ????????¨??????????????¢ ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } //??´?????¨??´????????????????????????????????? Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } // a,b:?????? // c:????????§?????? //???????????´?????????????????¢?????????????±?????????? vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } /* ??? */ // ?????¨???????????? vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } //??????????????????????????? /* 0 => out 1 => on 2 => in*/ int is_in_Circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } //???lc??????rc?????????????????? /*0 => out 1 => on 2 => in*/ int Circle_in_Circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } // ?????¨??´???????????? vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // ?????¨??????????????? vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } // ?????¨????????\??? vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } // ?????¨????????\??? vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), all(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } //??????????????????????????¢??? ld two_Circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } /* ????§???¢ */ typedef vector<Point> Polygon; // ??¢??? ld get_area(const Polygon &p) { ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } //????§???¢????????¢?????? bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); rep(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } // ?????????????????? /*0 => out 1 => on 2 => in*/ int is_in_Polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); rep(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } //??????????????????2????????? enum { out, on, in }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } // ?????? //???????????????????????¨????????????????????§??¨??? Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } //???????????? vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon q; Polygon r; rep(i, n) { Point a = ps[i], b = ps[(i + 1) % n]; Line m = Line(a, b); if (ccw(l.a, l.b, a) != -1) q.push_back(a); if (ccw(l.a, l.b, a) != 1) r.push_back(a); if (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) { q.push_back(is_ll(l, m)); r.push_back(is_ll(l, m)); } } const vector<Polygon> polys{q, r}; return polys; } // 0~2?? //????????????a ?????????????¨?????????? ?? ????????¨???b???????????? ld gettheta(const Point &a, const Point &b) { ld adot = dot(a, b) / abs(a) / abs(b); if (adot > 1) adot -= eps; if (adot < -1) adot += eps; ld theta = acos(adot); ld dd = cross(a, b); return cross(a, b) > -eps ? theta : 2 * pi - theta; } int n, r; ld aget(const Circle &c, Polygon poly) { if (get_area(poly) < eps) return 0; if (poly.size() > 3) { ld ans = 0; int aa = 0, ab = 1, ac = poly.size() - 1; bool flag = true; while (ab != ac) { Polygon apo = Polygon{poly[aa], poly[ab], poly[ac]}; if (is_counter_clockwise(apo)) { ans += aget(c, apo); } else { swap(apo[0], apo[1]); ans -= aget(c, apo); } if (flag) { aa = ab; ab = ab + 1; } else { aa = ac; ac = ac - 1; } flag = !flag; } return ans; } else if (poly.size() == 3) { const int in_circle_num = count_if(poly.begin(), poly.end(), [=](const Point &p) { return is_in_Circle(c, p); }); if (in_circle_num == 1) { while (!is_in_Circle(c, poly[0])) { rotate(poly.begin(), poly.begin() + 1, poly.end()); } } else if (in_circle_num == 2) { while (is_in_Circle(c, poly[2])) { rotate(poly.begin(), poly.begin() + 1, poly.end()); } } vector<Point> cas = is_sc(c, Line(poly[0], poly[1])); vector<Point> cbs = is_sc(c, Line(poly[1], poly[2])); vector<Point> ccs = is_sc(c, Line(poly[2], poly[0])); switch (in_circle_num) { case 0: { ld ans; if (cas.empty() && cbs.empty() && ccs.empty()) { if (is_in_Polygon(poly, c.p)) { ans = c.r * c.r * pi; } else { ans = 0; } } else { if (!cas.empty() && cbs.empty() && ccs.empty()) { rotate(poly.begin(), poly.begin() + 2, poly.end()); vector<Point> ccc = ccs; ccs = cbs; cbs = cas; cas = ccc; } if (cas.empty() && cbs.empty() && !ccs.empty()) { rotate(poly.begin(), poly.begin() + 1, poly.end()); vector<Point> ccc = cas; cas = cbs; cbs = ccs; ccs = ccc; } if (cas.empty() && !cbs.empty() && ccs.empty()) { Point cb1 = cbs[0], cb2 = cbs[1]; { ld theta = gettheta(cb2 - c.p, cb1 - c.p); if (theta < eps && is_in_Polygon(poly, c.p)) theta += 2 * pi; ans = c.r * c.r * theta / 2; } Polygon anspoly{cb1, cb2, c.p}; ans += get_area(anspoly); } else { if (cas.empty() && !cbs.empty() && !ccs.empty()) { rotate(poly.begin(), poly.begin() + 2, poly.end()); vector<Point> ccc = ccs; ccs = cbs; cbs = cas; cas = ccc; } if (!cas.empty() && !cbs.empty() && ccs.empty()) { rotate(poly.begin(), poly.begin() + 1, poly.end()); vector<Point> ccc = cas; cas = cbs; cbs = ccs; ccs = ccc; } if (!cas.empty() && cbs.empty() && !ccs.empty()) { Point ca1 = cas[0], ca2 = cas[1]; Point cc1 = ccs[0], cc2 = ccs[1]; { ld theta1 = gettheta(ca2 - c.p, cc1 - c.p); ld theta2 = gettheta(cc2 - c.p, ca1 - c.p); // assert(theta1>eps&&theta2>eps); // assert(abs(ca1 - ca2) > eps&&abs(cc1 - cc2) > eps); ans = c.r * c.r * theta1 / 2; ans += c.r * c.r * theta2 / 2; } Polygon anspoly1{ca1, ca2, c.p}; Polygon anspoly2{cc1, cc2, c.p}; ans += get_area(anspoly1) + get_area(anspoly2); } else { Point ca1 = cas[0], ca2 = cas[1]; Point cb1 = cbs[0], cb2 = cbs[1]; Point cc1 = ccs[0], cc2 = ccs[1]; { ld theta1 = gettheta(ca2, cb1); ld theta2 = gettheta(cb2, cc1); ld theta3 = gettheta(cc2, ca1); ans = c.r * c.r * theta1 / 2; ans += c.r * c.r * theta2 / 2; ans += c.r * c.r * theta3 / 2; } Polygon anspoly1{ca1, ca2, c.p}; Polygon anspoly2{cb1, cb2, c.p}; Polygon anspoly3{cc1, cc2, c.p}; ans += get_area(anspoly1) + get_area(anspoly2) + get_area(anspoly3); } } } return ans; } break; case 1: { ld ans = 0; if (cbs.empty()) { Point ca = cas.back(), cc = ccs[0]; { ld theta = gettheta(ca - c.p, cc - c.p); ans += c.r * c.r * theta / 2; } Polygon anspoly{poly[0], ca, c.p, cc}; ans += get_area(anspoly); } else { Point ca = cas.back(), cb1 = cbs[0], cb2 = cbs[1], cc = ccs[0]; Polygon anspoly1{poly[0], ca, c.p, cc}; Polygon anspoly2{cb1, cb2, c.p}; ans = get_area(anspoly1) + get_area(anspoly2); { ld theta1 = gettheta(ca - c.p, cb1 - c.p); ans += c.r * c.r * theta1 / 2; ld theta2 = gettheta(cb2 - c.p, cc - c.p); ans += c.r * c.r * theta2 / 2; } } return ans; } break; case 2: { ld ans; Point cb = cbs.back(), cc = ccs[0]; Polygon anspo{poly[0], poly[1], cb, c.p, cc}; ans = get_area(anspo); { ld theta = gettheta(cb - c.p, cc - c.p); ans += c.r * c.r * theta / 2; } return ans; } break; case 3: return get_area(poly); break; } } } int main() { cin >> n >> r; vector<Point> poly(n); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; poly[i] = Point(x, y); } Circle c(Point(0, 0), r); ld ans = aget(c, poly); cout << setprecision(10) << fixed << ans << endl; return 0; }
replace
643
645
643
645
0
p02314
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define irep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++) using namespace std; int main() { int N, W; int v[1005]; int dp[1005] = {0}; cin >> N >> W; rep(i, W) { cin >> v[i]; } rep(i, N + 1) { dp[i] = i; } rep(i, W) { rep(j, N + 1) { if ((j - v[i]) > -1) { dp[j] = min(dp[j - v[i]] + 1, dp[j]); } } } cout << dp[N] << endl; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define irep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++) using namespace std; int main() { int N, W; int v[30]; int dp[50005] = {0}; cin >> N >> W; rep(i, W) { cin >> v[i]; } rep(i, N + 1) { dp[i] = i; } rep(i, W) { rep(j, N + 1) { if ((j - v[i]) > -1) { dp[j] = min(dp[j - v[i]] + 1, dp[j]); } } } cout << dp[N] << endl; }
replace
25
27
25
27
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef long long ll; #define mod 1000000 #define INF 10000000 int main() { int n, m; cin >> n >> m; vi C(m); for (int i = 0; i < m; i++) { cin >> C[i]; } vi dp(2 * n + 10, INF); dp[0] = 0; for (int i = 0; i < m; i++) { int c = C[i]; for (int j = n; j >= 0; j--) { for (int k = 1; j + k * c <= n; k++) { dp[j + k * c] = min(dp[j + k * c], k + dp[j]); } } /*for (int j = 0; j <= n; j++) { cout << dp[j] << " "; }cout << endl;*/ } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef long long ll; #define mod 1000000 #define INF 10000000 int main() { int n, m; cin >> n >> m; vi C(m); for (int i = 0; i < m; i++) { cin >> C[i]; } vi dp(2 * n + 10, INF); dp[0] = 0; for (int i = 0; i < m; i++) { int c = C[i]; for (int j = 0; j <= n; j++) { dp[j + c] = min(dp[j + c], 1 + dp[j]); } /*for (int j = 0; j <= n; j++) { cout << dp[j] << " "; }cout << endl;*/ } cout << dp[n] << endl; return 0; }
replace
25
29
25
27
TLE
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int n, m; const int MAX_N = 50000, MAX_M = 20, INF = 1000000; int c[MAX_M + 1]; int dp[MAX_M + 1][MAX_N + 2]; void solve() { for (int j = 0; j <= n; j++) { dp[0][j] = INF; } for (int i = 0; i <= m; i++) { for (int j = 1; j <= n; j++) { if (j < c[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); } } } cout << dp[m][n] << endl; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> c[i]; } solve(); return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, m; const int MAX_N = 50000, MAX_M = 20, INF = 1000000; int c[MAX_M + 2]; int dp[MAX_M + 2][MAX_N + 2]; void solve() { for (int j = 0; j <= n; j++) { dp[0][j] = INF; } for (int i = 0; i <= m; i++) { for (int j = 1; j <= n; j++) { if (j < c[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); } } } cout << dp[m][n] << endl; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> c[i]; } solve(); return 0; }
replace
7
9
7
9
0
p02314
C++
Runtime Error
#include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <thread> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR_LT(i, beg, end) for (int i = beg; i < end; i++) #define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++) #define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--) using namespace std; int main() { int n, m; cin >> n >> m; ; vector<int> ms(m); for (auto &coin : ms) { cin >> coin; } sort(ms.begin(), ms.end(), greater<int>()); array<int, 10001> dp = {0}; FOR_LE(i, 1, n) { int min_c = INT_MAX; FOR_LT(c, 0, m) { if (i - ms[c] < 0) continue; min_c = min(min_c, dp[i - ms[c]] + 1); } dp[i] = min_c; } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <thread> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR_LT(i, beg, end) for (int i = beg; i < end; i++) #define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++) #define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--) using namespace std; int main() { int n, m; cin >> n >> m; ; vector<int> ms(m); for (auto &coin : ms) { cin >> coin; } sort(ms.begin(), ms.end(), greater<int>()); array<int, 50001> dp = {0}; FOR_LE(i, 1, n) { int min_c = INT_MAX; FOR_LT(c, 0, m) { if (i - ms[c] < 0) continue; min_c = min(min_c, dp[i - ms[c]] + 1); } dp[i] = min_c; } cout << dp[n] << endl; return 0; }
replace
40
41
40
41
0
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int main() { int i, j, n, m, t[10001], c[21]; cin >> n >> m; for (i = 0; i < m; i++) cin >> c[i]; for (j = 0; j <= n; j++) t[j] = 10001; t[0] = 0; for (i = 0; i < m; i++) { for (j = c[i]; j <= n; j++) { t[j] = min(t[j], t[j - c[i]] + 1); } } cout << t[n] << endl; ; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int i, j, n, m, t[50001], c[21]; cin >> n >> m; for (i = 0; i < m; i++) cin >> c[i]; for (j = 0; j <= n; j++) t[j] = 10001; t[0] = 0; for (i = 0; i < m; i++) { for (j = c[i]; j <= n; j++) { t[j] = min(t[j], t[j - c[i]] + 1); } } cout << t[n] << endl; ; return 0; }
replace
5
6
5
6
0
p02314
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int totalValueOfShares; // target sum int size; // number of coin types cin >> totalValueOfShares >> size; vector<int> numOfShares(size); vector<int> values(totalValueOfShares); for (int i = 0; i < size; i++) { cin >> numOfShares[i]; } values[0] = 0; int minValue = 0x7FFFFFFF; for (int i = 0; i < size; i++) { if (numOfShares[i] < minValue) { minValue = numOfShares[i]; } } for (int i = 1; i <= totalValueOfShares; i++) { values[i] = 0x7FFFFFFF; } for (int i = minValue; i <= totalValueOfShares; i++) { for (int j = 0; j < size; j++) if (numOfShares[j] <= i) { int sum = values[i - numOfShares[j]]; if (sum < 0x7FFFFFFF && sum + 1 < values[i]) values[i] = sum + 1; } } if (values[totalValueOfShares] == 0x7FFFFFFF) { cout << 0; } cout << values[totalValueOfShares] << "\r\n"; }
#include <iostream> #include <vector> using namespace std; int main() { int totalValueOfShares; // target sum int size; // number of coin types cin >> totalValueOfShares >> size; vector<int> numOfShares(size); vector<int> values(totalValueOfShares + 1); for (int i = 0; i < size; i++) { cin >> numOfShares[i]; } values[0] = 0; int minValue = 0x7FFFFFFF; for (int i = 0; i < size; i++) { if (numOfShares[i] < minValue) { minValue = numOfShares[i]; } } for (int i = 1; i <= totalValueOfShares; i++) { values[i] = 0x7FFFFFFF; } for (int i = minValue; i <= totalValueOfShares; i++) { for (int j = 0; j < size; j++) if (numOfShares[j] <= i) { int sum = values[i - numOfShares[j]]; if (sum < 0x7FFFFFFF && sum + 1 < values[i]) values[i] = sum + 1; } } if (values[totalValueOfShares] == 0x7FFFFFFF) { cout << 0; } cout << values[totalValueOfShares] << "\r\n"; }
replace
13
14
13
14
0
p02314
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define vvi vector<vector<int>> #define vec vector #define pq priority_queue #define all(v) (v).begin(), (v).end() #define uniqueV(x) \ sort(x.begin(), x.end()); \ x.erase(unique(x.begin(), x.end()), x.end()); #define rep(i, n) for (int(i) = (0); (i) < (n); ++(i)) #define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i)) #define debug(x) cerr << #x << ": " << x << endl; #define debug2(x, y) \ cerr << "(" << #x << ", " << #y << ") = " \ << "(" << x << ", " << y << ")" << endl; #define debug3(x, y, z) \ cerr << "(" << #x << ", " << #y << ", " << #z << ") = " \ << "(" << x << ", " << y << ", " << z << ")" << endl; #define debugB(value, size) \ cerr << #value << ": " << bitset<size>(value) << endl; #define line() cerr << "---------------" << endl; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const double PI = 3.14159265358979323846; template <typename T> void printA(vector<T> &printArray, char between = ' ') { int paSize = printArray.size(); for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between; } if (between != '\n') { cerr << endl; } } // ------------------------------------------------------------------------------------------ const int INF = 1e8; int n, m; int c[22]; int dp[11111]; int main() { cin >> n >> m; rep(i, m) { cin >> c[i]; } fill(dp, dp + n + 1, INF); dp[0] = 0; for (int i = 1; i <= n; i++) { rep(j, m) { dp[i] = min(dp[i], dp[i - 1] + 1); if (i - c[j] >= 0) { dp[i] = min(dp[i], dp[i - c[j]] + 1); } } } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define vvi vector<vector<int>> #define vec vector #define pq priority_queue #define all(v) (v).begin(), (v).end() #define uniqueV(x) \ sort(x.begin(), x.end()); \ x.erase(unique(x.begin(), x.end()), x.end()); #define rep(i, n) for (int(i) = (0); (i) < (n); ++(i)) #define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i)) #define debug(x) cerr << #x << ": " << x << endl; #define debug2(x, y) \ cerr << "(" << #x << ", " << #y << ") = " \ << "(" << x << ", " << y << ")" << endl; #define debug3(x, y, z) \ cerr << "(" << #x << ", " << #y << ", " << #z << ") = " \ << "(" << x << ", " << y << ", " << z << ")" << endl; #define debugB(value, size) \ cerr << #value << ": " << bitset<size>(value) << endl; #define line() cerr << "---------------" << endl; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const double PI = 3.14159265358979323846; template <typename T> void printA(vector<T> &printArray, char between = ' ') { int paSize = printArray.size(); for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between; } if (between != '\n') { cerr << endl; } } // ------------------------------------------------------------------------------------------ const int INF = 1e8; int n, m; int c[22]; int dp[55555]; int main() { cin >> n >> m; rep(i, m) { cin >> c[i]; } fill(dp, dp + n + 1, INF); dp[0] = 0; for (int i = 1; i <= n; i++) { rep(j, m) { dp[i] = min(dp[i], dp[i - 1] + 1); if (i - c[j] >= 0) { dp[i] = min(dp[i], dp[i - c[j]] + 1); } } } cout << dp[n] << endl; return 0; }
replace
43
44
43
44
0
p02314
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; typedef long long LL; ostream &debug = cout; /* AOJ DPL_1_A */ const int N = 25; const int M = 50005; const int INF = 0x3f3f3f3f; int n, m, a[N], dp[M]; signed main(void) { /* input */ { scanf("%d%d", &m, &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); } /* dynamic programming */ { memset(dp, 0x3f, sizeof(dp)), dp[0] = 0; for (int i = 1, sum = 0; i <= n; sum += a[i++]) for (int j = 0; j <= m; j++) dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1); } /* output */ { printf("%d\n", dp[m]); } // system("pause"); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; typedef long long LL; ostream &debug = cout; /* AOJ DPL_1_A */ const int N = 25; const int M = 60005; const int INF = 0x3f3f3f3f; int n, m, a[N], dp[M]; signed main(void) { /* input */ { scanf("%d%d", &m, &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); } /* dynamic programming */ { memset(dp, 0x3f, sizeof(dp)), dp[0] = 0; for (int i = 1, sum = 0; i <= n; sum += a[i++]) for (int j = 0; j <= m; j++) dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1); } /* output */ { printf("%d\n", dp[m]); } // system("pause"); }
replace
14
15
14
15
0
p02314
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m; int c[21]; int dp[21][50001]; int main() { cin >> n >> m; for (int i = 0; i < 21; i++) { dp[i][0] = 0; } for (int i = 0; i < 21; i++) { for (int k = 1; k < 50001; k++) { dp[i][k] = 20000; } } for (int i = 1; i <= m; i++) { cin >> c[i]; } for (int i = 1; i <= m; i++) { for (int k = 1; k <= 500000; k++) { if (k >= c[i]) dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]); else dp[i][k] = dp[i - 1][k]; } } cout << dp[m][n] << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; int c[21]; int dp[21][50001]; int main() { cin >> n >> m; for (int i = 0; i < 21; i++) { dp[i][0] = 0; } for (int i = 0; i < 21; i++) { for (int k = 1; k < 50001; k++) { dp[i][k] = 20000; } } for (int i = 1; i <= m; i++) { cin >> c[i]; } for (int i = 1; i <= m; i++) { for (int k = 1; k <= 50000; k++) { if (k >= c[i]) dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]); else dp[i][k] = dp[i - 1][k]; } } cout << dp[m][n] << endl; }
replace
20
21
20
21
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define MAX_NUM_COINS 20 #define MAX_PRICE 50000 #define CALC_MIN(a, b) (a < b ? a : b) int coin[MAX_NUM_COINS]; int memo[MAX_NUM_COINS][MAX_PRICE + 1]; int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; } int solve_sub_question(int index, int num_coin, int price) { if (memo[index][price] != -1) { return memo[index][price]; } if (index == num_coin - 1) { memo[index][price] = price; return price; } int ans = INT_MAX; for (int i = 0; i <= price / coin[index]; i++) { int total_coin = i; int current_price = price - coin[index] * i; total_coin += solve_sub_question(index + 1, num_coin, current_price); ans = CALC_MIN(total_coin, ans); } memo[index][price] = ans; return ans; } int main() { memset(memo, -1, sizeof(memo)); for (int i = 0; i < MAX_NUM_COINS; i++) { memo[i][0] = 0; } int price, num_coin; scanf("%d%d", &price, &num_coin); for (int i = 0; i < num_coin; i++) { scanf("%d", &coin[i]); } qsort(coin, num_coin, sizeof(int), is_less_than); printf("%d\n", solve_sub_question(0, num_coin, price)); return 0; }
#include <algorithm> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define MAX_NUM_COINS 20 #define MAX_PRICE 50000 #define CALC_MIN(a, b) (a < b ? a : b) int coin[MAX_NUM_COINS]; int memo[MAX_NUM_COINS][MAX_PRICE + 1]; int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; } int solve_sub_question(int index, int num_coin, int price) { if (memo[index][price] != -1) { return memo[index][price]; } if (index == num_coin - 1) { memo[index][price] = price; return price; } if (price % coin[index] == 0) { memo[index][price] = price / coin[index]; return price / coin[index]; } int ans = INT_MAX; for (int i = 0; i <= price / coin[index]; i++) { int total_coin = i; int current_price = price - coin[index] * i; total_coin += solve_sub_question(index + 1, num_coin, current_price); ans = CALC_MIN(total_coin, ans); } memo[index][price] = ans; return ans; } int main() { memset(memo, -1, sizeof(memo)); for (int i = 0; i < MAX_NUM_COINS; i++) { memo[i][0] = 0; } int price, num_coin; scanf("%d%d", &price, &num_coin); for (int i = 0; i < num_coin; i++) { scanf("%d", &coin[i]); } qsort(coin, num_coin, sizeof(int), is_less_than); printf("%d\n", solve_sub_question(0, num_coin, price)); return 0; }
insert
24
24
24
28
TLE
p02314
C++
Runtime Error
#include <iostream> // #define int long long #define SIZE 21 #define VALUE 10001 const int infty = 999; // #define _DEBUG using namespace std; int N, M, Cs[SIZE], T[SIZE][VALUE]; int solve() { for (int i = 0; i <= M; i++) { for (int n = 0; n <= N; n++) { if (n == 0) { T[i][n] = 0; } else if (i == 0) { T[i][n] = infty; } else if (n < Cs[i - 1]) { T[i][n] = T[i - 1][n]; } else { T[i][n] = min(T[i - 1][n], min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1); } } } #ifdef _DEBUG for (int i = 0; i <= M; i++) { if (i > 0) cout << Cs[i - 1] << "\t"; else cout << "i\\n\t"; for (int n = 0; n <= N; n++) { cout << T[i][n] << "\t"; } cout << endl; } #endif return T[M][N]; } signed main() { while (cin >> N >> M) { for (int i = 0; i < M; i++) { cin >> Cs[i]; } cout << solve() << endl; } }
#include <iostream> // #define int long long #define SIZE 21 #define VALUE 50001 const int infty = 99999; // #define _DEBUG using namespace std; int N, M, Cs[SIZE], T[SIZE][VALUE]; int solve() { for (int i = 0; i <= M; i++) { for (int n = 0; n <= N; n++) { if (n == 0) { T[i][n] = 0; } else if (i == 0) { T[i][n] = infty; } else if (n < Cs[i - 1]) { T[i][n] = T[i - 1][n]; } else { T[i][n] = min(T[i - 1][n], min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1); } } } #ifdef _DEBUG for (int i = 0; i <= M; i++) { if (i > 0) cout << Cs[i - 1] << "\t"; else cout << "i\\n\t"; for (int n = 0; n <= N; n++) { cout << T[i][n] << "\t"; } cout << endl; } #endif return T[M][N]; } signed main() { while (cin >> N >> M) { for (int i = 0; i < M; i++) { cin >> Cs[i]; } cout << solve() << endl; } }
replace
3
5
3
5
0
p02314
C++
Runtime Error
#include <algorithm> #include <cstdlib> #include <stdio.h> using namespace std; const int N_MAX = 10001, INF = 100000; int dp[N_MAX], n, m, i, j, d[20]; int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d", &d[i]); } fill(dp, dp + n + 1, INF); dp[0] = 0; for (i = 0; i <= n; i++) { for (j = 0; j < m; j++) { if (d[j] <= i) { dp[i] = min(dp[i - d[j]] + 1, dp[i]); } } } printf("%d\n", dp[n]); return 0; }
#include <algorithm> #include <cstdlib> #include <stdio.h> using namespace std; const int N_MAX = 50001, INF = 100000; int dp[N_MAX], n, m, i, j, d[20]; int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d", &d[i]); } fill(dp, dp + n + 1, INF); dp[0] = 0; for (i = 0; i <= n; i++) { for (j = 0; j < m; j++) { if (d[j] <= i) { dp[i] = min(dp[i - d[j]] + 1, dp[i]); } } } printf("%d\n", dp[n]); return 0; }
replace
6
7
6
7
0
p02314
C++
Time Limit Exceeded
// Coin Changing Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1000000 int main() { ll n, m; cin >> n >> m; vector<ll> c; for (ll i = 0; i < m; i++) { ll tmp; cin >> tmp; c.push_back(tmp); } ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????° for (ll i = 0; i <= n; i++) dp[i] = INF; dp[0] = 0; for (ll i = 0; i < c.size(); i++) { for (ll j = 0; j <= n; j++) { ll mini = dp[j]; for (ll k = 1; j - c[i] * k >= 0; k++) { if (mini > dp[j - c[i] * k] + k) mini = dp[j - c[i] * k] + k; } dp[j] = mini; } } cout << dp[n] << endl; return 0; }
// Coin Changing Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1000000 int main() { ll n, m; cin >> n >> m; vector<ll> c; for (ll i = 0; i < m; i++) { ll tmp; cin >> tmp; c.push_back(tmp); } ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????° for (ll i = 0; i <= n; i++) dp[i] = INF; dp[0] = 0; for (ll i = 0; i < c.size(); i++) { for (ll j = 0; j <= n; j++) { if (c[i] <= j) dp[j] = min(dp[j], dp[j - c[i]] + 1); } } cout << dp[n] << endl; return 0; }
replace
22
28
22
24
TLE
p02314
C++
Time Limit Exceeded
#include <iostream> #include <stdio.h> #include <string> using namespace std; #define N_MAX 50000 #define M_MAX 20 #define NIL 1000000000 int DP[N_MAX + 1][M_MAX + 1]; int C[M_MAX + 1]; int main(void) { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d", &C[i]); } for (int i = 0; i <= n; i++) { for (int k = 0; k <= m; k++) { DP[i][k] = NIL; } } for (int k = 0; k <= m; k++) { DP[0][k] = 0; } for (int k = 1; k <= m; k++) { for (int i = 0; i <= n; i++) { int coin_n = 0; while (i - C[k] * coin_n >= 0) { DP[i][k] = min(DP[i][k], DP[i - C[k] * coin_n][k - 1] + coin_n); if (DP[i][k] < coin_n) { break; } coin_n++; } // printf("DP[%d][%d]: %d\n", i, k, DP[i][k]); } } printf("%d\n", DP[n][m]); }
#include <iostream> #include <stdio.h> #include <string> using namespace std; #define N_MAX 50000 #define M_MAX 20 #define NIL 1000000000 int DP[N_MAX + 1][M_MAX + 1]; int C[M_MAX + 1]; int main(void) { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d", &C[i]); } for (int i = 0; i <= n; i++) { for (int k = 0; k <= m; k++) { DP[i][k] = NIL; } } for (int k = 0; k <= m; k++) { DP[0][k] = 0; } for (int k = 1; k <= m; k++) { for (int i = 0; i <= n; i++) { if (i - C[k] < 0) { DP[i][k] = DP[i][k - 1]; } else { DP[i][k] = min(DP[i][k - 1], DP[i - C[k]][k] + 1); } // printf("DP[%d][%d]: %d\n", i, k, DP[i][k]); } } printf("%d\n", DP[n][m]); }
replace
30
37
30
34
TLE
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <stack> #include <string> using namespace std; const long long int MOD = 1000000007; const long long int INF = 100000000; long long int N, M; long long int C[30]; long long int dp[21][100000]; int DP(int n, int v) { if (n < 0 || v < 0) return INF; if (dp[n][v] != -1) return dp[n][v]; return min(DP(n - 1, v), DP(n, v - C[n]) + 1); } int main() { cin >> N >> M; for (int i = 0; i < M; i++) { cin >> C[i]; } memset(dp, -1, sizeof(dp)); for (int i = 0; i < 21; i++) { dp[i][0] = 0; } cout << DP(M - 1, N) << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <stack> #include <string> using namespace std; const long long int MOD = 1000000007; const long long int INF = 100000000; long long int N, M; long long int C[30]; long long int dp[21][100000]; int DP(int n, int v) { if (n < 0 || v < 0) return INF; if (dp[n][v] != -1) return dp[n][v]; return dp[n][v] = min(DP(n - 1, v), DP(n, v - C[n]) + 1); } int main() { cin >> N >> M; for (int i = 0; i < M; i++) { cin >> C[i]; } memset(dp, -1, sizeof(dp)); for (int i = 0; i < 21; i++) { dp[i][0] = 0; } cout << DP(M - 1, N) << endl; return 0; }
replace
23
24
23
24
TLE
p02314
C++
Runtime Error
#include <stdio.h> #include <algorithm> using namespace std; int ans[50000 + 1]; int coin[20]; int main() { int n, m; scanf("%d %d", &n, &m); int i, j; for (i = 0; i < m; ++i) { scanf("%d", &coin[i]); ++ans[coin[i]]; } for (i = 1; i < n; ++i) { for (j = 0; j < m; ++j) { if (ans[i] + coin[j] <= n) { ans[i + coin[j]] = ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1; } } } printf("%d\n", ans[n]); return 0; }
#include <stdio.h> #include <algorithm> using namespace std; int ans[50000 + 1]; int coin[20]; int main() { int n, m; scanf("%d %d", &n, &m); int i, j; for (i = 0; i < m; ++i) { scanf("%d", &coin[i]); ++ans[coin[i]]; } for (i = 1; i < n; ++i) { for (j = 0; j < m; ++j) { if (i + coin[j] <= n) { ans[i + coin[j]] = ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1; } } } printf("%d\n", ans[n]); return 0; }
replace
23
24
23
24
0
p02314
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++) #define REP(i, e) FOR(i, 0, e) #define all(o) (o).begin(), (o).end() #define psb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) typedef long long ll; typedef pair<int, int> PII; const double EPS = 1e-10; const int INF = 1000 * 1000 * 1000; const int N = 500000; const int M = 20; int n, m; int dp[M + 1][N + 1]; int c[N + 1]; int main() { int i, j, k; scanf("%d%d ", &n, &m); for (i = 0; i < m; i++) scanf("%d ", c + i + 1); for (i = 0; i <= m; i++) for (j = 0; j <= n; j++) dp[i][j] = INF; for (j = 0; j <= n; j++) if (!(j % c[1])) dp[1][j] = j / c[1]; for (i = 2; i <= m; i++) for (j = 0; j <= n; j++) for (k = 0; j - c[i] * k >= 0; k++) dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i] * k] + k); printf("%d\n", dp[m][n]); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++) #define REP(i, e) FOR(i, 0, e) #define all(o) (o).begin(), (o).end() #define psb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) typedef long long ll; typedef pair<int, int> PII; const double EPS = 1e-10; const int INF = 1000 * 1000 * 1000; const int N = 500000; const int M = 20; int n, m; int dp[M + 1][N + 1]; int c[N + 1]; int main() { int i, j, k; scanf("%d%d ", &n, &m); for (i = 0; i < m; i++) scanf("%d ", c + i + 1); for (i = 0; i <= m; i++) for (j = 0; j <= n; j++) dp[i][j] = INF; for (j = 0; j <= n; j++) if (!(j % c[1])) dp[1][j] = j / c[1]; for (i = 2; i <= m; i++) for (j = 0; j <= n; j++) { if (j - c[i] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1); // for (k=0; j-c[i]*k>=0; k++) // dp[i][j] = min(dp[i][j], dp[i-1][j-c[i]*k]+k); } printf("%d\n", dp[m][n]); return 0; }
replace
38
41
38
46
TLE
p02314
C++
Runtime Error
#include <iostream> #include <string.h> using namespace std; #define MAX_N 70 int main() { int dp[MAX_N]; int n, m, c[100]; memset(dp, -1, sizeof(dp)); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> c[i]; } for (int i = 1; i <= m; i++) { dp[c[i]] = 1; } for (int i = 1; i <= 20; i++) { for (int j = 1; j <= n; j++) { if (dp[j] != -1) { for (int k = 1; k <= m; k++) { if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) { dp[j + c[k]] = dp[j] + 1; } } } } } cout << dp[n] << endl; return 0; }
#include <iostream> #include <string.h> using namespace std; #define MAX_N 1000000 int main() { int dp[MAX_N]; int n, m, c[100]; memset(dp, -1, sizeof(dp)); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> c[i]; } for (int i = 1; i <= m; i++) { dp[c[i]] = 1; } for (int i = 1; i <= 20; i++) { for (int j = 1; j <= n; j++) { if (dp[j] != -1) { for (int k = 1; k <= m; k++) { if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) { dp[j + c[k]] = dp[j] + 1; } } } } } cout << dp[n] << endl; return 0; }
replace
4
5
4
5
0
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { constexpr int INF = 1e9; int n, m; cin >> n >> m; vector<int> dp(n + 1, INF), d(m); for (auto i = 0; i < m; i++) { cin >> d[i]; dp[d[i]] = 1; } for (auto i = 0; i < n + 1; i++) { for (auto &j : d) { if (i + j > n + 1) continue; dp[i + j] = min(dp[i + j], dp[i] + 1); } } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { constexpr int INF = 1e9; int n, m; cin >> n >> m; vector<int> dp(n + 1, INF), d(m); for (auto i = 0; i < m; i++) { cin >> d[i]; dp[d[i]] = 1; } for (auto i = 0; i < n + 1; i++) { for (auto &j : d) { if (i + j >= n + 1) continue; dp[i + j] = min(dp[i + j], dp[i] + 1); } } cout << dp[n] << endl; return 0; }
replace
15
16
15
16
0
p02314
C++
Runtime Error
/// // File: dpl_1_a.cpp // Author: ymiyamoto // // Created on Wed Nov 1 01:41:22 2017 // #include <cstdint> #include <iostream> #include <vector> using namespace std; #define MAX_N 10001 static vector<int32_t> coins; static uint32_t dp[MAX_N]; int32_t main() { int32_t n, m; cin >> n >> m; for (int32_t i = 0; i < m; i++) { int32_t c; cin >> c; coins.push_back(c); } for (int32_t i = 0; i <= n; i++) { dp[i] = UINT32_MAX; dp[0] = 0; for (int32_t j = 0; j < (int32_t)coins.size(); j++) { if (i - coins[j] >= 0) { dp[i] = min(dp[i], 1 + dp[i - coins[j]]); } } } cout << dp[n] << endl; return 0; }
/// // File: dpl_1_a.cpp // Author: ymiyamoto // // Created on Wed Nov 1 01:41:22 2017 // #include <cstdint> #include <iostream> #include <vector> using namespace std; #define MAX_N 50001 static vector<int32_t> coins; static uint32_t dp[MAX_N]; int32_t main() { int32_t n, m; cin >> n >> m; for (int32_t i = 0; i < m; i++) { int32_t c; cin >> c; coins.push_back(c); } for (int32_t i = 0; i <= n; i++) { dp[i] = UINT32_MAX; dp[0] = 0; for (int32_t j = 0; j < (int32_t)coins.size(); j++) { if (i - coins[j] >= 0) { dp[i] = min(dp[i], 1 + dp[i - coins[j]]); } } } cout << dp[n] << endl; return 0; }
replace
12
13
12
13
0
p02314
C++
Runtime Error
#include <bits/stdc++.h> #define M 21 #define N 50010 using namespace std; int dp[N]; int Min(int &a, int b) { return a = min(a, b); } int main() { int n, m; cin >> n >> m; int coin[M]; for (int i = 0; i < m; i++) cin >> coin[i]; for (int i = 0; i < N; i++) dp[i] = 1e9; dp[0] = 0; for (int i = 0; i < m; i++) for (int j = 0; j <= n; j++) Min(dp[j + coin[i]], dp[j] + 1); cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> #define M 21 #define N 100010 using namespace std; int dp[N]; int Min(int &a, int b) { return a = min(a, b); } int main() { int n, m; cin >> n >> m; int coin[M]; for (int i = 0; i < m; i++) cin >> coin[i]; for (int i = 0; i < N; i++) dp[i] = 1e9; dp[0] = 0; for (int i = 0; i < m; i++) for (int j = 0; j <= n; j++) Min(dp[j + coin[i]], dp[j] + 1); cout << dp[n] << endl; return 0; }
replace
2
3
2
3
0
p02314
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n, m, t; int c[21]; int T[21][10001]; cin >> n >> m; int counter = 0; for (int k = 0; k < m; ++k) { cin >> t; c[k] = t; if (c[k] > n) { c[k] = c[k + 1]; counter++; } for (int j = 1; j <= n; ++j) { T[k][0] = 0; T[0][j] = 100000; } } int m1 = m - counter; for (int i = 1; i <= m1; ++i) { for (int j = 1; j <= n; ++j) { if (c[i - 1] <= j) { T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1); } else T[i][j] = T[i - 1][j]; } } cout << T[m1][n] << endl; }
#include <iostream> using namespace std; int main() { int n, m, t; int c[21]; int T[21][100001]; cin >> n >> m; int counter = 0; for (int k = 0; k < m; ++k) { cin >> t; c[k] = t; if (c[k] > n) { c[k] = c[k + 1]; counter++; } for (int j = 1; j <= n; ++j) { T[k][0] = 0; T[0][j] = 100000; } } int m1 = m - counter; for (int i = 1; i <= m1; ++i) { for (int j = 1; j <= n; ++j) { if (c[i - 1] <= j) { T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1); } else T[i][j] = T[i - 1][j]; } } cout << T[m1][n] << endl; }
replace
6
7
6
7
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; unsigned short dp[20][50010]; int main(void) { int N, M; cin >> N >> M; vector<int> c(M); for (auto &i : c) cin >> i; sort(c.begin(), c.end()); for (int j = 0; j <= N; j++) dp[0][j] = j; int m; for (int i = 1; i < M; i++) { for (int j = 0; j <= N; j++) { m = dp[i - 1][j]; for (int k = 1; j >= k * c[i]; k++) m = min(m, dp[i - 1][j - k * c[i]] + k); dp[i][j] = m; } } cout << dp[M - 1][N] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; unsigned short dp[20][50010]; int main(void) { int N, M; cin >> N >> M; vector<int> c(M); for (auto &i : c) cin >> i; sort(c.begin(), c.end()); for (int j = 0; j <= N; j++) dp[0][j] = j; int m; for (int i = 1; i < M; i++) { for (int j = 0; j <= N; j++) { m = dp[i - 1][j]; for (int k = j / c[i]; j / c[i] - k < 10 && k > 0; k--) m = min(m, dp[i - 1][j - k * c[i]] + k); dp[i][j] = m; } } cout << dp[M - 1][N] << endl; return 0; }
replace
21
22
21
22
TLE
p02314
C++
Runtime Error
#include <iostream> #include <limits> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> dp(n); vector<int> coins(m); for (int i = 0; i < m; ++i) { int coin; cin >> coin; coins[i] = coin; } dp[0] = 0; for (int val = 1; val <= n; ++val) { int min = numeric_limits<int>::max(); for (auto coin : coins) { if (val >= coin) { min = dp[val - coin] < min ? dp[val - coin] : min; } } dp[val] = 1 + min; } cout << dp[n] << endl; }
#include <iostream> #include <limits> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> dp(n + 1); vector<int> coins(m); for (int i = 0; i < m; ++i) { int coin; cin >> coin; coins[i] = coin; } dp[0] = 0; for (int val = 1; val <= n; ++val) { int min = numeric_limits<int>::max(); for (auto coin : coins) { if (val >= coin) { min = dp[val - coin] < min ? dp[val - coin] : min; } } dp[val] = 1 + min; } cout << dp[n] << endl; }
replace
10
11
10
11
0
p02314
C++
Runtime Error
#include <stdio.h> int i, j, a, b, c[20], d[50001], min = 2000000000; int main(void) { scanf("%d %d", &a, &b); for (i = 0; i < b; i++) { scanf("%d", &c[i]); } for (j = 0; j <= a; j++) { d[j] = min; } d[0] = 0; for (i = 0; i < b; i++) { for (j = 0; j <= a; j++) { if (d[j] != min) { if (d[j] + 1 < d[j + c[i]] && j + c[i] <= a) { d[j + c[i]] = d[j] + 1; } } } } printf("%d\n", d[a]); return 0; }
#include <stdio.h> int i, j, a, b, c[20], d[50001], min = 2000000000; int main(void) { scanf("%d %d", &a, &b); for (i = 0; i < b; i++) { scanf("%d", &c[i]); } for (j = 0; j <= a; j++) { d[j] = min; } d[0] = 0; for (i = 0; i < b; i++) { for (j = 0; j <= a; j++) { if (d[j] != min && j + c[i] <= a) { if (d[j] + 1 < d[j + c[i]]) { d[j + c[i]] = d[j] + 1; } } } } printf("%d\n", d[a]); return 0; }
replace
14
16
14
16
0
p02314
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <vector> using namespace std; #define INF INT_MAX / 2 int main() { int n, m; int c[20]; cin >> n >> m; for (int i = 0; i < m; i++) cin >> c[i]; int dp[50000 + 1]; fill_n(dp, n + 1, INF); dp[0] = 0; for (int i = 0; i <= m; i++) for (int j = c[i]; j <= n; j++) dp[j] = min(dp[j], dp[j - c[i]] + 1); cout << dp[n] << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <vector> using namespace std; #define INF INT_MAX / 2 int main() { int n, m; int c[20]; cin >> n >> m; for (int i = 0; i < m; i++) cin >> c[i]; int dp[50000 + 1]; fill_n(dp, n + 1, INF); dp[0] = 0; for (int i = 0; i < m; i++) for (int j = c[i]; j <= n; j++) dp[j] = min(dp[j], dp[j - c[i]] + 1); cout << dp[n] << endl; return 0; }
replace
27
28
27
28
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; int coins[m]; for (int i = 0; i < m; ++i) { cin >> coins[i]; } sort(coins, coins + m, greater<>()); int dp[m + 1][50001]; for (int i = 0; i <= 50000; ++i) { if (i == 0) dp[m][i] = 0; else dp[m][i] = 1'000'000'000; } for (int i = m - 1; i >= 0; --i) { for (int j = 0; j <= n; ++j) { if (j < coins[i]) { dp[i][j] = dp[i + 1][j]; } else { int min_val = dp[i + 1][j]; for (int k = 0; k <= (j / coins[i]); ++k) { min_val = min(min_val, dp[i + 1][j - coins[i] * k] + k); } dp[i][j] = min_val; } } } cout << dp[0][n] << endl; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; int coins[m]; for (int i = 0; i < m; ++i) { cin >> coins[i]; } sort(coins, coins + m, greater<>()); int dp[m + 1][50001]; for (int i = 0; i <= 50000; ++i) { if (i == 0) dp[m][i] = 0; else dp[m][i] = 1'000'000'000; } for (int i = m - 1; i >= 0; --i) { for (int j = 0; j <= n; ++j) { if (j < coins[i]) { dp[i][j] = dp[i + 1][j]; } else { dp[i][j] = min(dp[i + 1][j], dp[i][j - coins[i]] + 1); } } } cout << dp[0][n] << endl; }
replace
28
33
28
29
TLE
p02314
C++
Runtime Error
// DPL_1_A.cpp // #include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; int n, m; int dp[10001], coin[20]; int solve() { fill(dp, dp + n + 1, INF); dp[0] = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (i + coin[j] <= n) dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1); return dp[n]; } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) cin >> coin[i]; cout << solve() << endl; ; return 0; }
// DPL_1_A.cpp // #include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; int n, m; int dp[50001], coin[20]; int solve() { fill(dp, dp + n + 1, INF); dp[0] = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (i + coin[j] <= n) dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1); return dp[n]; } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) cin >> coin[i]; cout << solve() << endl; ; return 0; }
replace
6
7
6
7
0
p02314
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define M 20 #define N 50001 int d[M]; int DP[M][N]; int solve(int i, int x) { int res = N; if (DP[i][x] != N) return DP[i][x]; for (int k = 0, kMax = x / d[i]; k <= kMax; k++) res = min(res, solve(i - 1, x - k * d[i]) + k); DP[i][x] = res; return res; } int main() { int n, m; cin >> n >> m; REP(i, M) cin >> d[i]; for (int x = 0; x <= n; x++) { for (int i = 0; i < m; i++) DP[i][x] = N; } for (int k = 0, kMax = n / d[0]; k <= kMax; k++) DP[0][k * d[0]] = k; cout << solve(m - 1, n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define M 20 #define N 50001 int d[M]; int DP[M][N]; int solve(int i, int x) { int res = N; if (DP[i][x] != N) return DP[i][x]; res = solve(i - 1, x); if (x - d[i] >= 0) res = min(res, solve(i, x - d[i]) + 1); DP[i][x] = res; return res; } int main() { int n, m; cin >> n >> m; REP(i, M) cin >> d[i]; for (int x = 0; x <= n; x++) { for (int i = 0; i < m; i++) DP[i][x] = N; } for (int k = 0, kMax = n / d[0]; k <= kMax; k++) DP[0][k * d[0]] = k; cout << solve(m - 1, n) << endl; return 0; }
replace
17
19
17
20
TLE
p02314
C++
Runtime Error
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define P(p) cout << (p) << endl; #define p(p) cout << (p) << " "; #define pb push_back #define mp make_pair #define INF 1 << 25 typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pi; typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int n, m; int c[21]; int dp[21][10001]; // pos,yen/min int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, m) cin >> c[i]; rep(i, n + 1) dp[0][i] = i * c[0]; REP(i, 1, m) { REP(j, 1, n + 1) { if (j - c[i] >= 0) dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1); else dp[i][j] = dp[i - 1][j]; } } /* rep(i,m){ REP(j,1,n+1)p(dp[i][j]); cout<<endl; }*/ P(dp[m - 1][n]); return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define P(p) cout << (p) << endl; #define p(p) cout << (p) << " "; #define pb push_back #define mp make_pair #define INF 1 << 25 typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pi; typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int n, m; int c[21]; int dp[21][50001]; // pos,yen/min int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, m) cin >> c[i]; rep(i, n + 1) dp[0][i] = i * c[0]; REP(i, 1, m) { REP(j, 1, n + 1) { if (j - c[i] >= 0) dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1); else dp[i][j] = dp[i - 1][j]; } } /* rep(i,m){ REP(j,1,n+1)p(dp[i][j]); cout<<endl; }*/ P(dp[m - 1][n]); return 0; }
replace
48
49
48
49
0
p02314
C++
Runtime Error
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define P(p) cout << (p) << endl; #define p(p) cout << (p) << " "; #define pb push_back #define mp make_pair #define INF 1 << 25 typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pi; typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int dp[21][10001]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vi c(m); rep(i, m) cin >> c[i]; rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF; dp[0][0] = 0; rep(i, m) { rep(j, n + 1) { if (j - c[i] >= 0) dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); else dp[i + 1][j] = dp[i][j]; } } /*REP(i,1,m+1){ rep(j,n+1)p(dp[i][j]); cout<<endl; }*/ P(dp[m][n]); return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define P(p) cout << (p) << endl; #define p(p) cout << (p) << " "; #define pb push_back #define mp make_pair #define INF 1 << 25 typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pi; typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int dp[21][50001]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vi c(m); rep(i, m) cin >> c[i]; rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF; dp[0][0] = 0; rep(i, m) { rep(j, n + 1) { if (j - c[i] >= 0) dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); else dp[i + 1][j] = dp[i][j]; } } /*REP(i,1,m+1){ rep(j,n+1)p(dp[i][j]); cout<<endl; }*/ P(dp[m][n]); return 0; }
replace
45
46
45
46
0
p02314
C++
Runtime Error
#include <iostream> static const int INF = int(1e8); using namespace std; int dp[10001] = {}; int denom[10001] = {}; int main() { int n; int m; cin >> n; cin >> m; for (int i = 0; i < m; i++) { cin >> denom[i]; } dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = INF; for (int j = 0; j <= m; j++) { if (i >= denom[j]) { dp[i] = min(dp[i], dp[i - denom[j]] + 1); } } } cout << dp[n] << "\n"; return 0; }
#include <iostream> static const int INF = int(1e8); using namespace std; int dp[55555] = {}; int denom[55555] = {}; int main() { int n; int m; cin >> n; cin >> m; for (int i = 0; i < m; i++) { cin >> denom[i]; } dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = INF; for (int j = 0; j <= m; j++) { if (i >= denom[j]) { dp[i] = min(dp[i], dp[i - denom[j]] + 1); } } } cout << dp[n] << "\n"; return 0; }
replace
4
6
4
6
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; class Coin { public: int n, m; vector<int> c; vector<vector<int>> dp; Coin() {} Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {} int solve(); }; int Coin::solve() { for (auto i = 0; i <= n; ++i) dp[0][i] = i; for (auto i = 0; i < m; ++i) dp[i][0] = 0; sort(c.begin(), c.end()); for (auto i = 1; i < m; ++i) { for (auto j = 1; j <= n; ++j) { int mc = dp[i - 1][j]; for (auto k = 1; j >= k * c[i]; ++k) { mc = min(mc, dp[i - 1][j - k * c[i]] + k); } dp[i][j] = mc; } } cout << dp[m - 1][n] << endl; return 0; } int main() { int n, m; cin >> n >> m; Coin c(n, m); for (auto i = 0; i < m; ++i) cin >> c.c[i]; c.solve(); return 0; }
#include <algorithm> #include <array> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; class Coin { public: int n, m; vector<int> c; vector<vector<int>> dp; Coin() {} Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {} int solve(); }; int Coin::solve() { for (auto i = 0; i <= n; ++i) dp[0][i] = i; for (auto i = 0; i < m; ++i) dp[i][0] = 0; sort(c.begin(), c.end()); for (auto i = 1; i < m; ++i) { for (auto j = 1; j <= n; ++j) { int mc = dp[i - 1][j]; if (j >= c[i]) { mc = min(mc, dp[i - 1][j - c[i]] + 1); mc = min(mc, dp[i][j - c[i]] + 1); } dp[i][j] = mc; } } cout << dp[m - 1][n] << endl; return 0; } int main() { int n, m; cin >> n >> m; Coin c(n, m); for (auto i = 0; i < m; ++i) cin >> c.c[i]; c.solve(); return 0; }
replace
48
50
48
51
TLE
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> #define N 10001 #define INFINITY 2000000000 using namespace std; const int M = 21; void getTheNumberOfCoin(); int n, m, T[M][N], C[M], i, j, minv; int main() { cin >> n >> m; for (i = 1; i <= m; i++) cin >> C[i]; getTheNumberOfCoin(); return 0; } void getTheNumberOfCoin() { for (i = 1; i <= n; i++) T[0][i] = INFINITY; for (i = 1; i <= m; i++) T[i][0] = 0; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (j >= C[i]) { T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1); } else { T[i][j] = T[i - 1][j]; } } } minv = INFINITY; for (i = 1; i <= m; i++) { minv = min(minv, T[i][n]); } cout << minv << endl; }
#include <algorithm> #include <iostream> #define N 50001 #define INFINITY 2000000000 using namespace std; const int M = 21; void getTheNumberOfCoin(); int n, m, T[M][N], C[M], i, j, minv; int main() { cin >> n >> m; for (i = 1; i <= m; i++) cin >> C[i]; getTheNumberOfCoin(); return 0; } void getTheNumberOfCoin() { for (i = 1; i <= n; i++) T[0][i] = INFINITY; for (i = 1; i <= m; i++) T[i][0] = 0; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (j >= C[i]) { T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1); } else { T[i][j] = T[i - 1][j]; } } } minv = INFINITY; for (i = 1; i <= m; i++) { minv = min(minv, T[i][n]); } cout << minv << endl; }
replace
2
3
2
3
0
p02314
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n, m; cin >> n >> m; vector<int> v; rep(i, m) { int a; cin >> a; v.pb(a); } int dp[10001]; fill_n(dp, 10001, INF); dp[0] = 0; rep(i, n + 1) { rep(j, v.size()) { if (i - v[j] >= 0) dp[i] = min(dp[i - v[j]] + 1, dp[i]); } } FIN(dp[n]); return 0; }
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n, m; cin >> n >> m; vector<int> v; rep(i, m) { int a; cin >> a; v.pb(a); } int dp[n + 1]; fill_n(dp, n + 1, INF); dp[0] = 0; rep(i, n + 1) { rep(j, v.size()) { if (i - v[j] >= 0) dp[i] = min(dp[i - v[j]] + 1, dp[i]); } } FIN(dp[n]); return 0; }
replace
37
39
37
39
0
p02314
C++
Runtime Error
#include <iostream> using namespace std; int main() { int ws[23], sum, num, dp[10003]; cin >> sum >> num; for (int i = 0; i < num; i++) cin >> ws[i]; for (int i = 0; i <= sum; i++) dp[i] = i; for (int i = 1; i < num; i++) { for (int j = ws[i]; j <= sum; j++) dp[j] = min(dp[j], dp[j - ws[i]] + 1); } cout << dp[sum] << endl; }
#include <iostream> using namespace std; int main() { int ws[23], sum, num, dp[50003]; cin >> sum >> num; for (int i = 0; i < num; i++) cin >> ws[i]; for (int i = 0; i <= sum; i++) dp[i] = i; for (int i = 1; i < num; i++) { for (int j = ws[i]; j <= sum; j++) dp[j] = min(dp[j], dp[j - ws[i]] + 1); } cout << dp[sum] << endl; }
replace
3
4
3
4
0
p02314
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, m; int c[20]; int lp[50005] = {}; int solve(int x) { if (lp[x] != 0) return lp[x]; int t = INT_MAX; for (int i = 1; i <= x / 2; i++) t = min(t, solve(i) + solve(x - i)); lp[x] = t; return t; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> c[i]; lp[c[i]] = 1; } cout << solve(n) << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; int c[20]; int lp[50005] = {}; int solve(int x) { if (lp[x] != 0) return lp[x]; int t = INT_MAX; for (int i = 0; i < m; i++) if (x > c[i]) t = min(t, solve(x - c[i]) + 1); lp[x] = t; return t; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> c[i]; lp[c[i]] = 1; } cout << solve(n) << endl; }
replace
11
13
11
14
TLE
p02314
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 50000; const int INF = 114514; int main() { int N, M; int coin[21]; int dp[MAXN + 1]; memset(dp, INF, sizeof(dp)); dp[0] = 0; scanf("%d%d", &N, &M); for (int i = 0; i < M; i++) scanf("%d", &coin[i]); for (int i = 0; i <= M; i++) { for (int j = coin[i]; j <= N; j++) { dp[j] = min(dp[j], dp[j - coin[i]] + 1); } } printf("%d\n", dp[N]); return (0); }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 50000; const int INF = 114514; int main() { int N, M; int coin[21]; int dp[MAXN + 1]; memset(dp, INF, sizeof(dp)); dp[0] = 0; scanf("%d%d", &N, &M); for (int i = 0; i < M; i++) scanf("%d", &coin[i]); for (int i = 0; i < M; i++) { for (int j = coin[i]; j <= N; j++) { dp[j] = min(dp[j], dp[j - coin[i]] + 1); } } printf("%d\n", dp[N]); return (0); }
replace
21
22
21
22
0
p02314
C++
Runtime Error
#include <bits/stdc++.h> #define F first #define S second #define mp make_pair // everything go according to my plan #define pb push_back #define sz(a) (int)(a.size()) #define vec vector // shimkenttin kyzdary, dzyn, dzyn, dzyn... #define y1 Y_U_NO_y1 #define left Y_U_NO_left #define right Y_U_NO_right using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const int Mod = (int)1e9 + 7; const int MX = 1073741822; const ll MXLL = 4e18; const int Sz = 1110111; // a pinch of soul inline void Read_rap() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } inline void randomizer3000() { unsigned int seed; asm("rdtsc" : "=A"(seed)); srand(seed); } void files(string problem) { if (fopen((problem + ".in").c_str(), "r")) { freopen((problem + ".in").c_str(), "r", stdin); freopen((problem + ".out").c_str(), "w", stdout); } } void localInput(string in = "s") { if (fopen(in.c_str(), "r")) freopen(in.c_str(), "r", stdin); else cerr << "Input file not found" << endl; } const int N = 5e4 + 1; int dp[N]; int n, m; int main() { localInput(); Read_rap(); cin >> n >> m; fill(dp + 1, dp + n + 1, MX); for (int i = 1; i <= m; i++) { int x; cin >> x; for (int j = 0; j + x <= n; j++) dp[j + x] = min(dp[j + x], dp[j] + 1); } cout << dp[n]; cout << endl; return 0; } // Coded by Z..
#include <bits/stdc++.h> #define F first #define S second #define mp make_pair // everything go according to my plan #define pb push_back #define sz(a) (int)(a.size()) #define vec vector // shimkenttin kyzdary, dzyn, dzyn, dzyn... #define y1 Y_U_NO_y1 #define left Y_U_NO_left #define right Y_U_NO_right using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const int Mod = (int)1e9 + 7; const int MX = 1073741822; const ll MXLL = 4e18; const int Sz = 1110111; // a pinch of soul inline void Read_rap() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } inline void randomizer3000() { unsigned int seed; asm("rdtsc" : "=A"(seed)); srand(seed); } void files(string problem) { if (fopen((problem + ".in").c_str(), "r")) { freopen((problem + ".in").c_str(), "r", stdin); freopen((problem + ".out").c_str(), "w", stdout); } } void localInput(string in = "s") { if (fopen(in.c_str(), "r")) freopen(in.c_str(), "r", stdin); else cerr << "Input file not found" << endl; } const int N = 5e4 + 1; int dp[N]; int n, m; int main() { cin >> n >> m; fill(dp + 1, dp + n + 1, MX); for (int i = 1; i <= m; i++) { int x; cin >> x; for (int j = 0; j + x <= n; j++) dp[j + x] = min(dp[j + x], dp[j] + 1); } cout << dp[n]; cout << endl; return 0; } // Coded by Z..
delete
54
56
54
54
0
Input file not found
p02314
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++) #define INF INT_MAX / 2 - 1 #define PI 2 * acos(0.0) #define ll long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { int n, m; cin >> n >> m; int c[20]; REP(i, m) cin >> c[i]; int dp[10001]; fill_n(dp, 10001, INF); dp[0] = 0; REP(i, m) FOR(j, c[i], n + 1) dp[j] = min(dp[j], dp[j - c[i]] + 1); cout << dp[n] << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++) #define INF INT_MAX / 2 - 1 #define PI 2 * acos(0.0) #define ll long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { int n, m; cin >> n >> m; int c[20]; REP(i, m) cin >> c[i]; int dp[50001]; fill_n(dp, 50001, INF); dp[0] = 0; REP(i, m) FOR(j, c[i], n + 1) dp[j] = min(dp[j], dp[j - c[i]] + 1); cout << dp[n] << endl; return 0; }
replace
32
34
32
34
0
p02314
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll, ll> #define FOR(I, A, B) for (ll I = (A); I < (B); ++I) #define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I) const ll INF = 1e18 + 7; const ll MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, m; cin >> n >> m; vector<ll> c(m); FOR(i, 0, m) cin >> c[i]; vector<ll> dp(n + 1); FOR(i, 0, n + 1) dp[i] = i; FOR(i, 0, m) { FORR(j, 0, n + 1) { if (j + c[j] <= n) dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1); } } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll, ll> #define FOR(I, A, B) for (ll I = (A); I < (B); ++I) #define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I) const ll INF = 1e18 + 7; const ll MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, m; cin >> n >> m; vector<ll> c(m); FOR(i, 0, m) cin >> c[i]; vector<ll> dp(n + 1); FOR(i, 0, n + 1) dp[i] = i; FOR(i, 0, m) { FOR(j, 0, n + 1) { if (j + c[i] <= n) dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1); } } cout << dp[n] << endl; return 0; }
replace
19
21
19
21
0
p02314
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stdlib.h> #include <string> #include <time.h> #include <unordered_map> #include <vector> #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define int long long using namespace std; typedef long long ll; typedef long double ld; const int inf = 999999999999999999; using namespace std; int n, m, a[20], dp[60001]; signed main() { cin >> n >> m; for (int i = 0; i < m; i++) cin >> a[i]; fill(dp, dp + 60001, inf); dp[0] = 0; for (int i = 0; i < m; i++) { for (int j = 0; j <= n; j++) { int z = 1; while (a[i] * z + j <= n) { dp[j + a[i] * z] = min(dp[j + a[i] * z], dp[j] + z); z++; } } } cout << dp[n] << endl; getchar(); getchar(); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stdlib.h> #include <string> #include <time.h> #include <unordered_map> #include <vector> #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define int long long using namespace std; typedef long long ll; typedef long double ld; const int inf = 999999999999999999; using namespace std; int n, m, a[20], dp[60001]; signed main() { cin >> n >> m; for (int i = 0; i < m; i++) cin >> a[i]; fill(dp, dp + 60001, inf); dp[0] = 0; for (int i = 0; i < m; i++) { for (int j = 0; j <= n; j++) { dp[j + a[i]] = min(dp[j + a[i]], dp[j] + 1); } } cout << dp[n] << endl; getchar(); getchar(); }
replace
33
38
33
34
TLE
p02314
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define rep(i, a, b) for (int(i) = (a); i < (b); i++) #define INF 1000000000 #define MAX_N 100005 using namespace std; int main() { int n, m; int c[21], dp[21][50001]; cin >> n >> m; rep(i, 0, m) cin >> c[i]; rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF; rep(i, 0, m + 1) dp[i][0] = 0; rep(i, 0, m + 1) { rep(j, 0, n + 1) { if (j < c[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); } } /* rep(i,0,m+1){ rep(j,0,n+1){ cout<<dp[i][j]<<" "; } cout<<endl; } */ cout << dp[m][n] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define rep(i, a, b) for (int(i) = (a); i < (b); i++) #define INF 1000000000 #define MAX_N 100005 using namespace std; int main() { int n, m; int c[21], dp[21][50001]; cin >> n >> m; rep(i, 0, m) cin >> c[i]; rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF; rep(i, 0, m + 1) dp[i][0] = 0; rep(i, 0, m) { rep(j, 0, n + 1) { if (j < c[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1); } } /* rep(i,0,m+1){ rep(j,0,n+1){ cout<<dp[i][j]<<" "; } cout<<endl; } */ cout << dp[m][n] << endl; return 0; }
replace
24
25
24
25
0
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int INF = 100000000; int n, m; cin >> n >> m; int dp[10010]; rep(i, 10010) dp[i] = INF; dp[0] = 0; int c[m]; rep(i, m) cin >> c[i]; rep(i, n + 1) { rep(j, m) { int tmp = INF; if (i - c[j] >= 0) tmp = dp[i - c[j]] + 1; dp[i] = min(tmp, dp[i]); } } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int INF = 100000000; int n, m; cin >> n >> m; int dp[50010]; rep(i, 50010) dp[i] = INF; dp[0] = 0; int c[m]; rep(i, m) cin >> c[i]; rep(i, n + 1) { rep(j, m) { int tmp = INF; if (i - c[j] >= 0) tmp = dp[i - c[j]] + 1; dp[i] = min(tmp, dp[i]); } } cout << dp[n] << endl; return 0; }
replace
9
11
9
11
0
p02314
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #define INF (1 << 30) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<long long> dp(n, INF); vector<int> c(m, 0); for (int i = 0; i < m; i++) { cin >> c[i]; } dp[0] = 0; for (int i = 1; i <= n; i++) { for (auto val : c) { if (i - val < 0) continue; dp[i] = min(dp[i], dp[i - val] + 1); } } cout << dp[n] << endl; }
#include <algorithm> #include <iostream> #include <vector> #define INF (1 << 30) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<long long> dp(n + 1, INF); vector<int> c(m, 0); for (int i = 0; i < m; i++) { cin >> c[i]; } dp[0] = 0; for (int i = 1; i <= n; i++) { for (auto val : c) { if (i - val < 0) continue; dp[i] = min(dp[i], dp[i - val] + 1); } } cout << dp[n] << endl; }
replace
10
11
10
11
-6
munmap_chunk(): invalid pointer
p02314
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int dp[21][50001]; vector<int> c; //??\??? cin >> n >> m; for (int i = 0; i < m; i++) { int tmp; cin >> tmp; c.push_back(tmp); } sort(c.begin(), c.end()); // dp????????? memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) dp[0][i] = i; //???????¨?????????§?§£??? for (int i = 1; i < m; i++) { for (int j = 0; j <= n; j++) { int min_coins = dp[i - 1][j]; // ????°???????????????° int Cj_num = 1; // Cj????????????????????° while (j - c[i] * Cj_num >= 0) { if (min_coins > dp[i - 1][j - c[i] * Cj_num] + Cj_num) { min_coins = dp[i - 1][j - c[i] * Cj_num] + Cj_num; } Cj_num++; } dp[i][j] = min_coins; } } cout << dp[m - 1][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int dp[21][50001]; vector<int> c; //??\??? cin >> n >> m; for (int i = 0; i < m; i++) { int tmp; cin >> tmp; c.push_back(tmp); } sort(c.begin(), c.end()); // dp????????? memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) dp[0][i] = i; //???????¨?????????§?§£??? for (int i = 1; i < m; i++) { for (int j = 0; j <= n; j++) { if (j - c[i] >= 0) dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1); else dp[i][j] = dp[i - 1][j]; } } cout << dp[m - 1][n] << endl; return 0; }
replace
25
34
25
29
TLE
p02314
C++
Time Limit Exceeded
#include <iostream> #define INF 1e+9 using namespace std; int main() { int n, m; int gaku[20]; int dp[21][50001]; cin >> n >> m; for (int i = m - 1; i >= 0; i--) cin >> gaku[i]; for (int i = 0; i <= 50000; i++) { for (int j = 0; j <= 20; j++) dp[j][i] = INF; } dp[0][0] = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= j / gaku[i - 1]; k++) { dp[i][j] = min(dp[i][j], dp[i - 1][j - k * gaku[i - 1]] + k); } } } cout << dp[m][n] << endl; return 0; }
#include <iostream> #define INF 1e+9 using namespace std; int main() { int n, m; int gaku[20]; int dp[21][50001]; cin >> n >> m; for (int i = m - 1; i >= 0; i--) cin >> gaku[i]; for (int i = 0; i <= 50000; i++) { for (int j = 0; j <= 20; j++) dp[j][i] = INF; } dp[0][0] = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { if (j - gaku[i - 1] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = min(dp[i - 1][j], dp[i][j - gaku[i - 1]] + 1); } } cout << dp[m][n] << endl; return 0; }
replace
18
21
18
22
TLE