A
-
题意:
给出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 -
解题思路:
数组a[n]记录数据,标记数组vis记录出现次数,最后遍历出现次数,满足条件输出即可。 -
代码实现:
#include <iostream>
using namespace std;
int a[1000000],vis[1000000];
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<1000000;i++)
{
a[i]=0;
vis[i]=0;
}
int x,xx=(n+1)/2;
while(n--)
{
cin>>x;
if(!vis[x])
{
vis[x]=1;
a[x]=1;
}
else
a[x]++;
}
for(int i=0;i<1000000;i++)
{
if(a[i]>=xx)
cout<<i<<endl;
}
}
}
B
-
题意:
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! -
解题思路:
记录起点和终点,用bfs跑一遍判断能否到达,因为是三维空间所以方向为上下左右前后六个方向。需要注意读入的时候需要以字符串形式读入。 -
代码实现:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char ch[40][40][40];
int b[40][40][40];
int x,y,z;
int sx,sy,sz,ex,ey,ez;
struct node
{
int x;
int y;
int z;
int t;
}A,B;
int check(int i,int j,int k)
{
if(i>=0&&i<x&&j>=0&&j<y&&k>=0&&k<z&&b[i][j][k]==0&&ch[i][j][k]!='#')
return 1;
else return 0;
}
void bfs(int x1,int y1,int z1)
{
queue<node> q;
A.x=x1;A.y=y1;A.z=z1;
A.t=0;
q.push(A);
b[x1][y1][z1]=1;
int cnt=-1;
while(!q.empty())
{
A=q.front();
q.pop();
if(A.x==ex&&A.y==ey&&A.z==ez)
{
cnt=A.t;
break;
}
for(int i=0;i<6;i++)
{
if(i==0)
{
B.x=A.x+1;
B.y=A.y;
B.z=A.z;
}
else if(i==1)
{
B.x=A.x-1;
B.y=A.y;
B.z=A.z;
}
else if(i==2)
{
B.x=A.x;
B.y=A.y+1;
B.z=A.z;
}
else if(i==3)
{
B.x=A.x;
B.y=A.y-1;
B.z=A.z;
}
else if(i==4)
{
B.x=A.x;
B.y=A.y;
B.z=A.z+1;
}
else
{
B.x=A.x;
B.y=A.y;
B.z=A.z-1;
}
if(check(B.x,B.y,B.z))
{
B.t=A.t+1;
b[B.x][B.y][B.z]=1;
q.push(B);
}
}
}
if(cnt==-1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",cnt);
}
int main()
{
while(~scanf("%d %d %d",&x,&y,&z))
{
if(x==0&&y==0&&z==0)
break;
memset(b,0,sizeof(b));
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
scanf("%s",ch[i][j]);
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
for(int k=0;k<z;k++)
{
if(ch[i][j][k]=='S')
{
sx=i;sy=j;sz=k;
}
if(ch[i][j][k]=='E')
{
ex=i;ey=j;ez=k;
}
}
}
}
bfs(sx,sy,sz);
}
return 0;
}
C
-
题意:
东东每个学期都会去寝室接受扫楼的任务,并清点每个寝室的人数。
每个寝室里面有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 -
解题思路:
重点在于找出状态转移方程,转移方程为dp[i][j]=max(dp[i][j−1]+a[j],max(dp[i−1][k])+a[j]),但是发现会超时,故用滚动数组进行优化,还要注意本题数据量较大,输入要用scanf -
代码实现:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e6+5;
int a[maxn];
int d[maxn];
int pre[maxn];
int main()
{
int m,n,i,j,k;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(d,0,sizeof(d));
memset(pre,0,sizeof(pre));
int ans=0;
for(j=1;j<=m;j++)
{
ans=-1e9;
for(i=j;i<=n;i++)
{
d[i]=max(d[i-1],pre[i-1])+a[i];
pre[i-1]=ans;
ans = max(ans,d[i]);
}
}
printf("%d\n",ans);
}
return 0;
}