1. 程式人生 > >今日 日常訓練三題

今日 日常訓練三題

amp itl stand enume %d 排序 student com out

A. Gennady the Dentist time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Gennady is one of the best child dentists in Berland. Today n children got an appointment with him, they lined up in front of his office.

All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to n

in the order they go in the line. Every child is associated with the value of his cofidence pi. The children take turns one after another to come into the office; each time the child that is the first in the line goes to the doctor.

While Gennady treats the teeth of the i-th child, the child is crying with the volume of v

i. At that the confidence of the first child in the line is reduced by the amount of vi, the second one — by value vi - 1, and so on. The children in the queue after the vi-th child almost do not hear the crying, so their confidence remains unchanged.

If at any point in time the confidence of the j

-th child is less than zero, he begins to cry with the volume of dj and leaves the line, running towards the exit, without going to the doctor‘s office. At this the confidence of all the children after the j-th one in the line is reduced by the amount of dj.

All these events occur immediately one after the other in some order. Some cries may lead to other cries, causing a chain reaction. Once in the hallway it is quiet, the child, who is first in the line, goes into the doctor‘s office.

Help Gennady the Dentist to determine the numbers of kids, whose teeth he will cure. Print their numbers in the chronological order.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 4000) — the number of kids in the line.

Next n lines contain three integers each vi, di, pi (1 ≤ vi, di, pi ≤ 106) — the volume of the cry in the doctor‘s office, the volume of the cry in the hall and the confidence of the i-th child.

Output

In the first line print number k — the number of children whose teeth Gennady will cure.

In the second line print k integers — the numbers of the children who will make it to the end of the line in the increasing order.

Examples input Copy
5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
output Copy
2
1 3
input Copy
5
4 5 1
5 3 9
4 1 2
2 1 8
4 1 9
output Copy
4
1 2 4 5
Note

In the first example, Gennady first treats the teeth of the first child who will cry with volume 4. The confidences of the remaining children will get equal to  - 2, 1, 3, 1, respectively. Thus, the second child also cries at the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 0, 2, 0. Then the third child will go to the office, and cry with volume 5. The other children won‘t bear this, and with a loud cry they will run to the exit.

In the second sample, first the first child goes into the office, he will cry with volume 4. The confidence of the remaining children will be equal to 5,  - 1, 6, 8. Thus, the third child will cry with the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 5, 5, 7. After that, the second child goes to the office and cry with the volume of 5. The confidences of the remaining children will be equal to 0, 3. Then the fourth child will go into the office and cry with the volume of 2. Because of this the confidence of the fifth child will be 1, and he will go into the office last.

這個是一個簡單的模擬題,不過你需要認真讀題,不然就會想我這樣WA二十幾發

這個題目有一個點要註意,就是如果有一個人跑掉,那就要馬上處理跑掉的人對後面的影響,不然之後前面的人哭對後面的影響就會有問題。

然後就是模擬一下就好了

#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 5e3 + 10;
bool vis[maxn];
struct node
{
    ll v, d, p;
}exa[maxn];

int main()
{
    ll n, ans = 0;
    cin >> n;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++)
    {
        cin >> exa[i].v >> exa[i].d >> exa[i].p;
    }
    for (int i = 1; i <= n; i++)
    {
        if (exa[i].p < 0) continue;
        int sum = 0;
        int cnt = i;
        int v = exa[i].v;
        while(v&&cnt<=n)
        {
            cnt++;
            if (exa[cnt].p < 0) continue;
            exa[cnt].p -= v;
            v--;
        }
        for(int j=i+1;j<=n;j++)
        {
            if (exa[j].p >= 0)
            {
                exa[j].p -= sum;
                if(exa[j].p<0&&!vis[j])
                {
                    sum += exa[j].d;
                    vis[j] = 1;
                }
            }
            else if (vis[j] == 0)
            {
                sum += exa[j].d;
                vis[j] = 1;
            }
        }
        //printf("%lld %d i=%d\n", exa[i].p, sum, i);
    }
    for (int i = 1; i <= n; i++)
    {
        if (exa[i].p >= 0) ans++;
    }
    printf("%lld\n", ans);
    for (int i = 1; i <= n; i++)
    {
        if (exa[i].p >= 0) printf("%d ", i);
    }
    return 0;
}
/*5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
*/

A. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.

Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th voter cicibytecoins you can ask him to vote for any other party you choose.

The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.

Input

The first line of input contains two integers nn and mm (1n,m30001≤n,m≤3000) — the number of voters and the number of parties respectively.

Each of the following nn lines contains two integers pipi and cici (1pim1≤pi≤m, 1ci1091≤ci≤109) — the index of this voter‘s preferred party and the number of bytecoins needed for him to reconsider his decision.

The United Party of Berland has the index 11.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples input Copy
1 2
1 100
output Copy
0
input Copy
5 5
2 100
3 200
4 300
5 400
5 900
output Copy
500
input Copy
5 5
2 100
3 200
4 300
5 800
5 900
output Copy
600
Note

In the first sample, The United Party wins the elections even without buying extra votes.

In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 33, 44 and 55 get one vote and party number 22 gets no votes.

In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.

這個是一個特別惡心的貪心,不會寫。

然後就沒有用貪心寫了,看了一個題解,寫的簡單易懂。

就是枚舉成功需要的票數。

就是先對這個進行從大到小的排序,然後從前往後查詢,如果有黨的票數大於1號的票,那就行賄這個黨派的這個人。

然後之後,如果得到的票數(行賄有競爭的黨派)小於這個枚舉的狀態,那就從後往前去行賄就可以了。

然後對枚舉的每一個狀態取min

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 3e3 + 100;
struct node
{
    int id, mon;
}exa[maxn];
bool cmp(node a,node b)
{
    return a.mon > b.mon;
}
int vis[maxn], vit[maxn];

int main()
{
    int n, m;
    ll ans = inf, s = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &exa[i].id, &exa[i].mon);
        if (exa[i].id == 1) s++;
    }
    sort(exa + 1, exa + 1 + n,cmp);
    for(int i=s;i<=(n+1)/2;i++)
    {
        //printf("i=%d\n", i);
        memset(vis, 0, sizeof(vis));
        memset(vit, 0, sizeof(vit));
        ll sum = 0,num=s;
        for(int j=1;j<=n;j++)
        {
            node e = exa[j];
            vis[e.id]++;
            if(vis[e.id]>i-1&&e.id!=1)
            {
                vit[j] = 1;
                vis[e.id]--;
                num++;
                sum += e.mon;
                //printf("j=%d  sum=%d %d\n", j, sum,e.mon);
            }
        }
        for(int j=n;j>=1&&num<i;j--)
        {
            if(!vit[j]&&exa[j].id!=1)
            {
                num++;
                sum += exa[j].mon;
            }
        }
        if(num>=i)
        {
            ans = min(ans, sum);
        }
    }
    if (ans >= inf) printf("0\n");
    else printf("%lld\n", ans);
    return 0;
}
/*5 5
2 100
3 200
4 300
5 800
5 900
*/

今日 日常訓練三題