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的更多相关文章

  1. 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 ...

  2. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  3. WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

    http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...

  4. Amazon Interview | Set 27

    Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...

  5. Java Swing interview

    http://www.careerride.com/Swing-AWT-Interview-Questions.aspx   Swing interview questions and answers ...

  6. Pramp - mock interview experience

    Pramp - mock interview experience   February 23, 2016 Read the article today from hackerRank blog on ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. [译]Node.js Interview Questions and Answers (2017 Edition)

    原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...

  9. WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】

    http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...

  10. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

随机推荐

  1. 一天学一个Linux命令:第二天 cd pwd

    文章更新于:2020-03-08 注:本文参照 man pwd 手册,并给出使用样例. 文章目录 一.命令之 `cd` 和 `pwd` 1.命令介绍 2.语法格式 3.使用样例 4.pwd 参数 5. ...

  2. Java第五天,API常用类,静态(static)、集合(ArrayList)、日期(Date)、日历(Calendar)的使用方法

    上文中我们学习到了Random随机数类和ArrayList<E>集合.这两个知识点都是经常用到的,那么除了这两个外,还有哪些知识点是我们所必须掌握的呢? static 使用static需要 ...

  3. Oacle学习-01Oracle的安装

    @ 目录 下载Oracle 安装Oracle 安装plsqldeveloper客户端 下载Oracle 官方下载地址:Oracle下载 网盘地址:链接:https://pan.baidu.com/s/ ...

  4. Python库-Matplotlib

    Matplotlib官网https://matplotlib.org,Matplotlib是一个Python的2D绘图库. 可视化是整个数据分析的关键辅助工具,可以清晰的理解数据. 折线图(用于显示数 ...

  5. TP字段加一操作

    经常有需要对某个数据表的计数字段进行加减操作,我们来看下在ThinkPHP中的具体使用办法.最简单的,使用下面方法对score自动加1: M('User')->where('id=5')-> ...

  6. 事务的传播属性及隔离级别 Spring

    事务的传播属性(Propagation) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none exis ...

  7. 算法竞赛 从c到c++3

    const 常指针,指向固定位置,不能再次修改指向的位置,需要初始化,const 加在“*”号后面,名称前面,例如 int *const p: 指向常量的指针,不能修改指向地址的内容,相当于常引用,c ...

  8. 学习笔记分享之汇编---3. 堆栈&标志寄存器

    前言:   此文章收录在本人的<学习笔记分享>分类中,此分类记录本人的学习心得体会,现全部分享出来希望和大家共同交流学习成长.附上分类链接:   https://www.cnblogs.c ...

  9. 06-移动web之flex布局

    一.基本概念 flex布局又叫伸缩布局 .弹性布局 .伸缩盒布局 .弹性盒布局 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. ...

  10. 一文回顾Reids五大对象(数据类型)

    Redis 是一个高性能的分布式内存型数据库,在国内外各大互联网公司中都有着广泛的使用,即使是一些非互联网公司中也有着非常重要的适用场景,所以对 Redis 的掌握也成为后端工程师必备的基础技能,在面 ...