您的位置:首页 > 其它

微软2016校园招聘4月在线笔试题目解析

2016-04-24 00:24 489 查看
本文转载并整理自点击打开链接

题目1 : Font Size

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

Steven loves reading book on his phone. The book he reads now consists of N paragraphs and the i-th paragraph contains ai characters.

Steven wants to make the characters easier to read, so he decides to increase the font size of characters. But the size of Steven's phone screen is limited. Its width is W and height is H. As a result, if the font size of characters is S then it can only show
⌊W / S⌋ characters in a line and ⌊H / S⌋ lines in a page. (⌊x⌋ is the largest integer no more than x)

So here's the question, if Steven wants to control the number of pages no more than P, what's the maximum font size he can set? Note that paragraphs must start in a new line and there is no empty line between paragraphs.

输入

Input may contain multiple test cases.

The first line is an integer TASKS, representing the number of test cases.

For each test case, the first line contains four integers N, P, W and H, as described above.

The second line contains N integers a1, a2, ... aN, indicating the number of characters in each paragraph.

For all test cases,

1<=N<=10^3,

1<=W, H, ai<=10^3,

1<=P<=10^6,

There is always a way to control the number of pages no more than P.

输出

For each testcase, output a line with an integer Ans, indicating the maximum font size Steven can set.

样例输入

2

1 10 4 3

10

2 10 4 3

10 10

样例输出

3

2

字母是方的,即宽高一样。每页至少要显示一个字母,即字母大小的最大值为min(W, H)。一个包含p个字母的段落,至少需要占用(p + row - 1)/row行。

AC代码:

#include<algorithm>
#include<iostream>
using namespace std;
int paragraphs[1010];

int solve(int n,int p,int w,int h,int paragraphs[])
{
for(int i=min(w,h);i>=1;--i)
{
int row=w/i;
int col=h/i;
int ans=0;
for(int j=0;j<n;j++) ans+=(paragraphs[j]+row-1)/row;
if(ans<=p*col) return i;
}
}

int main()
{
int tasks;
cin>>tasks;
while(tasks-->0)
{
int n, p, w, h;
cin>>n>>p>>w>>h;
for(int i=0;i<n;++i) cin>>paragraphs[i];
cout<<solve(n,p,w,h,paragraphs)<<endl;
}
return 0;
}
题目2 : 403 Forbidden

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

Little Hi runs a web server. Sometimes he has to deny access from a certain set of malicious IP addresses while his friends are still allow to access his server. To do this he writes N rules in the configuration file which look like:

allow 1.2.3.4/30

deny 1.1.1.1

allow 127.0.0.1

allow 123.234.12.23/3

deny 0.0.0.0/0

Each rule is in the form: allow | deny address or allow | deny address/mask.

When there comes a request, the rules are checked in sequence until the first match is found. If no rule is matched the request will be allowed. Rule and request are matched if the request address is the same as the rule address or they share the same first
mask digits when both written as 32bit binary number.

For example IP "1.2.3.4" matches rule "allow 1.2.3.4" because the addresses are the same. And IP "128.127.8.125" matches rule "deny 128.127.4.100/20" because 10000000011111110000010001100100 (128.127.4.100 as binary number) shares the first 20 (mask) digits
with 10000000011111110000100001111101 (128.127.8.125 as binary number).

Now comes M access requests. Given their IP addresses, your task is to find out which ones are allowed and which ones are denied.

输入

Line 1: two integers N and M.

Line 2-N+1: one rule on each line.

Line N+2-N+M+1: one IP address on each line.

All addresses are IPv4 addresses(0.0.0.0 - 255.255.255.255). 0 <= mask <= 32.

For 40% of the data: 1 <= N, M <= 1000.

For 100% of the data: 1 <= N, M <= 100000.

输出

For each request output "YES" or "NO" according to whether it is allowed.

样例输入

5 5

allow 1.2.3.4/30

deny 1.1.1.1

allow 127.0.0.1

allow 123.234.12.23/3

deny 0.0.0.0/0

1.2.3.4

1.2.3.5

1.1.1.1

100.100.100.100

219.142.53.100

样例输出

YES

YES

NO

YES

NO

一个一个去匹配肯定是要超时的,题意应该是用trie树,只不过也没必要那么麻烦,用STL的map就行。

AC代码:
#include<map>
#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
map<unsigned int,int> _map[35];

