cf(416 A,B,C,E)

原创 2015年11月18日 20:03:42

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;
}


版权声明:本文为博主原创文章,若转载请注明转载地址http://blog.csdn.net/martinue。

一个字符数组,里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后,而且各部分内部分别有序(创新工场)

一个字符数组,里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后,而且各部分内部分别有序。思路:先进行字符串排序,用堆排...
  • zzran
  • zzran
  • 2013年11月04日 16:56
  • 5200

cf(412A,B,C)

A. Poster time limit per test 1 second memory limit per test 256 megabytes input s...
  • martinue
  • martinue
  • 2015年11月26日 20:31
  • 383

cf(413A,B,C)

A. Data Recovery time limit per test 1 second memory limit per test 256 megabytes i...
  • martinue
  • martinue
  • 2015年11月28日 16:59
  • 404

趣味编程:有A,B,C,D,E五人,每人额头上都帖了一张黑或白的纸

#include using std::cout; using std::endl; int main(){ /**** 有A、B、C、D、E五人,每人额头上都帖了一张黑或白的纸。五人对...
  • harrison2010
  • harrison2010
  • 2016年12月28日 10:36
  • 566

A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束

A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束:    1. 如果A参加,B也参加;    2. ...
  • qq_37614259
  • qq_37614259
  • 2017年04月07日 09:06
  • 293

CF 487E Tourists(JZOJ4691 旅行) 树链剖分维护点双连通分量信息

题目大意给定一个NN个点MM条边的无向图,每个点有一个点权。现在有两种操作,第一种是修改一个点的点权,第二种是询问两点间路径上的最小点权(不能经过重复的点)。操作数为QQ。N,M,Q≤109N,M,Q...
  • YxuanwKeith
  • YxuanwKeith
  • 2016年08月15日 10:46
  • 616

含括号的运算表达式求解——栈的基础应用3

如有错误不当之处,欢迎指出,以免害人害己 本文根据严蔚敏老师数据结构(c语言版) 写的程序 如有需要先去看视频  鄙人觉得相当不错 栈的基础应用3——运算表达式求解 例如 Exp =...
  • you1314520me
  • you1314520me
  • 2013年05月13日 18:59
  • 1134

PHP 面试题解析

1. 选择合适的答案填入空白处 PHP 是一种______脚本语言,基于______引擎。PHP 最常被用来开发动态的______内 容,此外,它同样还可被用来生成______(以及其他)文档。 A....
  • a437629292
  • a437629292
  • 2014年11月11日 17:09
  • 1360

多线程下载时HTTP response code: 416 解决方案

今天用java写的多线程下载报错,找了很久,发现是第一次请求服务器时响应码正常,开启多线程部分的响应码为416,响应码对应的意思可以去这里看看点击打开链接。主要意思就是public void set...
  • baidu_31119513
  • baidu_31119513
  • 2016年12月31日 15:57
  • 930

参赛约束 A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。

我实在没有想到更好的方法了,只好老老实实的,枚举了,十个for,是个人...package org.bluebridge.topics; /*参赛约束 A、B、C、D、E、F、G、H、I、J 共1...
  • u013091087
  • u013091087
  • 2015年02月13日 13:44
  • 1198
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cf(416 A,B,C,E)
举报原因:
原因补充:

(最多只允许输入30个字)