WEEK 12 必做题

A - 必做题 - 1

题目:

给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少?

Input

本题包含多组数据:
每组数据包含两行。
第一行一个数字N(1<=N<=999999) ,保证N为奇数。
第二行为N个用空格隔开的整数。
数据以EOF结束。

Output

对于每一组数据,你需要输出你找到的唯一的数。

Sample Input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output

3
5
1

代码

#include <iostream>
#include <string.h>

using namespace std;

const int N = 1e6 + 5;
int n, m, half;
int input[N];

int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		memset(input, 0, sizeof(input));
		int temp;
		half = (n + 1) / 2;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &temp);
			input[temp]++;
		}
		for (int i = 0; i < N; i++)
			if (input[i] >= half)
			{
				printf("%d\n", i);
				break;
			}
	}
    return 0; 
}

B - 必做题 - 2

题目

zjm被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成。
zjm每次向上下前后左右移动一个单位需要一分钟,且zjm不能对角线移动。
空间的四周封闭。zjm的目标是走到空间的出口。
是否存在逃出生天的可能性?如果存在,则需要多少时间?

Input

输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度,R和C分别表示每层空间的行与列的大小。
随后L层,每层R行,每行C个字符。
每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。
zjm的起始位置在’S’,出口为’E’。每层空间后都有一个空行。
L,R和C均为0时输入结束。

Output

每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。

如果无法逃生,则输出如下
Trapped!

Sample Input

3 4 5
S….
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0

Sample Outpu

Escaped in 11 minute(s).
Trapped!

思路

从起点开始在迷宫中进行BFS,因为是三维空间,所以每个点BFS时有六个方向,每当搜索到一个新的位置,将这一位置置为起点到该点的步数,直到遇到终点停止搜索。若搜索结束仍未到达终点,则终点不可达。

代码

#include <iostream>
#include <queue> 

using namespace std;

const int maxn = 35;
char ch[maxn][maxn][maxn];
int l, r, c;
int sx, sy, sz, ex, ey, ez;
int dis[maxn][maxn][maxn];
int dx[] = { 0,0,1,-1,0,0 };
int dy[] = { 1,-1,0,0,0,0 };
int dz[] = { 0,0,0,0,1,-1 };

struct node 
{
	int x, y, z;
	node(int thex, int they, int thez) :x(thex), y(they), z(thez) {}
};

bool Is_end(const node &a) 
{
	return (a.x == ex && a.y == ey && a.z == ez);
}

bool Go_next(const node &a) 
{
	return (a.x >= 1 && a.x <= r && a.y >= 1 && a.y <= c && a.z >= 1 && a.z <= c && ch[a.x][a.y][a.z] != '#' && dis[a.x][a.y][a.z] == INT_MAX / 2);
}

void output() 
{
	printf("sx: %d sy: %d sz: %d\n", sx, sy, sz);
	printf("ex: %d ey: %d ez: %d\n", ex, ey, ez);
	for (int i = 1; i <= l; i++) 
	{
		for (int j = 1; j <= r; j++) 
		{
			for (int k = 1; k <= c; k++) 
			{
				printf("%d ", dis[j][k][i]);
			}
			printf("\n");
		}
		printf("\n");
	}
}

void bfs() 
{
	for (int i = 1; i <= l; i++)
		for (int j = 1; j <= r; j++)
			for (int k = 1; k <= c; k++)
				dis[j][k][i] = INT_MAX / 2;
	queue<node> q;
	node snode(sx, sy, sz);
	q.push(snode); dis[sx][sy][sz] = 0;
	while (!q.empty()) 
	{
		node temp_node = q.front(); q.pop();
		if (Is_end(temp_node)) 
			break;
		for (int i = 0; i < 6; i++) 
		{
			node new_node(temp_node.x + dx[i], temp_node.y + dy[i], temp_node.z + dz[i]);
			if (Go_next(new_node)) 
			{
				q.push(new_node);
				dis[new_node.x][new_node.y][new_node.z] = dis[temp_node.x][temp_node.y][temp_node.z] + 1;
			}
		}
	}
	if (dis[ex][ey][ez] != INT_MAX / 2) 
		printf("Escaped in %d minute(s).\n", dis[ex][ey][ez]);
	else 
		printf("Trapped!\n");
}

int main() 
{
	while (scanf("%d %d %d", &l, &r, &c) == 3 && !(l == 0 && r == 0 && c == 0)) 
	{
		for (int i = 1; i <= l; i++)
			for (int j = 1; j <= r; j++)
				for (int k = 1; k <= c; k++) 
				{
					scanf(" %c", &ch[j][k][i]);
					if (ch[j][k][i] == 'S') sx = j, sy = k, sz = i;
					if (ch[j][k][i] == 'E') ex = j, ey = k, ez = i;
				}
		bfs();
	}
	return 0;
}

C - 必做题 - 3

题目

东东每个学期都会去寝室接受扫楼的任务,并清点每个寝室的人数。
每个寝室里面有ai个人(1<=i<=n)。从第i到第j个宿舍一共有sum(i,j)=a[i]+…+a[j]个人
这让宿管阿姨非常开心,并且让东东扫楼m次,每一次数第i到第j个宿舍sum(i,j)
问题是要找到sum(i1, j1) + … + sum(im,jm)的最大值。且ix <= iy <=jx和ix <= jy <=jx的情况是不被允许的。也就是说m段都不能相交。
注:1 ≤ i ≤ n ≤ 1e6 , -32768 ≤ ai ≤ 32767 人数可以为负数。。。。(1<=n<=1000000)

Input

输入m,输入n。后面跟着输入n个ai 处理到 EOF

Output

输出最大和

Sample Input

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output

6
8

Hint

数据量很大,需要scanf读入和dp处理。

代码

#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

const int maxn=1e6+5;
long long m,n,dp[maxn],a[maxn],last[maxn];

int main()
{
    while(scanf("%lld %lld",&m,&n)!=EOF)
	{
        memset(dp,0,sizeof(dp));
        for (int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        long long maxx=INT_MIN;
        for (int j=1; j<=n; j++)
		{
            dp[j]=max(a[j],a[j]+dp[j-1]);
            last[j-1]=maxx;
            maxx=max(maxx,dp[j]);
        }
        for (int i=2; i<=m; i++)
		{
            maxx=INT_MIN;
            for (int j=i; j<=n; j++)
			{
                dp[j]=max(dp[j-1]+a[j],last[j-1]+a[j]);
                last[j-1]=maxx;
                maxx=max(maxx,dp[j]);
            }
        }
        printf("%lld\n",maxx);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值