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 ...
随机推荐
- vector数组的相关知识
Vector 类实现了一个动态数组.和 ArrayList 很相似,但是两者是不同的: Vector 是同步访问的. Vector 包含了许多传统的方法,这些方法不属于集合框架. Vector 主要用 ...
- C++ stringstream(强制类型转换)
#include <iostream>#include <string>#include <sstream>using namespace std;int main ...
- NumPy学习1:基本概念
NumPy的数组类被称作 ndarray .通常被称作数组.注意numpy.array和标准Python库类array.array并不相同,后者只处理一维数组和提供少量功能.更多重要ndarray对象 ...
- 六、路由详细介绍之动态路由RIP(了解一下就行)
动态路由分为距离矢量路由(RIP)和链路状态(OSPF和ISIS) 一.离矢量路由协议-RIP RIP协议现在基本上被淘汰. RIP动态路由协议工作原理,如上图: R12中有192.168.1.0和1 ...
- 子域名爆破工具:OneForALL
0x00 简介 OneForAll是一款功能强大的子域收集工具 0x01 下载地址 码云: https://gitee.com/shmilylty/OneForAll.git Github: http ...
- HashMap主要方法源码分析(JDK1.8)
本篇从HashMap的put.get.remove方法入手,分析源码流程 (不涉及红黑树的具体算法) jkd1.8中HashMap的结构为数组.链表.红黑树的形式 (未转化红黑树时) (转 ...
- mongodb的远程连接和配置(阿里ECS)
1.) 首先安装mongodb 2.)配置mongodb.conf bind_ip = 0.0.0.0 port= dbpath=/root/mongodb/mongodb-linux-x86_64- ...
- 2020i春秋新春战疫
简单的招聘系统 登陆这里就可以注入 查询这里也可以注入 从登陆这里注入把 爆破数据库名 爆破表名 列名 flag 就很奇怪跑出来的东西 重开容器跑一遍列,估计是flaaag.后面可能是发生了502 再 ...
- ios快捷指令编程尝试
最近,,,啊好几个月了,发现这个ios的快捷指令很好玩 原生就提供了不少功能 用来练习编程思维是十分有用啊...) 其次呢,还可以使用外接的功能对原有的功能进行拓展,比如api借口啊,ssh执行程序啊 ...
- [PHP]PHP设计模式:单例模式
单例模式(职责模式): 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用new操作符 ...