目录
A - 必做题 - 1
给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少?
输入
本题包含多组数据:
每组数据包含两行。
第一行一个数字N(1<=N<=999999) ,保证N为奇数。
第二行为N个用空格隔开的整数。
数据以EOF结束。
输出
对于每一组数据,你需要输出你找到的唯一的数。
样例输入
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
样例输出
3
5
1
思路
n个数,找出出现次数至少为(n+1)/2的那个数,显然当一组奇数个数的数据中,一个数出现了(n+1)/2次,那个这个数一定是出现次数最多的。
因此直接用num数组记录每个数出现的次数,一旦等于(n+1)/2就直接输出即可。
代码
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
int num[1000010];
while (cin>>n)
{
memset(num, 0, sizeof(num));
int m = (n + 1) / 2;
int temp;
for (int i = 0; i < n; i++)
{
cin >> temp;
num[temp]++;
if (num[temp] == m)
{
cout << temp << endl;
}
}
}
}
B - 必做题 - 2
zjm被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成。
zjm每次向上下前后左右移动一个单位需要一分钟,且zjm不能对角线移动。
空间的四周封闭。zjm的目标是走到空间的出口。
是否存在逃出生天的可能性?如果存在,则需要多少时间?
输入
输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度,R和C分别表示每层空间的行与列的大小。
随后L层,每层R行,每行C个字符。
每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。
zjm的起始位置在’S’,出口为’E’。每层空间后都有一个空行。
L,R和C均为0时输入结束。
输出
每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
样例输入
3 4 5
S….
.###.
.##..
###.#
#####
#####
##.##
##…
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!
思路
就是一个三维的迷宫问题,每次枚举当前点上下左右前后的6个位置,将可以走的放入队列,依次进行,直至到达终点或者无路可走即可。
代码
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
/*B*/
int L, R, C;
char a[35][35][35];
//上下左右前后
int dx[] = { 0,0,-1,1,0,0 };
int dy[] = { 0,0,0,0,1,-1 };
int dz[] = { 1,-1,0,0,0,0 };
int vis[35][35][35];
struct point
{
int x, y, z, t;
point()
{
x = y = z = t = 0;
}
point(int x, int y, int z, int t)
{
this->x = x;
this->y = y;
this->z = z;
this->t = t;
}
};
int main()
{
while (1)
{
cin >> L >> R >> C;
if (L == 0 && R == 0 && C == 0)
{
break;
}
memset(vis, 0, sizeof(vis));
queue<point>q;
while (!q.empty())
{
q.pop();
}
for (int i = 1; i <= L; i++)//z
{
for (int j = 1; j <= R; j++)//x
{
for (int k = 1; k <= C; k++)//y
{
cin >> a[i][j][k];
if (a[i][j][k] == 'S')
{
q.push(point(j, k, i, 0));
vis[i][j][k] = 1;
}
}
}
}
int ans = 0;
while (!q.empty())
{
//cout << "mmmmmmmm" << endl;
point temp = q.front();
q.pop();
if (a[temp.z][temp.x][temp.y] == 'E')
{
ans = temp.t;
break;
}
for (int i = 0; i < 6; i++)
{
point no;
no.x = temp.x + dx[i];
no.y = temp.y + dy[i];
no.z = temp.z + dz[i];
if (no.x >= 1 && no.x <= R && no.y >= 1 && no.y <= C && no.z >= 1 && no.z <= L && vis[no.z][no.x][no.y] == 0 && a[no.z][no.x][no.y] != '#')
{
no.t = temp.t + 1;
q.push(no);
vis[no.z][no.x][no.y] = 1;
}
}
}
if (ans != 0)
{
cout << "Escaped in " << ans << " minute(s)." << endl;
}
else
{
cout << "Trapped!" << endl;
}
}
}
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)
输入
输入m,输入n。后面跟着输入n个ai 处理到 EOF
输出
输出最大和
样例输入
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
样例输出
6
8
Hint
数据量很大,需要scanf读入和dp处理。
思路
用dp[i][j]来表示前j个数取出i段得到的最大的值。
要么j是跟在j-1所在的段的后面
要么j是新的段的开始
状态转移方程为dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j])
考虑到n和m会很大,用二维数组不太现实,因此可将其优化,用一个一维数组b来记录前一个状态中dp的最大值。
代码
#include<iostream>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*C*/
int a[1000010];
int b[1000010];
int dp[1000010];
int main()
{
int m, n;
while (cin >> m >> n)
{
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
memset(b, 0, sizeof(b));
memset(dp, 0, sizeof(dp));
int ans;
for (int i = 1; i <= m; i++)
{
ans = -1e9;
for (int j = i; j <= n; j++)
{
dp[j] = max(dp[j - 1] + a[j], b[j - 1] + a[j]);
b[j - 1] = ans;
ans = max(ans, dp[j]);
}
}
cout << ans << endl;
}
}
D - 选做题 - 1
We give the following inductive definition of a “regular brackets” sequence:
the empty sequence is a regular brackets sequence,
if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
if a and b are regular brackets sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].
【翻译:】我们给出了“正则括号”序列的以下归纳定义:
空序列是一个正则方括号序列,
如果s是正则方括号序列,那么(s)和[s]是正则方括号序列,并且
如果a和b是正则括号序列,则ab是正则括号序列。
没有其他序列是正则括号序列
例如,以下所有字符序列都是正则方括号序列:
(), [], (()), ()[], ()[()]
而下列字符序列不是:
(, ], )(, ([)], ([(]
给定字符a1a2…a n的方括号序列,您的目标是找到作为s的子序列的最长正则方括号序列的长度。也就是说,您希望找到最大的m,以便对于索引i1、i2,…,im,其中1≤i1<i2<…<im≤n,ai1ai2…aim是正则方括号序列。
给定初始序列([([]]),最长的正则方括号子序列是[([]])。
输入
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
【翻译】
输入测试文件将包含多个测试用例。每个输入测试用例只包含一行字符(,),[,和];每个输入测试的长度都在1到100之间(包括1和100)。文件结尾由包含“end”的行标记,不应进行处理。
输出
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
【翻译】
对于每种输入情况,程序应该在一行上打印最长的正则括号子序列的长度。
样例输入
((()))
()()()
([]])
)[)(
([][][)
end
样例输出
6
6
4
0
6
思路
用f[i][j]来记录从第i个括号到第j个括号的最大匹配数目。
如果i和j位置的括号匹配,则f[i][j]=f[i+1][j-1]+2.
枚举即可。
代码
#include<iostream>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*D*/
string str;
int f[1020][1020];
int main()
{
while (cin >> str)
{
if (str == "end")
{
break;
}
int ssize = (int)str.length();
memset(f, 0, sizeof(f));
for (int k = 1; k < ssize; k++)
{
for (int i = 0; i < ssize - k; i++)
{
int j = i + k;
if ((str[i] == '(' && str[j] == ')') || (str[i] == '[' && str[j] == ']'))
{
f[i][j] = f[i + 1][j - 1] + 2;
}
for (int t = i; t < j; t++)
{
f[i][j] = max(f[i][j], f[i][t] + f[t + 1][j]);
}
}
}
cout << f[0][ssize - 1] << endl;
}
}
E - 选做题 - 2
马上假期就要结束了,zjm还有 n 个作业,完成某个作业需要一定的时间,而且每个作业有一个截止时间,若超过截止时间,一天就要扣一分。
zjm想知道如何安排做作业,使得扣的分数最少。
Tips: 如果开始做某个作业,就必须把这个作业做完了,才能做下一个作业。
输入
有多组测试数据。第一行一个整数表示测试数据的组数
第一行一个整数 n(1<=n<=15)
接下来n行,每行一个字符串(长度不超过100) S 表示任务的名称和两个整数 D 和 C,分别表示任务的截止时间和完成任务需要的天数。
这 n 个任务是按照字符串的字典序从小到大给出。
输出
每组测试数据,输出最少扣的分数,并输出完成作业的方案,如果有多个方案,输出字典序最小的一个。
样例输入
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
样例输出
2
Computer
Math
English
3
Computer
English
Math
Hint
在第二个样例中,按照 Computer->English->Math 和 Computer->Math->English 的顺序完成作业,所扣的分数都是 3,由于 English 的字典序比 Math 小,故输出前一种方案。
思路
采用状态压缩的方式,用f数组来记录当前状态最少扣的分数,枚举所有的作业。
每添加一个没有完成的作业,都进行更新作业集合的最少扣分数,完成的时间sum并记录前驱。
最终递归输出即可。
代码
#include<iostream>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*E*/
struct work
{
string name;
int d;
int c;
};
work a[16];
int n, f[1 << 16], pre[1 << 16], sum[1 << 16];
void output(int x)
{
if (x == 0) return;
output(x - (1 << pre[x]));
cout << a[pre[x]].name << endl;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
memset(f, 0, sizeof(f));
memset(pre, 0, sizeof(pre));
memset(sum, 0, sizeof(sum));
for (int i = 0; i < n; i++)
{
cin >> a[i].name >> a[i].d >> a[i].c;
}
for (int i = 1; i <= (1 << n); i++)
{
f[i] = 1e9;
for (int j = n - 1; j >= 0; j--)
{
int temp = 1 << j;
if (!(i & temp))
{
continue;
}
if (f[i] > f[i - temp] + max(0, sum[i - temp] + a[j].c - a[j].d))
{
f[i] = f[i - temp] + max(0, sum[i - temp] + a[j].c - a[j].d);
sum[i] = sum[i - temp] + a[j].c;
pre[i] = j;
}
}
}
cout << f[(1 << n) - 1] << endl;
output((1 << n) - 1);
}
}