結果

問題 No.2546 Many Arithmetic Sequences
ユーザー 👑 kmjpkmjp
提出日時 2023-11-26 23:34:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 2,268 bytes
コンパイル時間 3,028 ms
コンパイル使用メモリ 219,844 KB
実行使用メモリ 17,136 KB
最終ジャッジ日時 2023-11-26 23:35:05
合計ジャッジ時間 8,246 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,648 KB
testcase_01 AC 2 ms
7,648 KB
testcase_02 AC 3 ms
7,648 KB
testcase_03 AC 60 ms
8,388 KB
testcase_04 AC 113 ms
13,688 KB
testcase_05 AC 91 ms
10,864 KB
testcase_06 AC 21 ms
8,036 KB
testcase_07 AC 45 ms
9,072 KB
testcase_08 AC 13 ms
7,904 KB
testcase_09 AC 44 ms
11,060 KB
testcase_10 AC 79 ms
15,616 KB
testcase_11 AC 87 ms
17,092 KB
testcase_12 AC 36 ms
9,844 KB
testcase_13 AC 82 ms
10,696 KB
testcase_14 AC 74 ms
14,484 KB
testcase_15 AC 22 ms
8,904 KB
testcase_16 AC 70 ms
14,936 KB
testcase_17 AC 108 ms
10,836 KB
testcase_18 AC 56 ms
10,168 KB
testcase_19 AC 65 ms
10,064 KB
testcase_20 AC 41 ms
10,132 KB
testcase_21 AC 49 ms
8,164 KB
testcase_22 AC 77 ms
10,452 KB
testcase_23 AC 144 ms
17,136 KB
testcase_24 AC 144 ms
17,136 KB
testcase_25 AC 171 ms
17,132 KB
testcase_26 AC 166 ms
17,128 KB
testcase_27 AC 147 ms
17,128 KB
testcase_28 AC 133 ms
15,100 KB
testcase_29 AC 133 ms
15,128 KB
testcase_30 AC 132 ms
15,156 KB
testcase_31 AC 135 ms
15,028 KB
testcase_32 AC 133 ms
15,272 KB
testcase_33 AC 2 ms
7,648 KB
testcase_34 AC 129 ms
15,248 KB
testcase_35 AC 3 ms
7,648 KB
testcase_36 AC 101 ms
14,620 KB
testcase_37 AC 3 ms
7,648 KB
testcase_38 AC 3 ms
7,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
ll A[303030],D[303030];
int cur[303030];

template<typename V> struct ConvexHull {
	deque<pair<V,V>> Q;
	V calc(pair<V,V> p, V x) {
		return p.first*x+p.second;
	}
	int dodo(pair<V,V> A,pair<V,V> B, pair<V,V> C) {
		return ((__int128)(B.second-C.second)*(B.first-A.first)<=(__int128)(A.second-B.second)*(C.first-B.first));
	}
	void add(V a, V b) { // add ax+b
		if(Q.size() && Q.back().first==a) {
			//aが同じ場合
			if(b<=Q.back().second) return; //maxの場合
			Q.pop_back();
		}
		Q.push_back({a,b});
		int v;
		while((v=Q.size())>=3 && dodo(Q[v-3],Q[v-2],Q[v-1]))
			Q[v-2]=Q[v-1], Q.pop_back();
	}
	void add(vector<pair<V,V>> v) {
		sort(v.begin(),v.end());
		for(auto r=v.begin();r!=v.end();r++) add(r->first,r->second);
	}
	
	
	V query(V x) {
		int L=-1,R=Q.size()-1;
		while(R-L>1) {
			int M=(L+R)/2;
			(0^((calc(Q[M],x)<=calc(Q[M+1],x)))?L:R)=M;
		}
		return calc(Q[R],x);
	}
};
ConvexHull<ll> ch;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	vector<pair<ll,ll>> C;
	C.push_back({-(3LL<<30),-(1LL<<40)});
	priority_queue<pair<ll,int>> Q;
	FOR(i,N) {
		cin>>A[i]>>D[i];
		if(D[i]>=0) {
			C.push_back({D[i],2*A[i]-D[i]});
		}
		else {
			Q.push({A[i],i});
		}
	}
	ch.add(C);
	A[N]=-1LL<<42;
	Q.push({A[N],N});
	ll ret=M*ch.query(M)/2;
	ll sum=0;
	for(i=M-1;i>=0;i--) {
		auto p=Q.top();
		Q.pop();
		sum+=p.first;
		x=p.second;
		A[x]+=D[x];
		Q.push({A[x],x});
		ret=max(ret,sum+i*ch.query(i)/2);
		
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0