Last active
March 13, 2020 01:39
-
-
Save Tdrj2716/c0c66f51c54803bf35bd885e1e318686 to your computer and use it in GitHub Desktop.
二分探索を使う問題(https://atcoder.jp/contests/abc077/tasks/arc084_a , https://atcoder.jp/contests/abc143/tasks/abc143_d )のソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
vector<int> a, b, c; | |
int n, inp; | |
long long int ans = 0; | |
cin >> n; | |
for(int i = 0; i < n; i++){ | |
cin >> inp; | |
a.push_back(inp); | |
} | |
sort(a.begin(), a.end()); | |
for(int i = 0; i < n; i++){ | |
cin >> inp; | |
b.push_back(inp); | |
} | |
sort(b.begin(), b.end()); | |
for(int i = 0; i < n; i++){ | |
cin >> inp; | |
c.push_back(inp); | |
} | |
sort(c.begin(), c.end()); | |
for(int i = 0; i < n; i++){ | |
long long int ai = distance(a.begin(), lower_bound(a.begin(), a.end(), b[i])); | |
long long int ci = distance(upper_bound(c.begin(), c.end(), b[i]), c.end()); | |
ans += ai * ci; | |
} | |
cout << ans << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
int n, li, ans = 0; | |
cin >> n; | |
vector<int> l; | |
for(int i = 0; i < n; i++){ | |
cin >> li; | |
l.push_back(li); | |
} | |
sort(l.begin(), l.end()); | |
for(int m1 = n - 1; m1 >= 2; m1--){ | |
for(int m2 = m1 - 1; m2 >= 1; m2--){ | |
ans += distance(upper_bound(l.begin(), l.begin() + m2, l[m1] - l[m2]), l.begin() + m2); | |
} | |
} | |
cout << ans << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment