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
思路
注意中间不能因为该数是唯一的,就一遇到该数就直接输出并break,因为还要输入后面的数
Answer
#include <iostream>
#include<string.h>
using namespace std;
const int maxn=1e6+100;
int n,a[maxn];
int s[maxn];
int main() {
while(cin>>n){
memset(s,0,sizeof s);
int ans;
for(int i=0;i<n;i++){
cin>>a[i];
s[a[i]]++;
if(s[a[i]]>=(n+1)/2){
ans=a[i];
}
}
cout<<ans<<endl;
}
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是进行更新,将权重更新为之前步数的总和
还要注意边界的判断
Answer
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 30000;
int l, r, c;
char a[35][35][35];
bool vis[35][35][35];
struct node {
int x, y, z,w;
};
int dx[6] = { 1,-1,0,0,0,0 };
int dy[6] = { 0,0,1,-1,0,0 };
int dz[6] = { 0,0,0,0,1,-1 };
int main() {
while (1) {
cin >> l >> r >> c;
if (l == r && l == c && l == 0)break;
else {
memset(a, '#', sizeof a);
memset(vis, 0, sizeof vis);
queue<node> q;
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= r; j++) {
char s[35];
cin >> s;
for (int k = 1; k <= c; k++) {
a[i][j][k] = s[k - 1];
if (a[i][j][k] == 'S') {
q.push({ i,j,k ,0});
vis[i][j][k] = 1;
//cout << q.front().x;
}
}
}
//getchar();
}
//bfs
bool flag = 0;
int ans=0;
while (!q.empty()) {
node t = q.front();
q.pop();
int x = t.x, y = t.y, z = t.z;
if (a[x][y][z] == 'E') {
ans = t.w;
break;
}
for (int i = 0; i < 6; i++){
if (x + dx[i] > l || y + dy[i] > r || z + dz[i] > c)continue;
if (a[x + dx[i]][y + dy[i]][z + dz[i]] != '#' && vis[x + dx[i]][y + dy[i]][z + dz[i]] == 0) {
q.push({ x + dx[i],y + dy[i],z + dz[i],t.w+1 });
vis[x + dx[i]][y + dy[i]][z + dz[i]] = 1;
}
if (a[x + dx[i]][y + dy[i]][z + dz[i]] == 'E')
flag = 1;
}
}
if (flag)cout << "Escaped in " << ans<< " minute(s)." << endl;
else cout << "Trapped!" << endl;
}
}
return 0;
}
【dp】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
Output
输出最大和
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
Hint
数据量很大,需要scanf读入和dp处理。
思路
最大m区间和问题
dp[i][j]表示取第j个数,且前j个数共分成i个区间的最大和,状态转移方程为:dp[i][j]=max(dp[i][j-1]+a[j], dp[i-1][k]+a[j]) ,其中k从(i-1)到(j-1)。
dp[i][j-1]+a[j]表示在取第j-1个数的基础上,加上第j个数,没有增加新的区间;dp[i-1][k]+a[j]表示已经有了i-1个区间,加入第j个数后产生了一个新的区间,所以k应该从i-1到j-1取值。
本题还需要进行空间优化,在更新dp[i][j-1]+a[j]并没有用到第i-1层,f[i-1][k]+a[j]用到了,可以在i-1层更新的过程中,用pre数组将其记录下来,这样可将二维降为一维
Answer
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e6+100;
const int inf=-1e9;//最小值
int n,m,maxx,dp[maxn],a[maxn],pre[maxn];
int main(){
while(scanf("%d %d",&m,&n)!=EOF){
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
maxx=inf;
for(int j=i;j<=n;j++){
dp[i-1]=inf;
dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]);
pre[j-1]=maxx;//pre[j]表示前一个状态中1–j的dp的最大值
if(dp[j]>maxx)maxx=dp[j];
}
}
printf("%d\n",maxx);
}
return 0;
}
【区间dp】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 [([])].
Input
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.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6
题目简述:
给定一串()[]的字符串,从中选取最长的能够实现“正则”(括号合法匹配)的序列,输出序列长度。多组数据,end结束。
思路
区间dp
dp[i][j]表示符合题意的最长子序列的长度。首先枚举区间长度l,因为枚举过程中,较长的区间的结果需要依靠其中较短的区间的结果。枚举过程中,若i恰好满足s[i] == ’ (’ s[j] == ')‘或者s[i] == ’ [’ s[j] == ‘]’,则dp[i][j]=dp[i+1][j-1]+2.然后区间内部再依靠区间内更短区间的结果进行转移
Answer
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn=1e3+10;
int n,dp[maxn][maxn];
string s;
int main(){
while(1){
cin>>s;
if(s=="end") break;
n=s.length();
memset(dp,0,sizeof(dp));
for(int l=1;l<n;l++)
for(int i=0;i+l<n;i++){
int j=i+l;
if((s[i]=='['&&s[j]==']')||(s[i]=='('&&s[j]==')')){
if(j==i+1) dp[i][j]=2;
else dp[i][j]=dp[i+1][j-1]+2;
}
for(int k=i;k<j;k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
}
cout<<dp[0][n-1]<<endl;
}
return 0;
}