Codeforces Round #241 (Div. 2) 解题报告

11 篇文章 0 订阅
6 篇文章 0 订阅
Codeforces Round #241 (Div. 2)
A. Guess a number!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show.

The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions:

  • Is it true that y is strictly larger than number x?
  • Is it true that y is strictly smaller than number x?
  • Is it true that y is larger than or equal to number x?
  • Is it true that y is smaller than or equal to number x?

On each question the host answers truthfully, "yes" or "no".

Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible".

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is:

  • ">" (for the first type queries),
  • "<" (for the second type queries),
  • ">=" (for the third type queries),
  • "<=" (for the fourth type queries).

All values of x are integer and meet the inequation  - 109 ≤ x ≤ 109. The answer is an English letter "Y" (for "yes") or "N" (for "no").

Consequtive elements in lines are separated by a single space.

Output

Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation  - 2·109 ≤ y ≤ 2·109. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes).

Sample test(s)
input
4
>= 1 Y
< 3 N
<= -3 N
> 55 N
output
17
input
2
> 100 Y
< -100 Y
output
Impossible

    解题报告:第一题不难,但是我也被人Hack了一次,因为刚开始最大值设大了。

    将最大值设为2000000000,最小值设为-2000000000,每次判断更新即可。代码如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

void work(int n)
{
    int sta=-2000000000, end=2000000000;

    while(n--)
    {
        string op, res;
        int num;

        cin>>op>>num>>res;

        if((op==">" && res=="Y") || (op=="<=" && res=="N"))
            sta=max(sta, num+1);
        else if((op=="<" && res=="Y") || (op==">=" && res=="N"))
            end=min(end, num-1);
        else if((op==">" && res=="N") || (op=="<=" && res=="Y"))
            end=min(end, num);
        else if((op=="<" && res=="N") || (op==">=" && res=="Y"))
            sta=max(sta, num);
    }

    if(end>=sta)
        printf("%d\n", sta);
    else
        puts("Impossible");
}

int main()
{
    int n;
    while(~scanf("%d", &n))
        work(n);
}

    当然,也可以把所有左边的限制保持下来,最后取最大值;所有的右边的限制保存下来,最后取最小值。


B. Art Union
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.

Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time.

Order is important everywhere, so the painters' work is ordered by the following rules:

  • Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n);
  • each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on;
  • each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest;
  • as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter.

Given that the painters start working at time 0, find for each picture the time when it is ready for sale.

Input

The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), where tij is the time the j-th painter needs to work on the i-th picture.

Output

Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.

Sample test(s)
input
5 1
1
2
3
4
5
output
1 3 6 10 15 
input
4 2
2 5
3 1
5 3
10 1
output
7 8 13 21 

    解题报告:水题来的。每次注意更新时间的起点:是上一次结束的时间,还是上一位画家结束的时间。代码如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
int sta[5];

void work(int m, int n)
{
    memset(sta, 0, sizeof(sta));

    for(int i=0;i<m;i++)
    {
        int tmp;
        scanf("%d", &tmp);
        sta[0] += tmp;

        for(int i=1;i<n;i++)
        {
            int tmp;
            scanf("%d", &tmp);
            sta[i]=max(sta[i-1], sta[i]);
            sta[i]+=tmp;
        }

        printf("%d ", sta[n-1]);
    }

    puts("");
}

int main()
{
    int m, n;
    while(~scanf("%d%d", &m, &n))
        work(m, n);
}


C. Booking System
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!

A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.

There are n booking requests received by now. Each request is characterized by two numbers: ci and pi — the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.

We know that for each request, all ci people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.

Unfortunately, there only are k tables in the restaurant. For each table, we know ri — the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.

Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.

Input

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of requests from visitors. Then n lines follow. Each line contains two integers: ci, pi (1 ≤ ci, pi ≤ 1000) — the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.

The next line contains integer k (1 ≤ k ≤ 1000) — the number of tables in the restaurant. The last line contains k space-separated integers: r1, r2, ..., rk (1 ≤ ri ≤ 1000) — the maximum number of people that can sit at each table.

Output

In the first line print two integers: m, s — the number of accepted requests and the total money you get from these requests, correspondingly.

Then print m lines — each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.

If there are multiple optimal answers, print any of them.

Sample test(s)
input
3
10 50
2 100
5 30
3
4 6 9
output
2 130
2 1
3 2

    解题报告: 贪心。将桌子按容量从小到大排。每次选择钱最多的团队坐进桌子并标记,这样就能保证最终的最大值。代码如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

struct Group
{
    int num;
    int val;
    int idx;
    bool operator<(const Group& cmp) const
    {
        return num<cmp.num;
    }
} group[1111];

struct Node
{
    int r;
    int idx;
    bool operator<(const Node& cmp) const
    {
        return r<cmp.r;
    }
} r[1111];
int sit[1111];

void work(int n)
{
    memset(sit, -1, sizeof(sit));

    for(int i=0;i<n;i++)
        scanf("%d%d", &group[i].num, &group[i].val), group[i].idx = i+1;
    sort(group, group+n);

    int k;
    scanf("%d", &k);
    for(int i=0;i<k;i++)
        scanf("%d", &r[i].r), r[i].idx=i+1;
    sort(r, r+k);

    int t=0;
    int res=0;
    for(int i=0;i<k;i++)
    {
        int v=0;
        int pos=-1;
        for(int j=0;j<n;j++) if(sit[j]==-1 && group[j].num<=r[i].r && group[j].val>v)
            pos=j, v=group[j].val;

        if(~pos)
            sit[pos]=i, t++, res+=group[pos].val;
    }

    printf("%d %d\n", t, res);
    for(int i=0;i<n;i++) if(~sit[i])
        printf("%d %d\n", group[i].idx, r[sit[i]].idx);
}

