INTERVIEW #4
120min, 5题。本菜鸡怒跪。
1、变身程序员
(读取时可以按行读取,直到读到空行为止,再对读取过的所有行做转换处理)
输出描述:
如果能将所有的产品经理变成程序员,输出最小的分钟数;
如果不能将所有的产品经理变成程序员,输出-1。
示例1:
输入:
0 2
1 0
输出:
-1
示例2:
输入:
1 2 1
1 1 0
0 1 1
输出:
3
示例3:
输入:
1 2
2 1
1 2
0 1
0 1
1 1
输出:
4
此题与https://leetcode.com/problems/rotting-oranges/类似。
基本思想就是将所有的程序员入队,BFS所有的产品经理,最后检查是否还有产品经理存在。
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue> using namespace std; struct node {
int x, y;
int time;
node() {}
node(int xx, int yy, int t) :x(xx), y(yy), time(t) {}
}; queue<node> q; int dir[][] = { {,-},{,},{-,},{,} }; int main()
{
int grid[][];
int row = , col = ;
string str; /*按行读取输入*/
while (getline(cin, str))
{
col = ;
for (int i = ; str[i]; i++)
{
if (str[i] != ' ')
{
grid[row][col++] = str[i] - '';
}
} row++;
} for(int i = ;i < row;i++)
for (int j = ; j < col; j++)
{
if (grid[i][j] == )
{
//将所有程序员入队
q.push(node(i, j, ));
}
} node s;
while (!q.empty())
{
s = q.front(); /*四个方向遍历*/
for (int i = ; i < ; i++)
{
int newx = s.x + dir[i][];
int newy = s.y + dir[i][]; //没有越界并且找到一枚产品经理
if (newx >= && newx < row && newy >= && newy < col && grid[newx][newy] == )
{
grid[newx][newy] = ;
q.push(node(newx, newy, s.time + ));
}
}
q.pop();
} for (int i = ; i < row; i++)
for (int j = ; j < col; j++)
{
if (grid[i][j] == )
{
printf("-1\n");
return ;
}
} printf("%d\n", s.time); return ;
}
2、特征提取
示例:
输入:
4
2 1 1 2 2
输出:
说明:
特征<,>在连续的帧中出现3次,相比其他特征连续出现的次数大,所以输出3
备注:
如果没有长度大于2的特征运动,返回1
可以使用pair存储当前特征,使用map存储当前特征上一次出现的行数以及当前特征连续出现的长度。
还是对C++不熟唉
#define _CRT_SECURE_NO_WARNINGS #include <cstdio>
#include <utility>
#include <map>
#include <algorithm> using namespace std; int main()
{
int N, M, fea_num, res;
scanf("%d", &N); while (N--)
{
scanf("%d", &M);
res = ;
pair<int, int> cur;
//当前特征上一次出现的行数以及连续出现的长度
map<pair<int, int>, int> lastIndex, length;
for (int i = ; i < M; i++)
{
scanf("%d", &fea_num);
for (int j = ; j < fea_num; j++)
{
scanf("%d%d", &cur.first, &cur.second);
if (lastIndex[cur] == i)
{
length[cur]++;
}
else
{
length[cur] = ;
}
lastIndex[cur] = i + ;
res = max(res, length[cur]);
}
}
if (res <= )
printf("1\n");
else
printf("%d\n", res);
} return ;
}
3、机器人跳跃
示例1:
输入:
输出:
示例2:
输入:
输出:
示例3:
输入:
输出:
备注:
<= N <= ^
<= H(i) <= ^
据说是小学数学,还想了半天。
根据题意可推出:$dp[k + 1] = 2*dp[k] - H[k + 1]$
#define _CRT_SECURE_NO_WARNINGS #include <cstdio>
#include <cmath>
#include <vector> using namespace std; int main()
{
int N;
scanf("%d", &N);
vector<int> H(N + ); for (int i = ; i < N; i++)
{
scanf("%d", &H[i + ]);
} vector<int> dp(N + ); //dp[k]表示从第k级开始需要的能量 for (int i = N - ; i >= ; i--)
{
dp[i] = ceil((dp[i + ] + H[i + ]) / 2.0);
} printf("%d\n", dp[]); return ;
}
4、毕业旅行问题
示例:
输入:
输出:
典型的TSP问题,据说动态规划能够得到理论最优解,然而本渣看不懂状态转移方程。
贪心算法:从某城市出发,每次在未到达的城市中选择最近的一个,直到遍历完所有城市,最后回到出发地。
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> using namespace std; #define INF 1<<30; int main()
{
int n, m[][], res = ;
int edge_count = , flag[] = { , };
int cur = , next;
scanf("%d", &n); for(int i = ;i < n;i++)
for (int j = ; j < n; j++)
{
scanf("%d", &m[i][j]);
} while (edge_count < n)
{
int min = INF;
for (int j = ; j < n; j++)
{
if (!flag[j] && m[cur][j] && m[cur][j] < min)
{
next = j;
min = m[cur][j];
}
}
res += m[cur][next];
flag[next] = ;
edge_count++;
cur = next;
} res += m[cur][]; return ;
}
5、过河
示例:
输入:
输出:
每次过河只能2个或3个人,这种过河问题遵循“能者多劳”原则,即花费时间少的人折返去接其他人。
#define _CRT_SECURE_NO_WARNINGS #include <cstdio>
#include <algorithm> using namespace std; int a[], dp[]; int main()
{
int n, N;
scanf("%d", &N); while (N--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
} sort(a, a + n);
dp[] = a[], dp[] = a[];
for (int i = ; i <= n; i++)
{
//前i个人过河的最短时间
dp[i] = min( dp[i - ] + a[] + a[i - ],dp[i - ] + a[] + a[i - ] );
} printf("%d\n", dp[n]);
} return ;
}
INTERVIEW #4的更多相关文章
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】
http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...
- Amazon Interview | Set 27
Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- Pramp - mock interview experience
Pramp - mock interview experience February 23, 2016 Read the article today from hackerRank blog on ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- [译]Node.js Interview Questions and Answers (2017 Edition)
原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
随机推荐
- Android 启动一个Activity的几种方式
启动一个Activity的几种方式在Android中我们可以通过下面两种方式来启动一个新的Activity,注意这里是怎么启动,而非启动模式!分为显示启动和隐式启动! 1.显式启动,通过包名来启动,写 ...
- Nuxt内导航栏的两种实现方式
方式一 | 通过嵌套路由实现 在pages页面根据nuxt的路由规则,建立页面 1. 创建文件目录及文件 根据规则,如果要创建子路由,子路由的文件夹名字,必须和父路由名字相同 所以,我们的文件夹也为i ...
- 19cRAC增删节点
由于测试环境打19c补丁失败,导致节点2 d2 crs无法启动!打补丁没有有效备份,亲人两行泪 先删再加节点. 一.删除节点 1.1 删除实例 参考 https://www.cnblogs.com/ ...
- intelij idea 和 eclipse 使用上的区别
一.项目创建区别 使用基于IntelliJ的IDE,都会对project和module的关系比较糊涂.用简单的一句话来概括是: IntelliJ系中的Project相当于Eclipse系中的works ...
- TP基础
一.目录结构 解压缩到web目录下面,可以看到初始的目录结构如下: www WEB部署目录(或者子目录)├─index.php 入口文件├─README.md README文件├─Applicatio ...
- Spire.Cloud 私有化部署教程(二)- Ubuntu 18.04 系统
本教程主要介绍如何在Ubuntu 18.04系统上实现Spire.Cloud私有化部署.CentOS 7系统部署请参考 这篇教程. 详细步骤如下: 一.环境配置 1.关闭防火墙 1)首先查看防火墙状态 ...
- python3(三十四)unitTest
""" 单元测试 """ __author__on__ = 'shaozhiqi 2019/9/23' class Dict(dict): ...
- 用Python画的,5 种非传统的可视化技术,超炫酷的动态图
数据可以帮助我们描述这个世界.阐释自己的想法和展示自己的成果,但如果只有单调乏味的文本和数字,我们却往往能难抓住观众的眼球.而很多时候,一张漂亮的可视化图表就足以胜过千言万语.本文将介绍 5 种基于 ...
- python的多线程、多进程、协程用代码详解
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:刘早起早起 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- 后端code review finished 12-28
今天只有天赋和士杰有相应的后端代码的code review工作,因为并没有召开daily scrum.只是天赋和士杰对后端所有的代码进行了review,对代码进行了整理并删除了一些残留的0 refre ...