Virgo's Trial F-6 HDU-1789 && G-7 HDU-1241 && H-8 HDU-1495 && I-9 UVA-10020 && J-0 UVA-10382

                                                                          Doing Homework again

Problem Description

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input

The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.

Output

For each test case, you should output the smallest total reduced score, one line per test case.

Sample Input

3

3

3 3 3

10 5 1

3

1 3 1

6 2 3

7

1 4 6 4 2 4 3

3 2 1 7 6 5 4

Sample Output

0

3

5

参考文章:https://blog.csdn.net/huifeng_/article/details/81094397

思路:贪心问题,先用sort函数将分数从大到小排序,先写失分最大的作业,若在截止日期还不能写完,则作业失分。
下面给出AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct lou
{
	int day, fen;
}s[1010];
bool cmp(lou a, lou b)
{
	return a.fen>b.fen;
}
int book[10000];
int main()
{
	int n, t;
	cin >> t;
	while (t--)
	{
		cin>>n;
		int i;
		for (i = 0; i < n; i++)
			cin >> s[i].day;
		for (i = 0; i < n; i++)
			cin >> s[i].fen;
		sort(s, s + n, cmp);//结构体排序;
		memset(book, 0, sizeof(book));
		int sum = 0;
		for (i = 0; i < n; i++)
		{
			int a = s[i].day;
			while (a)
			{
				if (book[a] == 0)//这一天还没写作业;
				{
					book[a] = 1;//标记这一天写了作业;
					break;
				}
				a--;
			}
			if (a == 0)//这门作业只能失分;
				sum += s[i].fen;
		}
		cout << sum << endl;
	}
	return 0;
}

                                                   Oil Deposits

Problem Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

题意概括:

        在一个n*m的油田里用“@”表示石油,“*”为非石油,在一个石油的上下左右,斜上,斜下,八个方向上挨着的石油都是属于同一块油矿,计算有多少个油矿。

解题分析:

        先确定一块石油的位置,然后搜索其周边的八个方向,将能连到的记为同一块油矿,同时把找过的石油标记,表示已找过,然后搜索下一块没找到过的石油,直至找完。

下面给出AC代码:

#include<iostream>
using namespace std;
int m[8][2] = { {-1,0},{1,0},{0,-1},{0,1},{-1,1},{-1,-1}, {1,1},{1,-1} };
int vis[105][105], ans;
int w, h;
char str[205][205];
void dfs(int x, int y)
{
	if (x < 0 || x >= w || y < 0 || y >= h || str[x][y] == '*' || vis[x][y])
		return;
	else
	{
		int dx, dy;
		for (int i = 0; i < 8; i++)
		{
			vis[x][y] = 1;
			dx = x + m[i][0];
			dy = y + m[i][1];
			dfs(dx, dy);
		}
	}
}
int main()
{
	while (cin >> w >> h&&w&&h)
	{
		ans = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < w; i++)
		{
			cin >> str[i];
		}
		for (int i = 0; i < w; i++)
		{
			for (int j = 0; j < h; j++)
			{
				if (str[i][j] == '@'&&vis[i][j] == 0)
				{
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

                                                                    非常可乐

Problem Description

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。

Input

三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。

Output

如果能平分的话请输出最少要倒的次数,否则输出"NO"。

Sample Input

7 4 3

4 1 3

0 0 0

Sample Output

NO

3

参考文章https://blog.csdn.net/qq_36424540/article/details/76290210

思路:
1.第一个难点是标记问题,如果用三维会超时;
2.分成六种情况,详见代码。

#include <iostream>
#include<queue>
using namespace std;
const int Max = 101;
int s, n, m;
int vis[Max][Max];
typedef struct node
{
	int x, y, z;
	int step;
}Node;
int bfs()
{
	memset(vis, 0, sizeof(vis));
	Node now, next;
	queue<Node> q;
	now.x = s, now.y = 0, now.z = 0, now.step = 0;
	vis[now.x][now.y] = 1;
	q.push(now);
	while (q.size())
	{
		now = q.front();
		q.pop();
		if ((now.x == now.y&&now.z == 0) || (now.x == now.z&&now.y == 0) || (now.y == now.z&&now.x == 0))
			return now.step;

		for (int i = 1; i <= 6; i++)
		{
			switch (i)
			{
			case 1:
				if (now.x != 0 && now.y != n)
					next.x = now.x - (n - now.y), next.y = n, next.z = now.z;
				break;
			case 2:
				if (now.x != 0 && now.z != m)
					next.x = now.x - (m - now.z), next.y = now.y, next.z = m;
				break;
			case 3:
				if (now.y != 0)
					next.x = now.x + now.y, next.y = 0, next.z = now.z;
				break;
			case 4:
				if (now.y != 0 && now.z != m)
				{
					if (now.y + now.z>m)
						next.x = now.x, next.y = now.y - (m - now.z), next.z = m;
					else
						next.x = now.x, next.y = 0, next.z = now.z + now.y;
				}
				break;
			case 5:
				if (now.z != 0)
					next.x = now.x + now.z, next.y = now.y, next.z = 0;
				break;
			case 6:
				if (now.z != 0 && now.y != n)
				{
					if (now.y + now.z>n)
						next.x = now.x, next.y = n, next.z = now.z - (n - now.y);
					else
						next.x = now.x, next.y = now.y + now.z, next.z = 0;
				}
				break;
			}
			if (vis[next.x][next.y])
				continue;
			vis[next.x][next.y] = 1;
			next.step = now.step + 1;
			q.push(next);
		}
	}
	return -1;
}
int main()
{
	while (cin>>s>>n>>m)
	{
		if (!s && !n && !m)
			break;
		memset(vis, 0, sizeof(vis));
		if (s & 1)
			cout<<"NO"<<endl;
		else
		{
			int ans = bfs();
			if (ans == -1)
				cout << "NO" << endl;
			else
				cout << ans << endl;
		}

	}
}

                                          UVA 10382 Watering Grass 

参考文章:https://blog.csdn.net/Mannix_Y/article/details/82819876

 题解:贪心区间覆盖问题,计算出每个装置所能覆盖的最大矩形的最左端和最右端,并记录下来,就成了一个裸的区间覆盖问题。
区间覆盖问题:先对小区间段的按左端大小进行排序,每次查找,衔接已经覆盖的区间且最大向右延伸距离的区间段,直到覆盖整个大区间,具体看代码注释。

#include<iostream>
#include<algorithm>
using namespace std;
#define MT(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
struct dd 
{
	double l; double r;
	bool friend operator < (dd a, dd b) 
	{
		return a.l == b.l ? a.r > b.r : a.l < b.l;
	}
};
int main()
{
	int n, l, w;
	while (cin>>n>>l>>w)
	{
		dd a[maxn];
		MT(a, 0);
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			int pos, r;
			cin >> pos >> r;
			if (r <= 1.0 * w / 2) continue;
			double d = sqrt(1.0*r*r - 1.0*w*w / 4);
			a[cnt].l = pos - d;
			a[cnt++].r = pos + d; 
		}
		double pos = 0, t = 0; 
		int k = 0, ans = 0;  
		sort(a, a + cnt);
		while (pos < l) 
		{
			for (int i = k; i < cnt; i++) 
			{
				if (a[i].l <= pos)  t = max(t, a[i].r);
				else { k = i;  break; }
			}
			if (pos == t) { ans = -1;  break; }
			ans++; 
			pos = t; 
		}
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43527358/article/details/86685001
今日推荐