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).

Examples
Input
Copy
4
>= 1 Y
< 3 N
<= -3 N
> 55 N
Output
17
Input
Copy
2
> 100 Y
< -100 Y
Output
Impossible

题意:找到任意一个在题目规定区间的数,规定区间也就是输入语句中的提示。

思路:模拟即可。

#include<stdio.h>
#include<iostream> 
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 10010;
char str[maxn],str2[maxn];

int main()
{
	int s,e;
	int n,number;
	while(scanf("%d",&n)!=EOF)
	{
		s = -inf;
		e = inf;
		for(int i = 0; i < n; i++)
		{
			scanf("%s%d%s",str,&number,str2);
			if(str[0] == '>')
			{
				if(str[1] == '=')
				{
					if(str2[0] == 'Y')
					{
						if(number > s)
							s = number;
					}
					else
						if(number <= e)
							e = number-1;
				}
				else
				{
					if(str2[0] == 'Y')
					{
						if(number >= s)
							s = number +1;
					}
					else 
						if(number < e)
							e = number;
				}
				
			}
			else if(str[0] == '<')
			{
				if(str[1] == '=')
				{
					if(str2[0] == 'Y')
					{
						if(number < e)
							e = number;
					}
					else
						if(number >= s)
							s = number +1;
				}
				else
				{
					
					if(str2[0] == 'Y')
					{
						if(number <= e)
							e = number -1;
					}
					else 
						if(number > s)
							s = number;
				}
			}
		}
		if(s > e)
			printf("Impossible\n");
		else
			printf("%d\n",s);
			
	}
	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 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.

Examples
Input
Copy
5 1
1
2
3
4
5
Output
1 3 6 10 15 
Input
Copy
4 2
2 5
3 1
5 3
10 1
Output
7 8 13 21 

题意:已知n幅画和m个画家,接下来输入n行,每行m个数,表示第j个画家在第i幅画上所花时间,输出n个数,表示第m个画家完成第i(0<=i<=n-1)幅画时的时刻。

思路:一道比较简单的dp题,转移方程就是比较第j个画家完成i-1幅画的时间和第j-1个画家完成第i幅画的时间大小,再加上第j个画家完成第i幅画的时间,得到第j个画家完成第j幅画的总时间,初始化需要注意,需要将第0个画家完成第0~n-1幅画的时间分别计算存入数组。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 50010;
int vis[maxn][10],time[maxn][10];
int num[maxn];

int main()
{
	int n,m;
	int sum,front;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		for(int i = 0; i < n; i ++)
			for(int j = 0; j < m; j ++)
				scanf("%d",&time[i][j]);
				
		vis[0][0] = time[0][0];
		for(int i = 1; i < m; i ++)
			vis[0][i] = vis[0][i-1] + time[0][i];//第m-1个画家画完第一幅画的时间 
				
		for(int i = 1; i < n; i ++)
			vis[i][0] = vis[i-1][0] + time[i][0];//记录第1个画家在第i幅画上需要的时间 
		
		for(int i = 1; i < n; i ++)
			for(int j = 1; j < m; j ++)
				vis[i][j] = max(vis[i][j-1],vis[i-1][j]) + time[i][j];////递推计算第m-1个画家在第i幅画上所花时间 
				
		for(int i = 0; i < n; i ++)
			printf("%d ",vis[i][m-1]);
		printf("\n");
	}
	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.

Examples
Input
Copy
3
10 50
2 100
5 30
3
4 6 9
Output
Copy
2 130
2 1
3 2

题意:输出n,接下来,输入n行数,每行两个数,表示食客团的人数和所带金额;再输入m,接下来输入m个数,表示每个饭桌能容纳的人数,问,能接纳的食客团的总数和所收金额数,再输出接纳的食客团的编号和对应的饭桌编号。

思路:贪心,排序需要注意,食客团按金额数从大到小排,而饭桌能容纳的人数从小到大排,这样进行计算时,保证了第一个满足能容纳食客团的饭桌是最优条件,一开始自己是按食客团的人数从小到大排,所以出错。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1010;
struct node{
	int pepole,money,number;
}num[maxn];//记录食客团 
struct Node{
	int v,n;
}vis[maxn];//记录酒桌 
struct name{
	int number1,number2;
}book1[maxn];//记录最终食客团序号和酒桌序号 
int book[maxn],book2[maxn];

int cmp(struct node a,struct node b)//对食客进行排序 
{
	if(a.money == b.money )
		return  a.pepole < b.pepole;
	return a.money > b.money ;
}
int cmp2(struct Node a,struct Node b)//对酒桌容量进行排序 
{
	return a.v < b.v ;
}
int main()
{
	int n,m;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i = 0; i < n; i ++)
		{
			scanf("%d%d",&num[i].pepole ,&num[i].money );
			num[i].number = i+1;
		}
		sort(num,num+n,cmp);//对食客团钱数从大到小排 
		
		scanf("%d",&m);
		for(int i = 0; i < m; i ++)
		{
			scanf("%d",&vis[i].v );
			vis[i].n = i+1;
		}
		sort(vis,vis+m,cmp2);//对酒桌容量从小到大排 
		int sum = 0,sum2=0;
		memset(book,0,sizeof(book));
		memset(book2,0,sizeof(book2));
		for(int i = 0; i < n; i ++)
		{
			for(int j = 0; j < m; j ++)
			{
				if(!book[i]&&!book2[j]&&num[i].pepole <= vis[j].v )//如果没有访问过,并且食客团能坐下 
				{
					sum += num[i].money ;//记录盈利金额 
					book[i] = 1;//标记食客团已经被访问过 
					book1[i].number1 = num[i].number ;//记录食客团序号 
					book1[i].number2 = vis[j].n ;//记录酒桌序号 
					book2[j] = vis[j].n ;//标记酒桌已经被访问过 
					sum2 ++;//记录接纳食客团的总数 
					break;
				}
			}
		}	
		printf("%d %d\n",sum2,sum);//输出接纳食客团的总数和盈利金额数 
		for(int i = 0; i < n; i ++)
		{
			if(book[i])
				printf("%d %d\n",book1[i].number1,book1[i].number2 );//输出食客团序号和酒桌序号 
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值