Codeforces Global Round 6

23887 단어 codeforces
C. Diverse Matrix
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Let a be a matrix of size r×c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 1 to r, columns are numbered from 1 to c. We can construct an array b consisting of r+c integers as follows: for each i∈[1,r], let bi be the greatest common divisor of integers in the i-th row, and for each j∈[1,c] let br+j be the greatest common divisor of integers in the j-th column.
We call the matrix diverse if all r+c numbers bk (k∈[1,r+c]) are pairwise distinct.
The magnitude of a matrix equals to the maximum of bk.
For example, suppose we have the following matrix:
(249144784) We construct the array b:
b1 is the greatest common divisor of 2, 9, and 7, that is 1; b2 is the greatest common divisor of 4, 144, and 84, that is 4; b3 is the greatest common divisor of 2 and 4, that is 2; b4 is the greatest common divisor of 9 and 144, that is 9; b5 is the greatest common divisor of 7 and 84, that is 7. So b=[1,4,2,9,7]. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to 9.
For a given r and c, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer 0.
Input The only line in the input contains two space separated integers r and c (1≤r,c≤500) — the number of rows and the number of columns of the matrix to be found.
Output If there is no solution, output a single integer 0.
Otherwise, output r rows. The i-th of them should contain c space-separated integers, the j-th of which is ai,j — the positive integer in the i-th row and j-th column of a diverse matrix minimizing the magnitude.
Furthermore, it must hold that 1≤ai,j≤109. It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude).
Examples inputCopy 2 2 outputCopy 4 12 2 9 inputCopy 1 1 outputCopy 0 Note In the first example, the GCDs of rows are b1=4 and b2=1, and the GCDs of columns are b3=2 and b4=3. All GCDs are pairwise distinct and the maximum of them is 4. Since the GCDs have to be distinct and at least 1, it is clear that there are no diverse matrices of size 2×2 with magnitude smaller than 4.
In the second example, no matter what a1,1 is, b1=b2 will always hold, so there are no diverse matrices.
#include
using namespace std;
int n,t,r,c;
int x[510],y[510];
int main()
{
    cin>>r>>c;
    if(r==1&&c==1){cout<<0<<endl;return 0;}
    if(r==1||c==1)
    {
        if(r==1)for(int i=1;i<=c;i++){cout<<i+1<<" ";cout<<endl;}
        else
        {
            for(int i=1;i<=r;i++)cout<<i+1<<endl;
        }
        return 0;
    }
    int num=1;
    for(int i=1;i<=r;i++){x[i]=num;num++;}
    for(int i=1;i<=c;i++){y[i]=num;num++;}
    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            cout<<x[i]*y[j]<<" ";
        }cout<<endl;
    }
    return 0;
}

D. Decreasing Debts
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
Let d(a,b)>0 and d(c,d)>0 such that a≠c or b≠d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 Let d(a,a)>0. We can set d(a,a) to 0. The total debt is defined as the sum of all debts:
Σd=∑a,bd(a,b) Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don’t have to minimise the number of non-zero debts, only the total debt.
Input The first line contains two space separated integers n (1≤n≤105) and m (0≤m≤3⋅105), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers ui, vi (1≤ui,vi≤n,ui≠vi), di (1≤di≤109), meaning that the person ui borrowed di burles from person vi.
Output On the first line print an integer m′ (0≤m′≤3⋅105), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m′ lines, i-th of which contains three space separated integers ui,vi,di, meaning that the person ui owes the person vi exactly di burles. The output must satisfy 1≤ui,vi≤n, ui≠vi and 0
For each pair i≠j, it should hold that ui≠uj or vi≠vj. In other words, each pair of people can be included at most once in the output.
Examples inputCopy 3 2 1 2 10 2 3 5 outputCopy 2 1 2 5 1 3 5 inputCopy 3 3 1 2 10 2 3 15 3 1 10 outputCopy 1 2 3 5 inputCopy 4 2 1 2 12 3 4 8 outputCopy 2 1 2 12 3 4 8 inputCopy 3 4 2 3 1 2 3 2 2 3 4 2 3 8 outputCopy 1 2 3 15 Note In the first example the optimal sequence of operations can be the following:
Perform an operation of the first type with a=1, b=2, c=2, d=3 and z=5. The resulting debts are: d(1,2)=5, d(2,2)=5, d(1,3)=5, all other debts are 0; Perform an operation of the second type with a=2. The resulting debts are: d(1,2)=5, d(1,3)=5, all other debts are 0. In the second example the optimal sequence of operations can be the following:
Perform an operation of the first type with a=1, b=2, c=3, d=1 and z=10. The resulting debts are: d(3,2)=10, d(2,3)=15, d(1,1)=10, all other debts are 0; Perform an operation of the first type with a=2, b=3, c=3, d=2 and z=10. The resulting debts are: d(2,2)=10, d(3,3)=10, d(2,3)=5, d(1,1)=10, all other debts are 0; Perform an operation of the second type with a=2. The resulting debts are: d(3,3)=10, d(2,3)=5, d(1,1)=10, all other debts are 0; Perform an operation of the second type with a=3. The resulting debts are: d(2,3)=5, d(1,1)=10, all other debts are 0; Perform an operation of the second type with a=1. The resulting debts are: d(2,3)=5, all other debts are 0.
#include
using namespace std;
int n,m,d;
map<int,long long>mp[100010];
long long s[100010];
queue<int>q1,q2;
int main()
{
    cin>>n>>m;
    int u,v,x;
    for(int i=1;i<=m;i++)
    {
        cin>>u>>v>>x;
        s[u]+=x;
        s[v]-=x;
    }
    for(int i=1;i<=n;i++)if(s[i]>0)q1.push(i);else if(s[i]<0)q2.push(i);
    while(!q1.empty())
    {
        int f=q1.front();q1.pop();
        int g=q2.front();q2.pop();
        long long mi=min(s[f],-s[g]);
        s[f]-=mi;s[g]+=mi;mp[f][g]+=mi;
        if(s[f]>0)q1.push(f);if(s[g]<0)q2.push(g);
    }
    int ans=0;
    for(int i=1;i<=n;i++)ans+=mp[i].size();
    cout<<ans<<endl;
    for(int i=1;i<=n;i++)
    for(auto j:mp[i])
    {
        cout<<i<<" "<<j.first<<" "<<j.second<<endl;
    }
    return 0;
}

좋은 웹페이지 즐겨찾기