int main()
{
    int n;
    while(~scanf("%d", &n))
        work(n);
}


D. Population Size
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions.

Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence a1, a2, ..., an, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8, 6, 4, 2, 1, 4, 7, 10, 2) can be considered as a sequence of three arithmetic progressions (8, 6, 4, 2)(1, 4, 7, 10) and (2), which are written one after another.

Unfortunately, Polycarpus may not have all the data for the n consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of ai ​​may be unknown. Such values are represented by number -1.

For a given sequence a = (a1, a2, ..., an), which consists of positive integers and values ​​-1, find the minimum number of arithmetic progressions Polycarpus needs to get a. To get a, the progressions need to be written down one after the other. Values ​​-1 may correspond to an arbitrary positive integer and the values ai > 0 must be equal to the corresponding elements of sought consecutive record of the progressions.

Let us remind you that a finite sequence c is called an arithmetic progression if the difference ci + 1 - ci of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression.

Input

The first line of the input contains integer n (1 ≤ n ≤ 2·105) — the number of elements in the sequence. The second line contains integer values a1, a2, ..., an separated by a space (1 ≤ ai ≤ 109 or ai =  - 1).

Output

Print the minimum number of arithmetic progressions that you need to write one after another to get sequence a. The positions marked as-1 in a can be represented by any positive integers.

Sample test(s)
input
9
8 6 4 2 1 4 7 10 2
output
3
input
9
-1 6 -1 2 -1 4 7 -1 2
output
3
input
5
-1 -1 -1 -1 -1
output
1
input
7
-1 -1 4 5 1 2 3
output
2

    解题报告:因为中途第一题被人Hack了,这题就没想了,直接去hack别人去了……成功了两次……。

    赛后看这题,标签上说是贪心,想了一下可以。每次使当前的数列尽量长。如果不和条件则新建序列。

    每个序列保持这几个参数:起点sta,第一个确定数据点p,以及数据点的值pv,是否有斜率k,斜率值kv。

    注意确定斜率值kv时要保证起点处的值大于0,确定kv值后要保证-1点的值大于0。

    最后,用long long,因为斜率最大可以是99999999。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        LL p, pv;
        LL k, kv;

        p=-1;
        k=-1;

        LL sta = -1;
        int ans = 0;
        for(int i=0;i<n;i++)
        {
            int tmp;
            scanf("%d", &tmp);

            if(~tmp)
            {
                if(~k)
                {
                    if(kv*(i-p)!=(tmp-pv))
                    {
                        ans++;
                        sta = i;
                        p = i;
                        pv = tmp;
                        k = -1;
                    }
                }
                else if(~p)
                {
                    if((tmp-pv)%(i-p)==0 && tmp-(tmp-pv)/(i-p)*(i-sta)>0)
                    {
                        k = 1;
                        kv = (tmp-pv)/(i-p);
                    }
                    else
                    {
                        ans++;
                        sta = i;
                        p = i;
                        pv = tmp;
                        k = -1;
                    }
                }
                else if(~sta)
                {
                    p = i;
                    pv = tmp;
                    k = -1;
                }
                else
                {
                    ans++;
                    sta = i;
                    p = i;
                    pv = tmp;
                    k = -1;
                }
            }
            else
            {
                if(~k)
                {
                    LL tmp = kv*(i-p)+pv;
                    if(tmp<1)
                    {
                        ans++;
                        sta = i;
                        p = -1;
                        k = -1;
                    }
                }
                else if(~p)
                {

                }
                else if(~sta)
                {

                }
                else
                {
                    ans++;
                    sta = i;
                    p = -1;
                    k = -1;
                }
            }
        }

        printf("%d\n", ans);
    }
}


E. President's Path
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.

We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.

The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.

Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s < t) find the number of roads that lie on at least one shortest path from s to t.

Input

The first line of the input contains integers n, m (2 ≤ n ≤ 5000 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.

Output

Print the sequence of  integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.

Sample test(s)
input
5 6
1 2 1
2 3 1
3 4 1
4 1 1
2 4 2
4 5 4
output
1 4 1 2 1 5 6 1 2 1 
 
    解题报告:这题看了其他人的代码了……思路是这样的。先求弗洛伊德,然后cnt[i][j]记录i到j最短路中与j相连的边数。
    求结果时遍历最短路中出现的点k ,求cnt[i][k]的和,即为解。
    代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 555;
const int inf = 0x3f3f3f3f;
int c[maxn][maxn];
int d[maxn][maxn];
int cnt[maxn][maxn];

inline void minimize(int & x, const int & y)
{
    if(x>y) x=y;
}

void floyd(int n)
{
    memset(d, 0x3f, sizeof(d));
    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
        minimize(d[i][j], c[i][j]);

    for(int i=1;i<=n;i++) d[i][i]=0;

    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
            minimize(d[i][j], d[i][k]+d[k][j]);

    memset(cnt, 0, sizeof(cnt));
    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(d[i][j]!=inf)
        for(int k=1;k<=n;k++) if(c[k][j]!=inf && d[i][k]+c[k][j]==d[i][j])
            cnt[i][j]++;
}

void work(int n, int m)
{
    memset(c, 0x3f, sizeof(c));
    for(int i=0;i<m;i++)
    {
        int u, v, l;
        scanf("%d%d%d", &u, &v, &l);
        minimize(c[u][v], l);
        minimize(c[v][u], l);
    }

    floyd(n);

    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
    {
        int res=0;
        for(int k=1;k<=n;k++) if(d[i][k]+d[k][j]==d[i][j])
            res+=cnt[i][k];

        printf("%d ", res);
    }

    puts("");
}

int main()
{
    int n, m;
    while(~scanf("%d%d",&n, &m))
        work(n, m);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值