QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#384543 | #6532. Trading | DDT | TL | 0ms | 4352kb | C++14 | 1.1kb | 2024-04-10 01:05:20 | 2024-04-10 01:05:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
#define endl '\n'
#define x first
#define y second
typedef long long ll;
typedef pair<int,int> PII;
void solve()
{
PII a[100010];
int n,cnt=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
cnt+=a[i].y;
}
sort(a+1,a+n+1);
int cnt1,cnt2;
if(cnt&1)cnt1=(cnt-1)/2;
else cnt1=cnt/=2;
cnt2=cnt1;
ll cb=0,xs=0;
int l=1,r=n;
// while(l<r)
// {
// if(a[l].x==a[r].x)break;
// cb+=a[l].x*min(a[l].y,cnt1);
// cnt1-=min(a[l].y,cnt1);
// if(cnt1>0)l++;
//
// xs+=a[r].x*min(a[r].y,cnt2);
// cnt2-=min(a[r].y,cnt2);
// if(cnt2>0)r--;
//
// if(l>n-r+1)break;
// }
for(int i=1,j=n;i<=n,j>=1;i++,j--)
{
cb+=a[i].x*min(a[i].y,cnt1);
cnt1-=min(a[i].y,cnt1);
xs+=a[j].x*min(a[j].y,cnt2);
cnt2-=min(a[j].y,cnt2);
if(cnt1<=0)break;
}
cout<<xs-cb<<endl;
}
/*
10 2
20 4
25
30 7
50 1
*/
int main()
{
ios::sync_with_stdio(false);
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
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: 0ms
memory: 4352kb
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: -100
Time Limit Exceeded
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:
-744919867 -2400664259 -1134720638 3084388480 -1601339592 -9934515505 1385928560 3938969176 1372696906 -613732009 -4133797096 142232121 -4685763516 2935792391 0 -22761517 2255434397 -2275224070 3845990352 1846443908 5397242676 2747831516 -774885684 0 -1763662317 -4037589206 1629150693 6701464159 244...