CF进击小分队(二)

今天刚刚回学校

有些无聊...

昂神推荐一个练习...

18:45开始...

去做了一下...

还有两道题..

一个是nim博弈不会做..

还有一个是字符串dp吧看样子...也不太会..或者说木有耐心做...

好饿..

去吃点东西..

明天不拖小明后腿好好训练..


A - Cottage Village
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.

The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the Оx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn't overlap any of the houses in the village.

Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?

Input

The first line of the input data contains numbers n and t (1 ≤ n, t ≤ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi — x-coordinate of the centre of the i-th house, and ai — length of its side ( - 1000 ≤ xi ≤ 10001 ≤ ai ≤ 1000).

Output

Output the amount of possible positions of the new house.

Sample Input

Input
2 2
0 4
6 2
Output
4
Input
2 2
0 4
5 2
Output
3
Input
2 3
0 4
5 2
Output
2

Hint

It is possible for the x-coordinate of the new house to have non-integer value.


模拟一遍...木有什么难度?

#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
	double left;
	double right;
}house[10005];
bool cmp(const node &x,const node &y)
{
	return x.left<y.left;
}
int main()
{
	int n;
	double t;
	while(cin>>n>>t)
	{
		for(int i=1;i<=n;i++)
		{
			double x,a;
			cin>>x>>a;
			house[i].left=x-a/2;
			house[i].right=x+a/2;
		}
		house[0].left=house[0].right=-INF;
		sort(house+1,house+n+1,cmp);
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			if(house[i].left-house[i-1].right>t)
			{
				ans+=2;
			}
			else if(house[i].left-house[i-1].right==t)
			{
				ans+=1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
		

B - Laser
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector.

The following facts about the experiment are known:

  • initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active;
  • the chocolate melts within one cell of the field at which the laser is pointed;
  • all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells;
  • at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman.

You are given nm and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.

Input

The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers nmx1y1x2y2, separated by a space (2 ≤ n, m ≤ 1091 ≤ x1, x2 ≤ n1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct.

Output

Each of the t lines of the output should contain the answer to the corresponding input test set.

Sample Input

Input
2
4 4 1 1 3 3
4 3 1 1 2 2
Output
8
2

容斥原理

/*
容斥原理
画个图就清楚了
*/
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		long long n,m,x1,y1,x2,y2;
		long long minx,miny,maxx,maxy,up,left,right,down;
		long long lenleft,lenright,lenup,lendown,deltax=0,deltay=0;
		long long ans=0;
		cin>>n>>m>>x1>>y1>>x2>>y2;
		minx=min(x1,x2);
		miny=min(y1,y2);//左上点 
		maxx=max(x1,x2);
		maxy=max(y1,y2);//右下点 
		up=minx-1;//距四边距离 
		left=miny-1;
		down=n-maxx;
		right=m-maxy;
		lendown=minx+down;//分别覆盖举行长宽 
		lenleft=miny+right;
		lenup=maxx-up;
		lenright=maxy-left;
		if(lendown>=lenup)//相交部分 
		{
			deltax=lendown-lenup+1;
		}
		if(lenleft>=lenright)
		{
			deltay=lenleft-lenright+1;
		}
		ans=lendown*lenleft+(n-lenup+1)*(m-lenright+1);
		if(deltax>0&&deltay>0)//容斥 
		{
			ans-=deltax*deltay;
		}
		cout<<n*m-ans<<endl;
	}
	return 0;
}

D - Noldbach problem
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even integer greater than 2 can be expressed as the sum of two primes. That got Nick's attention and he decided to invent a problem of his own and call it Noldbach problem. Since Nick is interested only in prime numbers, Noldbach problem states that at least k prime numbers from 2 to n inclusively can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5 + 7 + 1.

Two prime numbers are called neighboring if there are no other prime numbers between them.

You are to help Nick, and find out if he is right or wrong.

Input

<p< p="">

The first line of the input contains two integers n (2 ≤ n ≤ 1000) and k (0 ≤ k ≤ 1000).

Output

<p< p="">YES

Sample Input

Input
27 2
Output
YES
Input
45 7
Output
NO

Hint

<p< p="">YES
筛法求素,然后根据条件判断?数据量好小..

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int prime[1005];
int a[1005];
void init()
{
	memset(prime,1,sizeof(prime));
	prime[0]=0;
	prime[1]=0;
	int k=0;
	for(int i=2;i<=1000;i++)
	{
		if(prime[i])
		{
			a[k++]=i;
			for(int j=i+i;j<=1000;j+=i)
			{	
				prime[j]=0;
			}
		}
	}
}
int main()
{
	init();
	int n,k;
	while(cin>>n>>k)
	{
		int ans=0;
		for(int i=1;a[i]<=n;i++)
		{
			int temp=a[i]+a[i-1]+1;
			if(prime[temp]&&temp<=n)
			{
				ans++;
			}
		}
		if(ans<k)
		{
			cout<<"NO"<<endl;
		}
		else
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}
			
		

E - Hierarchy
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employeeai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.

Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

Input

The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: aibi and ci (1 ≤ ai, bi ≤ n0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.

Output

Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.

Sample Input

Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1

Hint

In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.


这个题.......签到么...简单贪心,最后根的cost为INF,其他只要都不为INF就输出ans

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
	int n;
	cin>>n;
	int cost[1005];
	memset(cost,INF,sizeof(cost));
	for(int i=1;i<=n;i++)
	{
		int temp;
		cin>>temp;
	}
	int m;
	cin>>m;
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		cost[b]=min(cost[b],c);
	}
	int ans=0;
	int flag=1;
	int first=1;
	for(int i=1;i<=n;i++)
	{
//		cout<<cost[i]<<endl;
		if(cost[i]==INF&&first)
		{
			first=0;
			ans-=INF;
		}
		else if(cost[i]==INF)
		{
			flag=0;
		}
		ans+=cost[i];
	}
	if(flag)
	{
		cout<<ans<<endl;
	}
	else
	{
		cout<<"-1"<<endl;
	}
	return 0;
}
加油啊~

好好训练一个月就可以回去开黑十天了...



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值