QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#428896 | #6532. Trading | dnialh# | RE | 0ms | 0kb | C++20 | 2.2kb | 2024-06-01 22:53:53 | 2024-06-01 22:53:53 |
answer
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define sq(a) ((a)*(a))
#define MOO(i,a,b) for (int i=a; i<b; i++)
#define M00(i,a) for (int i=0; i<a; i++)
#define MOOd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define M00d(i,a) for (int i = (a)-1; i >= 0; i--)
#define per(i,a,b) for (int i = (b)-1; i >= a; i--)
#define rep(i,a,b) for (int i=a; i<b; i++)
#define FOR(i,a,b) for (int i=a; i<b; i++)
#define F0R(i,a) for (int i=0; i<a; i++)
#define ROF(i,a,b) for (int i = (b)-1; i >= a; i--)
#define R0F(i,a) for (int i = (a)-1; i >= 0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << endl, 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << endl;
#define _ << " _ " <<
#define int long long
template<class T> bool ckmin(T&a, T&b) { bool B = a > b; a = min(a,b); return B; }
template<class T> bool ckmax(T&a, T&b) { bool B = a < b; a = max(a,b); return B; }
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ld,ld> pld;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<vector<int>> vvi;
const ld PI = acos(-1.0);
const ld EPS = 1e-7;
const int MOD = 1e9+7;
// Really should be changed to a modular int class
int POW(int b, int e) {
int r = 1;
while(e) {
if(e % 2 == 1) {
r *= b;
r %= MOD;
}
e /= 2;
b *= b;
b %= MOD;
}
return r;
}
int INV(int a) {
return POW(a, MOD-2);
}
//Constants and Variables here
void solve() {
int n; cin >> n;
vector<pi> price(n);
M00(i, n) {
int a, b;
cin >> a >> b;
price[i] = mp(a,b);
}
sort(all(price));
int l = 0;
int r = n-1;
int money = 0;
while(l < r) {
int q = min(price[r].s, price[l].s);
money += (price[r].f - price[l].f) * q;
price[r].s -= q;
price[l].s -= q;
if(price[r].s == 0) r--;
if(price[l].s == 0) l--;
}
cout << money << endl;
}
int32_t main() { FAST
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int t; cin >> t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 4 10 2 30 7 20 4 50 1 2 1 100 1 1000
output:
320