結果
| 問題 |
No.1008 Bench Craftsman
|
| コンテスト | |
| ユーザー |
koi_kotya
|
| 提出日時 | 2020-03-06 23:48:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 210 ms / 2,000 ms |
| コード長 | 2,369 bytes |
| コンパイル時間 | 1,770 ms |
| コンパイル使用メモリ | 174,636 KB |
| 実行使用メモリ | 8,592 KB |
| 最終ジャッジ日時 | 2024-10-14 10:02:32 |
| 合計ジャッジ時間 | 6,449 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}
int main() {
int n,m;
cin >> n >> m;
vector<ll> a(n),w(m),sum(n,0);
vector<int> x(m);
for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
for (int i = 0;i < m;++i) scanf("%d%lld",&x[i],&w[i]),x[i]--;
for (int i = 0;i < m;++i) sum[x[i]] += w[i];
for (int i = 0;i < n;++i) if (a[i] <= sum[i]) {
cout << -1 << endl;
return 0;
}
ll s = 0;
for (int i = 0;i < m;++i) s += w[i];
bool f = true;
for (int i = 0;i < n;++i) if (a[i] <= s) {f = false;break;}
if (f) {
cout << 0 << endl;
return 0;
}
ll mn = 0,mx = 1e9;
while (mx-mn > 1) {
int mid = (mn+mx)/2;
vector<ll> sum1(n,0),sum2(n,0),sum3(n,0);
for (int i = 0;i < m;++i) {
int c = w[i]/mid;
sum1[max(0,x[i]-c)] += w[i];
if (x[i]+c+1 < n) sum1[x[i]+c+1] -= w[i];
if (x[i]+1 < n) sum2[x[i]+1] += mid;
if (x[i]+1+c < n) sum2[x[i]+c+1] -= mid;
if (x[i]-1 >= 0) sum3[x[i]-1] += mid;
if (x[i]-1-c >= 0) sum3[x[i]-1-c] -= mid;
}
for (int i = 1;i < n;++i) sum1[i] += sum1[i-1];
for (int i = 1;i < n;++i) sum2[i] += sum2[i-1];
for (int i = n-1;i > 0;--i) sum3[i-1] += sum3[i];
for (int i = 0;i < m;++i) {
int c = w[i]/mid;
if (x[i]+1+c < n) sum2[x[i]+c+1] -= (ll)c*mid;
if (x[i]-c-1 >= 0) sum3[x[i]-c-1] -= (ll)c*mid;
}
for (int i = 1;i < n;++i) sum2[i] += sum2[i-1];
for (int i = n-1;i > 0;--i) sum3[i-1] += sum3[i];
bool ok = true;
for (int i = 0;i < n;++i) if (sum1[i]-sum2[i]-sum3[i] >= a[i]) {
ok = false;
break;
}
(ok ? mx : mn) = mid;
}
cout << mx << endl;
}
koi_kotya