Codeforces Round #178 (Div. 2)——A,B

A. Shaass and Oskols
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to nfrom top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire.

Codeforces Round #178 (Div. 2)——A,B_第1张图片

Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on thei-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away.

Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

Input

The first line of the input contains an integer n(1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an,(0 ≤ ai ≤ 100).

The third line contains an integer m(0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.

Output

On the i-th line of the output print the number of birds on the i-th wire.

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=100+10;
int cnt[maxn];
int main()
{
    int n;
    cin>>n;
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++)
        cin>>cnt[i];
    int m;
    cin>>m;
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        if(x!=1) cnt[x-1]+=y-1;
        if(x!=n) cnt[x+1]+=cnt[x]-y;
        cnt[x]=0;
    }
    for(int i=1;i<=n;i++)
        cout<<cnt[i]<<endl;
    return 0;
}

B. Shaass and Bookshelf
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Codeforces Round #178 (Div. 2)——A,B_第2张图片

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

Input

The first line of the input contains an integer n(1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100+10;
int w[maxn],t[maxn],dp[maxn*100];//dp表示总厚度为i时可以减小的最大厚度
int main()
{
    int n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t[i]>>w[i];
        sum+=t[i];
    }
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++)
        for(int j=sum;j>=t[i]+w[i];j--)
            dp[j]=max(dp[j],dp[j-w[i]-t[i]]+t[i]);
    cout<<sum-dp[sum]<<endl;
    return 0;
}


你可能感兴趣的:(Codeforces Round #178 (Div. 2)——A,B)