int main()
{

int n,m;
scanf("%d%d",&n,&m);
int x1,x2,x3,x4;
for(int i=1;i<=n;i++)
{
static char a[35];
scanf("%s",a);
scanf("%d.%d.%d.%d",&x1,&x2,&x3,&x4);
unsigned t=(x1<<24)+(x2<<16)+(x3<<8)+x4;
char temp;
scanf("%c",&temp);
int masks=32;
if(temp=='/') scanf("%d",&masks);
t>>=(32-masks);
if(masks==0) t=0;
if(a[0]=='a')
{
if(_map[masks][t]==0) _map[masks][t]=i;
}
else
{
if(_map[masks][t]==0) _map[masks][t]=-i;
}
}
for(int i=1;i<=m;i++)
{
scanf("%d.%d.%d.%d",&x1,&x2,&x3,&x4);
unsigned t=(x1<<24)+(x2<<16)+(x3<<8)+x4;
int rules=n+1;
for(int j=0;j<=32;j++)
{
unsigned x=t>>(32-j);
if(j==0) x=0;
if(_map[j].find(x)!=_map[j].end())
{
if(abs(_map[j][x])<abs(rules)) rules=_map[j][x];
}
}
if(rules<0) puts("NO");
else puts("YES");
}
}
题目3 : Demo Day

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You work as an intern at a robotics startup. Today is your company's demo day. During the demo your company's robot will be put in a maze and without any information about the maze, it should be able to find a way out.

The maze consists of N * M grids. Each grid is either empty(represented by '.') or blocked by an obstacle(represented by 'b'). The robot will be release at the top left corner and the exit is at the bottom right corner.

Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can't and keep moving to the bottom until it can't. At the beginning, the robot
keeps moving to the right.

rrrrbb..

...r.... ====> The robot route with broken sensors is marked by 'r'.

...rrb..

...bb...

While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked
and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?

输入

Line 1: N, M.

Line 2-N+1: the N * M maze.

For 20% of the data, N * M<=16.

For 50% of the data, 1<=N, M<=8.

For 100% of the data, 1<=N, M<=100.

输出

The minimum number of grids to be changed.

样例输入

4 8

....bb..

........

.....b..

...bb...

样例输出

1

动态规划求解,由于到每一个点有两种情况,所以用f[i][j][0]表示走到(i,j)方向向下的最小操作数,f[i][j][1]表示走到(i,j)方向向右的最小操作数,得出递推关系,最后取最小值。

AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=105,M=0;
int n,m,f

[2];
char a

;

int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n+1;++i)
{
for(int j=0;j<=m+1;++j) a[i][j]='b';
}
for(int i=1;i<=n;++i)
{
scanf("%s",a[i]+1);
a[i][m+1]='b';
}
//为了方便起见在最右边和最下边加上障碍物
memset(f,16,sizeof(f));
//f的最大值为16
f[1][0][1]=0;
//一开始只能向右走
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
f[i][j][0]=f[i-1][j][0]+(a[i][j]=='b');
f[i][j][1]=f[i][j-1][1]+(a[i][j]=='b');
f[i][j][0]=min(f[i][j][0],f[i][j][1]+(a[i][j+1]!='b'));
f[i][j][1]=min(f[i][j][1],f[i][j][0]+(a[i+1][j]!='b'));
}
}
printf("%d\n",min(f
[m][0],f
[m][1]));
}
}
题目4 : Building in Sandbox

时间限制:30000ms

单点时限:3000ms

内存限制:256MB

描述

Little Hi is playing a sandbox voxel game. In the game the whole world is constructed by massive 1x1x1 cubes. The edges of cubes are parallel to the coordinate axes and the coordinates (x, y, z) of the center of each cube are integers.



At the beginning there is nothing but plane ground in the world. The ground consists of all the cubes of z=0. Little Hi needs to build everything by placing cubes one by one following the rules:

1. The newly placed cube must be adjacent to the ground or a previously placed cube. Two cubes are adjacent if and only if they share a same face.

2. The newly placed cube must be accessible from outside which means by moving in 6 directions(up, down, left, right, forward, backward) there is a path from a very far place - say (1000, 1000, 1000) in this problem - to this cube without passing through ground
or other cubes.

Given a sequence of cubes Little Hi wants to know if he can build the world by placing the cubes in such order.

输入

The first line contains the number of test cases T(1<=T<=10).

For each test case the first line is N the number of cubes in the sequence.

The following N lines each contain three integers x, y and z indicating the coordinates of a cube.

For 20% of the data, 1<=N<=1000, 1<=x, y, z<=10.

For 100% of the data, 1<=N<=100000, 1<=x, y, z<=100.

输出

For each testcase output "Yes" or "No" indicating if Little Hi can place the cubes in such order.

样例提示

In the first test case three cubes are placed on the ground. It's OK.

In the second test case (1, 3, 2) is neither on the ground nor adjacent to previous cubes. So it can't be placed.

In the last test case (2, 2, 1) can not be reached from outside. So it can't be placed.

样例输入

3

3

1 1 1

1 2 1

1 3 1

3

1 1 1

1 2 1

1 3 2

17

1 1 1

1 2 1

1 3 1

2 3 1

3 3 1

3 2 1

3 1 1

2 1 1

2 1 2

1 1 2

1 2 2

1 3 2

2 3 2

3 3 2

3 2 2

2 2 2

2 2 1

样例输出

Yes

No

No

沙盒中搭建筑,基本元素为1x1x1的立方体,现在地面已经铺满了立方体了(z坐标为0),并且立方体没有斜着的。新添加的立方体要求必须挨着一个之前放好的立方体且立方体必须能放进去,即存在一条路径从所有给出方块之外的地方移动到目的地而不穿过已有的立方体。简单的理解就是当这个建筑搭好了,能按照立方体添加顺序的逆序把它拆了。

放置时,检查上下左右前后有没有已放的立方体。若有,则标记该位置放了一个立方体;否则,返回No。当所有立方体放置完毕后,倒序遍历每一个放置的立方体,判断是否存在一条路径可以到达题目中给出的方块之外。若有则标记该位置没有立方体了;否则返回No。在bfs的过程中多传了一个参数进行记忆化搜索,否则会TLE。

AC代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
bool sandbox[102][102][102];
int visited[102][102][102];
int dir[6][3]={{-1,0,0},{0,-1,0},{0,0,-1},{1,0,0},{0,1,0},{0,0,1}};
int min_x,min_y,min_z,max_x,max_y,max_z;
struct Block
{
int x,y,z;
Block(int a,int b,int c):x(a),y(b),z(c){}
Block():x(0),y(0),z(0){}
}blocks[100001];

bool bfs(int a,int b,int c,int n)
{
sandbox[c][a][b]=false;
bool ret=false;
queue<Block> q;
q.push(Block(a,b,c));
visited[c][a][b]=n;
while(!q.empty())
{
int x=q.front().x;
int y=q.front().y;
int z=q.front().z;
q.pop();
if(x<min_x||y<min_y||z<min_z||x>max_x||y>max_y||z>max_z)
{
ret=true;
break;
}
//如果bfs的点超出了界限说明可以找到从外部到达的路径
for(int i=0;i<6;++i)
{
int nz=z+dir[i][0];
int nx=x+dir[i][1];
int ny=y+dir[i][2];
if(!sandbox[nz][nx][ny]&&n!=visited[nz][nx][ny])
{
//如果这个点之前被访问过可以直接停止bfs
if(visited[nz][nx][ny])
{
ret=true;
break;
}
else
{
visited[nz][nx][ny]=n;
q.push(Block(nx,ny,nz));
}
}
}
}
return ret;
}

bool unbuild(int n)
{
memset(visited,0,sizeof(visited));
while(n-->0)
{
if(!bfs(blocks
.x,blocks
.y,blocks
.z,n+1)) return false;
}
return true;
}

bool build(int x,int y,int z)
{
if(sandbox[z][x][y]) return false;
int i;
for(i=0;i<6;++i)
{
if(sandbox[z+dir[i][0]][x+dir[i][1]][y+dir[i][2]]) break;
}
if(i==6) return false;
//放置时检查上下左右前后有没有已放的立方体
sandbox[z][x][y]=true;
min_x=min(min_x,x);
max_x=max(max_x,x);
min_y=min(min_y,y);
max_y=max(max_y,y);
min_z=min(min_z,z);
max_z=max(max_z,z);
return true;
}

int main()
{
int t;
cin>>t;
for(int i=0;i<t;++i)
{
memset(sandbox,false,sizeof(sandbox));
memset(sandbox[0],true,sizeof(sandbox[0]));
//地面已经铺满了
int n;
cin>>n;
bool res=true;
for(int j=0;j<n;++j)
{
int x,y,z;
cin>>x>>y>>z;
blocks[j].x=x;
blocks[j].y=y;
blocks[j].z=z;
if(!res) continue;
res=build(x,y,z);
}
res=res?unbuild(n):false;
cout<<(res?"Yes":"No")<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: