QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#111952 | #6532. Trading | __stick | AC ✓ | 108ms | 4096kb | C++20 | 1.7kb | 2023-06-09 11:00:40 | 2023-06-09 11:00:41 |
Judging History
answer
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
using namespace std;
template<typename T>
inline bool cmax(T&x,const T& y){return x<y?x=y,1:0;}
template<typename T>
inline bool cmin(T&x,const T& y){return y<x?x=y,1:0;}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef unsigned long long ull;
#define sz(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define em emplace
#define X first
#define Y second
const int mod=998244353;
inline void MOD(int&x){x-=mod,x+=x>>31&mod;}
inline void MOD(ll& x){x-=mod,x+=x>>63&mod;}
inline int add(int x,int y){MOD(x+=y);return x;}
inline int mul(int x,int y){return 1ll*x*y%mod;}
template<typename ... A>inline int mul(const int& x,const A&... p){return 1ll*x*mul(p...)%mod;}
inline ll ksm(ll a,ll p=mod-2){ll ans=1;for(;p;p>>=1,a=a*a%mod)if(p&1)ans=ans*a%mod;return ans;}
typedef long double LD;
const int MAXN=5e5+10;
inline void solve()
{
int n;cin>>n;
vector<pii> a(n+1);
for(int i=1;i<=n;i++)cin>>a[i].first>>a[i].second;
sort(a.begin()+1,a.end());
int l=1,r=n;
ll ans=0;
while(l<r)
{
if(a[l].second<a[r].second)
{
ans+=1ll*a[l].second*(a[r].first-a[l].first);
a[r].second-=a[l].second;l++;
}
else
{
ans+=1ll*a[r].second*(a[r].first-a[l].first);
a[l].second-=a[r].second;r--;
}
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cout<<fixed<<setprecision(10);
int T;cin>>T;while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3380kb
input:
2 4 10 2 30 7 20 4 50 1 2 1 100 1 1000
output:
100 0
result:
ok 2 number(s): "100 0"
Test #2:
score: 0
Accepted
time: 104ms
memory: 3392kb
input:
100000 8 567091 283679 875020 918237 314684 148083 456411 304598 766056 882388 135371 326501 578773 250140 221306 874117 5 126777 129517 846433 679825 649281 330021 427768 362636 390068 692169 5 657677 231119 941936 991342 901241 15133 660372 970476 698958 209343 10 478657 163635 752788 819629 82110...
output:
974212656325 422801098045 290923055490 905027520640 1029190811448 678507966352 198954424176 854342493784 14257598794 988991921252 588571689752 736448232231 1193610112068 190497179448 0 931985141715 607845823133 684919543290 764055201744 1066998333316 404829201204 908985930972 518916157132 0 18970544...
result:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 98ms
memory: 3372kb
input:
1000 943 72269 378475 977924 359650 767015 807123 588692 74765 415453 560758 928758 428092 806008 738436 208619 75109 42177 669012 962462 479504 590660 677629 539374 152571 248543 325297 781505 297957 16148 319989 551104 444452 227850 215480 851246 491844 513358 739037 788832 628842 60861 351413 302...
output:
114201255531485 53276337328736 92046111258743 103154270867625 105744931013248 32643398141462 88110706162215 47935111239811 15241903293912 111168188920159 40432059191865 31710483882560 76824850524735 5062330895031 699195293530 102704425622910 62567748918438 111989972317852 70973066348865 748113762268...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 108ms
memory: 4096kb
input:
10 3648 321905 608337 913130 232679 807842 182580 108666 826799 649762 754578 461310 612685 351739 113156 227668 993674 779322 394130 762123 235998 591912 252590 648451 525516 758596 858327 444142 632425 993345 476968 987323 970898 664888 739790 582325 596631 537216 606677 30160 316873 32104 109750 ...
output:
456752959290141 11993792944026063 11554678993093858 9453515389111085 8600545413208495 5664061450260769 2045423469498213 7668923751962719 2600263474973873 3635314464114077
result:
ok 10 numbers