关闭

cf(416 A,B,C,E)

标签: acm大二思维题
595人阅读 评论(0) 收藏 举报
分类:

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

简单模拟


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int f[100005];

int main()
{
    int n,min0=-1000001000,max0=1000010000;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        char x[3],an;int num;
        cin>>x>>num>>an;
        if(an=='Y'&&x[0]=='>')
        {
            if(x[1]=='=')
            min0=max(min0,num);
            else min0=max(min0,num+1);
        }
        else if(an=='N'&&x[0]=='>')
        {
            if(x[1]=='=')
                max0=min(max0,num-1);
            else max0=min(max0,num);
        }
        else if(an=='Y'&&x[0]=='<')
        {
            if(x[1]=='=')
            max0=min(max0,num);
            else max0=min(max0,num-1);
        }
        else if(an=='N'&&x[0]=='<')
        {
            if(x[1]=='=')
                min0=max(min0,num+1);
            else min0=max(min0,num);
        }
       // cout<<max0<<' '<<min0<<endl;
    }
    if(max0>=min0)
        cout<<min0<<endl;
    else cout<<"Impossible"<<endl;
    return 0;
}



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 tijis 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 

不可思议的简单dp。。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int dp[50010][6]={{0}};

int main()
{
    int n,m;
    cin>>m>>n;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        {
            int a;
            cin>>a;
            dp[i][j]=max(a+dp[i-1][j],a+dp[i][j-1]);
        }
    for(int i=0;i<m;i++)
        cout<<dp[i][n-1]<<' ';
    cout<<endl;
    return 0;
}


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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

int ss[1010][2];
struct data
{
    int c,p,i;double w;
}a[1010];

struct hh
{
    int rr,i;
}r[1010];

bool cmp(data a,data b)
{
    if(a.p==b.p)
        return a.w>b.w;
    return a.p>b.p;
}

bool cmm(hh a,hh b)
{
    return a.rr<b.rr;
}

int main()
{
    int n,k;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i].c>>a[i].p,a[i].w=1.0*a[i].p/a[i].c,a[i].i=i+1;
    cin>>k;
    for(int i=0;i<k;i++)
        cin>>r[i].rr,r[i].i=i+1;
    sort(a,a+n,cmp);
    sort(r,r+k,cmm);
    int ans=0,num=0,t=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<k;j++)
        if(a[i].c<=r[j].rr)
        {
            //cout<<a[i].c<<"<="<<r[j].rr<<endl<<endl;
            r[j].rr=0,num++,ans+=a[i].p,ss[t][0]=a[i].i,ss[t++][1]=r[j].i;break;
        }
    }
    cout<<num<<' '<<ans<<endl;
    for(int i=0;i<num;i++)
        cout<<ss[i][0]<<' '<<ss[i][1]<<endl;
    return 0;
}

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 
由于数据比较小。。直接用floyd就ok,不过由于这题要求最短路的条数,所以再开一个数组记录两两点之间的最短路的条数;


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
int a[505][505],d[505][505],num[505][505]={{0}};
int main()
{
    int max0=1000000000;
    for(int i=0;i<505;i++)
        for(int j=0;j<505;j++)
        d[i][j]=a[i][j]= i==j?0:max0;
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a[x][y]=a[y][x]=d[x][y]=d[y][x]=z;
    }
    for(int k=1;k<=n;k++)
     for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
            if(d[i][k]!=max0&&d[k][j]!=max0)
            d[i][j]=min(d[i][k]+d[k][j],d[i][j]);
    for(int k=1;k<=n;k++)
     for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
         {
             if(a[k][j]&&d[i][j]==d[i][k]+a[k][j])
            num[i][j]++;
            //cout<<"dp["<<i<<"]["<<k<<"]+a["<<k<<"]["<<j<<"]==dp["<<i<<"]["<<j<<"]"<<endl;
         }
        //for(int i=1;i<=n;i++)
        //   for(int j=1;j<=n;j++)
         //   cout<<i<<"dao"<<j<<':'<<num[i][j]<<endl;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
    {
        int s=0;
        for(int k=1;k<=n;k++)
            if(d[i][k]!=max0&&d[k][j]!=max0&&d[i][k]+d[k][j]==d[i][j])
            s+=num[i][k];
        printf("%d ",s);
    }puts("");
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人说明


    即便如此,


    我们也在自己


    选择的道路上


    稳步前行!


    个人资料
    • 访问:113257次
    • 积分:4366
    • 等级:
    • 排名:第7341名
    • 原创:335篇
    • 转载:8篇
    • 译文:0篇
    • 评论:2条
    最爱JAY
    博客专栏