結果
| 問題 |
No.2546 Many Arithmetic Sequences
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-24 21:42:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 407 ms / 2,000 ms |
| コード長 | 2,738 bytes |
| コンパイル時間 | 1,964 ms |
| コンパイル使用メモリ | 190,284 KB |
| 実行使用メモリ | 37,864 KB |
| 最終ジャッジ日時 | 2024-09-26 11:16:04 |
| 合計ジャッジ時間 | 9,954 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
main.cpp: In function 'std::vector<long long int> slv2(std::vector<std::pair<long long int, long long int> >, long long int)':
main.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
70 | for(auto [a,d]:v)
| ^
main.cpp:75:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
75 | for(auto [k,b]:l)
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define app push_back
#ifndef LOCAL
#define endl '\n'
#endif // LOCAL
typedef long long ll;
struct Line {
ll k, b;
Line() : k(), b() {}
Line (ll _k, ll _b) : k(_k), b(_b) {}
ll getVal(ll x) {
return k * x + b;
}
};
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
struct Hull {
vector<Line> lines;
vector<ll> borders;
Hull() : lines(), borders() {}
void addLine(Line L) {
while(!lines.empty()) {
if (lines.back().getVal(borders.back()) >= L.getVal(borders.back())) {
lines.pop_back();
borders.pop_back();
} else break;
}
if (lines.empty()) {
lines.push_back(L);
borders.push_back(0LL); //leftmost query
return;
}
if (lines.back().k <= L.k) return;
ll x = div_up(L.b - lines.back().b, lines.back().k - L.k); //must work for negative!
lines.push_back(L);
borders.push_back(x);
}
ll getMinVal(ll x) {
int pos = upper_bound(borders.begin(), borders.end(), x) - borders.begin();
if (pos == 0) throw;
pos--;
return lines[pos].getVal(x);
}
};
Hull c;
vector<int> slv1(vector<pair<int,int> > a,int m)
{
set<pair<int,int> > d;
vector<int> answ={0};
if(a.empty()) {for(int i=0;i<m;++i) answ.push_back(-1e18); return answ;}
for(int i=0;i<a.size();++i) {d.insert({a[i].first,i});}
for(int i=0;i<m;++i)
{
auto o=*--d.end();d.erase(o);
answ.push_back(answ.back()+o.first);
o.first+=a[o.second].second;d.insert(o);
}
return answ;
}
vector<int> slv2(vector<pair<int,int> > v,int m)
{
if(v.empty()) {vector<int> answ;for(int i=0;i<=m;++i) answ.push_back((i!=0)*(-1e18)); return answ;}
vector<pair<int,int> > l;
for(auto [a,d]:v)
{
l.push_back({-d,-2*a+d});
}
sort(l.begin(),l.end());reverse(l.begin(),l.end());
for(auto [k,b]:l)
{
c.addLine(Line(k,b));
}
vector<int> res;
for(int i=0;i<=m;++i)
{
int ans=-c.getMinVal(i);
ans*=i;assert(ans%2==0);ans/=2;
res.push_back(ans);
}
return res;
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;cin>>n>>m;
vector<pair<int,int> > v1,v2;
for(int i=0;i<n;++i) {int x,y;cin>>x>>y;if(y<=0) {v1.push_back({x,y});} else {v2.push_back({x,y});}}
vector<int> res1=slv1(v1,m);vector<int> res2=slv2(v2,m);
//for(int x:res1) cout<<x<<' '; cout<<endl;
int answ=(-1e18);
for(int i=0;i<=m;++i) {answ=max(answ,res1[i]+res2[m-i]);}
cout<<answ;
return 0;
}