結果
| 問題 |
No.1687 What the Heck?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-24 21:48:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,161 bytes |
| コンパイル時間 | 2,444 ms |
| コンパイル使用メモリ | 203,088 KB |
| 最終ジャッジ日時 | 2025-01-24 17:05:55 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 WA * 10 |
ソースコード
/**
* author: ivanzuki
* created: Fri Sep 24 2021
**/
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template<typename T> void dout(string name, int idx, T arg) {
cerr << name << " = " << to_string(arg);
}
template<typename T1, typename... T2> void dout(string names, int idx, T1 arg, T2... args) {
cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", ";
dout(names.substr(names.find(',') + 2), idx + 1, args...);
}
#ifdef LOCAL
#define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl;
#else
#define debug(...) 42
#endif
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> p(n);
set<int> s;
for (int i = 0; i < n; i++) {
cin >> p[i];
--p[i];
s.insert(i);
}
if (n == 2 && p[0] == 0) {
cout << 0 << '\n';
return 0;
}
vector<int> ans(n, -1);
for (int i = n - 1; i >= 0; i--) {
auto it = s.upper_bound(p[i]);
if (it != s.end()) {
ans[i] = *it;
s.erase(it);
}
}
for (int i = n - 1; i >= 0; i--) {
if (ans[i] == -1) {
auto it = s.lower_bound(p[i]);
if (it == s.end()) {
--it;
}
ans[i] = *it;
}
}
debug(ans);
long long res = 0;
for (int i = 0; i < n; i++) {
if (ans[i] > p[i]) {
res += i + 1;
} else if (ans[i] < p[i]) {
res -= i + 1;
}
}
cout << res << '\n';
return 0;
}