Skip to content

Instantly share code, notes, and snippets.

@ArthurLoboLobo
Created April 1, 2024 19:07
Show Gist options
  • Save ArthurLoboLobo/b301dbdb7bcb95f842642083848b6004 to your computer and use it in GitHub Desktop.
Save ArthurLoboLobo/b301dbdb7bcb95f842642083848b6004 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1010;
int n, m, q, mark[maxn], matr[maxn], matl[maxn], p[maxn], t[maxn];
int ansq[(int) 1e6+10];
vector<int> g[maxn];
bool dfs(int u, int idm) {
mark[u] = idm;
for(auto v : g[u]) {
if(matr[v] == 0 or (mark[matr[v]] != idm && dfs(matr[v],idm))) {
matl[u] = v;
matr[v] = u;
return true;
}
}
return false;
}
int x;
void solve() {
cin >> n >> m;
vector<pair<pair<int,int>,vector<int>>> ord;
for(int i = 1; i <= n; i++) {
int t1,p1;
cin >> t1 >> p1;
int k1;
cin >> k1;
vector<int> apts1;
while(k1--) {
int x; cin >> x;
apts1.pb(x);
}
ord.pb(mp(mp(p1,t1),apts1));
}
sort(all(ord),greater<pair<pair<int,int>,vector<int>>>());
for(int i = 1; i <= n; i++) {
p[i] = ord[i-1].fr.fr;
t[i] = ord[i-1].fr.sc;
g[i] = ord[i-1].sc;
}
vector<pair<pair<dbl,int>,pair<int,int>>> swaps;
// T/P id T P
// (pi+pj)/(ti+tj) 0 i j
cin >> q;
for(int i = 1; i <= q; i++) {
int T,P;
cin >> T >> P;
swaps.pb(mp(mp(((dbl) T)/((dbl) P),i),mp(T,P)));
}
// comeca com o menor T/P, entao quem comeca ganhando é o maior p
for(int i = 1; i <= n; i++) {
for(int j = i+1; j <= n; j++) {
if(t[i] >= t[j]) continue;
// ti*T + pi*P < tj*T + pj*P
// T(ti-tj) < P(pj-pi)
// T/P < (pj-pi)/(ti-tj)
swaps.pb(mp(mp(((dbl) p[j]-p[i])/((dbl) t[i]-t[j]),0),mp(i,j)));
}
}
int sumt = 0;
int sump = 0;
int curid = 1;
for(int i = 1; i <= n; i++) {
if(dfs(i,curid)) {
sumt+= t[i];
sump+= p[i];
curid++;
}
}
sort(all(swaps));
for(auto X : swaps) {
if(X.fr.sc == 0) {
int i = X.sc.fr;
int j = X.sc.sc;
// j melhor que i daqui pra frente
if(matl[i] == 0 or matl[j] != 0) continue;
int mati = matl[i];
matl[i] = 0;
matr[mati] = 0;
curid++;
if(dfs(j,curid)) {
sumt+= -t[i]+t[j];
sump+= -p[i]+p[j];
curid++;
}
else {
matl[i] = mati;
matr[mati] = i;
}
}
else {
int id = X.fr.sc;
int T = X.sc.fr;
int P = X.sc.sc;
ansq[id] = T*sumt + P*sump;
}
}
for(int i = 1; i <= q; i++) {
cout << ansq[i] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment