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. readthedocs网托管持多语言文档

    希望在readthedocs上创建支持多语言的文档,效果类似: 通过语言选项,可以切到到不同的语言版本:实现这个目标包含两个主要步骤: 在本地对文档进行翻译 在readthedocs.org上配置翻译 ...

  2. MyBatis(十):Mybatis缓存的重要内容

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  3. matplotlib TransformNode类

    TransformNode 是所有参与变换的类和所有需要无效自己或祖先的类的基类 方法: __init__(shorthand_name=None): 参数 [shorthand_name]: 别名 ...

  4. Js 事件原理与事件委托

    事件原理三阶段 捕获(有外向内).目标.冒泡(由内向外) 事件冒泡(event bubbling),即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点( ...

  5. 动态规划_基础_最长公共子序列_多种方法_递归/dp

    D: 魔法少女资格面试 题目描述 众所周知,魔法少女是一个低危高薪职业.随着近年来报考魔法少女的孩子们越来越多,魔法少女行业已经出现饱和现象!为了缓和魔法少女界的就业压力,魔法少女考核员丁丁妹决定增加 ...

  6. 【Java】 语言基础习题汇总 [2] 面向对象

    30 面向对象的三条主线和面向对象的编程思想? 类与类的成员 : 属性.方法.构造器.代码块.内部类. 面向对象的三大特征:封装.继承.多态[如果还有一个,那就是抽象] 关键字:this.super. ...

  7. 引用传参与reference_wrapper

    本文是<functional>系列的第3篇. 引用传参 我有一个函数: void modify(int& i) { ++i; } 因为参数类型是int&,所以函数能够修改传 ...

  8. 4.加密与token(node+express)

    一. 敏感数据加密1.安装并引入中间件     npm install utility     const utils = require('utility')2.加密方法     function ...

  9. testNG 断言

    testNG提供一个Assert类,来判断输出值是否与预期值一致,Assert常用的方法有: Assert.assertEquals():此方法可以有两个参数值,也可以有3个参数值,参数的顺序是 ac ...

  10. python 获取的json字符串取值

    获取到的json字符串,然后对其取值 {u'result': {u'10.10.10.100': {u'status': u'OK', u'msg': u"{'listen': {'': s ...