Global round 6 D、E

D

Meaning of the questions:

Very simple, a map, liabilities, zoom relations, so that the simpler the better the relationship, embodied as a minimum the sum of the edge weights.

answer

Each person only concerned about how much money to borrow and lend out much.
We put all of borrowed money are allocated to borrow from people inside out, one by one points.
It can be.

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
 
typedef long long ll;
const int maxn = 200050;
 
int n,m;
ll sum[maxn];
queue<pair<ll,int> >P,Q;
vector<pair<ll,int> >G[maxn];
 
int main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v;ll d;
        scanf("%d%d%lld",&u,&v,&d);
        sum[u]-=d;
        sum[v]+=d;
    }
    for(int i=1;i<=n;i++){
        if(sum[i]==0)continue;
        if(sum[i]<0)P.push(make_pair(-sum[i],i));
        else Q.push(make_pair(sum[i],i));
    }
    m=0;
    while(!P.empty()){
        ll val=P.front().first;
        int u=P.front().second;
        P.pop();
        //cout<<u<<":"<<val<<endl;
        while(!Q.empty()){
            int v=Q.front().second;
            ll have=Q.front().first;
            //cout<<v<<" "<<have<<endl;
            if(have<val){
                G[u].push_back(make_pair(have,v));
                val-=have;
                Q.pop();
                m++;
            }
            else{
                Q.front().first-=val;
                G[u].push_back(make_pair(val,v));
                m++;
                if(Q.front().first==0)Q.pop();//cout<<Q.front().second<<endl;
                break;
            }
        }
    }
    cout<<m<<endl;
    for(int i=1;i<=n;i++){
        for(int j=0;j<G[i].size();j++){
            cout<<i<<" "<<G[i][j].second<<" "<<G[i][j].first<<endl;
        }
    }
}

E

Meaning of the questions:

need n n an article, each article needs a [ i ] a[i] one, get accomplished, once they reach a certain number of items you can send a an article.
This achievement required number > 0 & & < a [ i ] >0\&\&<a[i]

answer:

According to the conditions, that is, we will complete the achievements in the process required to reach, that is, each achievement will be to use. [Official website proof did not understand, come again and look]
statistics can be, remember that achievement will not exist the same conditions, the same conditions will be covered.
the answer is m a x ( a [ i ] c [ i ] , 0 ) \sum max(a[i]-c[i],0) , c [ i ] c[i] refers to the number of achievements will send a free i i

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
 
typedef long long ll;
const int maxn = 200050;
 
map<pair<int,int>,int>mp;
map<int,int>Mp;
int a[maxn];
 
int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    ll s=0;
    for(int i=1;i<=n;i++)s+=a[i];
    int m;cin>>m;
    ll h=0;
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        int id=mp[make_pair(x,y)];
        if(id){
            Mp[id]--;
            if(Mp[id]<a[id])h--;
            mp[make_pair(x,y)]=z;
            Mp[z]++;
            if(Mp[z]<=a[z])h++;
        }
        else{
            mp[make_pair(x,y)]=z;
            Mp[z]++;
            if(Mp[z]<=a[z])h++;
        }
        cout<<s-h<<endl;
    }
}
Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104029269