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. Java第十天,多态

    多态 一.多态的定义: 一个对象拥有多种形态,这就是对象的多态性.也就是说多态针对的是对象.多态的前提是接口和继承(C++中实行多继承,不存在接口). 二.多态在代码中的形式: 父类 对象名 = ne ...

  2. CSS躬行记(5)——渐变

    渐变是由两种或多种颜色之间的渐进过渡组成,它是一种特殊的图像类型,分为线性渐变和径向渐变,这两类渐变还会细分为单次和重复两种.渐变图像与传统图像相比,它的优势包括占用更少的字节,避免额外的服务器请求, ...

  3. Mysql fundamental knowledge

    Mysql 5.1, 5.5 are more stable than other versions. postgresql has more strict "sql standard &q ...

  4. 从谷歌面试翻车到offer收割的心路历程

    首先声明,这只是我的播客随感,其中无法避免有一些个人色彩的见解,请不要在意,我尊敬任何的互联网公司,尊敬研究生期间的老师同学,我只希望给在求学路上的CS同学一些启发. 先介绍一下背景,我是ACM铜牌退 ...

  5. 谁说.NET不适合搞大数据,机器学习、人工智能

    SciSharp Stack SciSharp STACK: https://scisharp.github.io/SciSharp/ 基于.NET的开源生态系统,用于数据科学.机器学习和AI. Sc ...

  6. Mysql 错误 Connection is read-only 解决方式

    环境Spring+Mybatis <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.s ...

  7. matlab计算样本熵

    计算14通道得脑电数据吗,将得出的样本熵插入Excel表格 a = zeros(1,14); b = a'; for i =1:14 b(i) = SampEn(d1_1(i,1:3000),2,0. ...

  8. MergeSort归并排序和利用归并排序计算出数组中的逆序对

    首先先上LeetCode今天的每日一题(面试题51. 数组中的逆序对): 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. ...

  9. [nodejs] 同步/异步创建多层目录

    背景 有时项目里需要同时创建多层目录的功能,但低版本的nodejs并没有提供快捷的api 尽管在v10.12.0版本 mkdir() 第二个参数支持recursive 参数,为true时能递归创建,但 ...

  10. 【认证与授权】2、基于session的认证方式

    这一篇将通过一个简单的web项目实现基于Session的认证授权方式,也是以往传统项目的做法. 先来复习一下流程 用户认证通过以后,在服务端生成用户相关的数据保存在当前会话(Session)中,发给客 ...