网易2018.03.27算法岗,三道编程题100%样例AC题解
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/8660814.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~
1. 自定义排序
第一题是第一行给出n(1<=n<=100),表示下面有n行,每行A(0<=A<24)和B(0<=B<60),表示定的闹钟为AhBmin。
接下来给定X,表示小明从起床到教室需要X分钟,最后一行给出A(0<=A<24)和B(0<=B<60)表示上课时间AhBmin。
求问小明赶在上课前,能够定的最晚闹铃时间为多少,样例保证必定有一个符合要求。
对闹铃排序,按照A从小到大排序,当A相同的时候,B从小到大排序,然后从最后一个往回遍历,找到距离上课时间>=Xmin中的闹铃时间,输出即可。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- /*
- 3
- 5 0
- 6 0
- 7 0
- 59
- 6 59
- */
- using namespace std;
- const int maxn=;
- struct Node{
- int h;
- int m;
- bool operator<(const Node tmp)const{
- if(h==tmp.h)
- return m<tmp.m;
- else
- return h<tmp.h;
- }
- };
- Node clocks[maxn];
- int n;
- int main()
- {
- int a,b;
- scanf("%d",&n);
- for(int i=;i<n;i++){
- scanf("%d %d",&clocks[i].h,&clocks[i].m);
- }
- sort(clocks,clocks+n);
- int x;
- scanf("%d",&x);
- scanf("%d %d",&a,&b);
- for(int i=n-;i>=;i--){
- if(clocks[i].h>a)
- continue;
- if(clocks[i].h==a){
- if(b-clocks[i].m>=x){
- printf("%d %d\n",clocks[i].h,clocks[i].m);
- break;
- }
- }
- else{
- if(-clocks[i].m+(a-clocks[i].h-)*+b>=x){
- printf("%d %d\n",clocks[i].h,clocks[i].m);
- break;
- }
- }
- }
- return ;
- }
2. 离散化+二分查找
对于一个矩阵,在坐标系内,左下角坐标(x1,y1),右上角坐标(x2,y2),现在给出n个矩阵的坐标,问重叠区域矩阵最多的个数?如果没有矩阵重叠,输出1。
输入样例,第一行n,接下来分别为n个x1,n个y1,n个x2,n个y2。1<=n<=50,-10^9<=xi,yi<=10^9。
很明显,n的范围只有50,数据量很小,但是x和y很大,需要离散化处理,这样的话最多200个不同的值。
处理之后,对于第i个矩阵,遍历它所在的范围,cnt[i][j]++即可。最后输出cnt最大的那个。如果cnt都为0,即没有矩阵,也就没有重叠,也输出1。
这里注意,一开始我在遍历的时候,下面for循环,起始条件没有+1,这样的话还有10%样例是过不了的。应该是统计边,而不是统计点,因为对于[(0,0),(0,0)]是构不成矩阵的。
- for(int k=lx+;k<=rx;k++){
- for(int p=ly+;p<=ry;p++){
- cnt[k][p]++;
- }
- }
完整代码:
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- using namespace std;
- /*
- 2
- 0 90
- 0 90
- 100 200
- 100 200
- */
- const int maxn=;
- int n;
- int xx1[maxn],xx2[maxn],yy1[maxn],yy2[maxn];
- int idx=;
- int a[maxn];
- int hash_x[maxn];
- int cnt[maxn][maxn];
- int binarySearch(int *a,int t,int n){
- int l=,r=n-;
- int mid;
- while(l<=r){
- mid=(l+r)>>;
- if(a[mid]==t)
- return mid;
- if(t<a[mid])
- r=mid-;
- else
- l=mid+;
- }
- return -;
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=;i<n;i++){
- scanf("%d",&xx1[i]);
- a[i*+]=xx1[i];
- }
- for(int i=;i<n;i++){
- scanf("%d",&yy1[i]);
- a[i*+]=yy1[i];
- }
- for(int i=;i<n;i++){
- scanf("%d",&xx2[i]);
- a[i*+]=xx2[i];
- }
- for(int i=;i<n;i++){
- scanf("%d",&yy2[i]);
- a[i*+]=yy2[i];
- }
- //离散化,将-10^9~10^9的数据映射为0~200以内,因为最多出现4*50不同的数,映射值即为索引。
- sort(a,a+*n);
- hash_x[]=a[];
- idx=;
- for(int i=;i<*n;i++){
- if(a[i]!=a[i-]){
- hash_x[idx]=a[i];
- idx++;
- }
- }
- memset(cnt,,sizeof(cnt));
- for(int i=;i<n;i++){
- int lx,ly,rx,ry;
- //二分查找对应离散化后的索引。
- lx=binarySearch(hash_x,xx1[i],idx);
- ly=binarySearch(hash_x,yy1[i],idx);
- rx=binarySearch(hash_x,xx2[i],idx);
- ry=binarySearch(hash_x,yy2[i],idx);
- for(int k=lx+;k<=rx;k++){
- for(int p=ly+;p<=ry;p++){
- cnt[k][p]++;
- }
- }
- }
- int ans=;
- for(int i=;i<=idx;i++){
- for(int j=;j<=idx;j++){
- ans=max(ans,cnt[i][j]);
- }
- }
- if(ans==)
- ans=;
- printf("%d\n",ans);
- return ;
- }
3. dfs+剪枝
第一行n和w,接下来有n个零食的重量v[i],0<v[i]<10^9,问你在背包重量为w的情况下,最多能有几种装法?背包重量为0也算一种。
比如说
3 8
1 2 3
因为总的背包容量大于三个总重量,所以三个每个都可选可不选,共计2^3种。
先对零食的重量从小到大排序,然后从最后一个开始,零食索引为idx,背包剩余容量为left,现有方案总数为tot,初始为0。
1. 若idx<=0或者left<=0,表示没得选了,只有就这一种方案,所以tot++即可。
2. 若idx之前所有零食的总重量<=left,那么很显然,该方案数总共为2^idx,加到tot上即可。
3. 若v[idx]<=left,那么我可以放第idx个零食,方案数即为dfs(idx-1,left-v[idx])。
4. 当然不管怎样,我也可以选择不放第idx个零食,方案数即为dfs(idx-1,left)。
注意,因为零食重量的范围,所以代码里的two数组、sum数组、tot为long long,才不会溢出,否则样例会有不过。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- using namespace std;
- const int maxn=;
- int n;
- int v[maxn];
- long long sum[maxn]; //sum[i]统计v[1]~v[i]的和
- int w;
- long long tot=;
- long long two[]; //two[i]即为2^i值
- //dfs搜索所有的方案数
- void dfs(int idx,int left){
- if(idx<= || left<=){
- tot+=;
- return;
- }
- if(sum[idx]<=left){
- tot+=two[idx];
- return;
- }
- if(left>=v[idx]){
- dfs(idx-,left-v[idx]);
- }
- dfs(idx-,left);
- }
- int main()
- {
- two[]=;
- for(int i=;i<=;i++)
- two[i]=two[i-]*;
- scanf("%d %d",&n,&w);
- for(int i=;i<=n;i++){
- scanf("%d",&v[i]);
- }
- sort(v+,v+n+);
- sum[]=;
- for(int i=;i<=n;i++){
- sum[i]=sum[i-]+v[i];
- }
- dfs(n,w);
- printf("%lld\n",tot);
- return ;
- }
网易2018.03.27算法岗,三道编程题100%样例AC题解的更多相关文章
- 【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】
--------– 2018.03.27 更新--------- 便携版已更新,点此获取便携版 已知BUG:中文目录无法正常调试 用于cpptools 0.15.0插件的配置文件更新 新的launch ...
- 2017 CVTE春招内推专场 C/C++软件开发岗笔试编程题
先来一波吐槽:选择题全是不定项选择,考的内容在我看来,"反正我接受唔到咯". 比如: 1.Windows操作系统某个通信机制(具体题目忘了,反正答案我选了个熟悉的名词"消 ...
- 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】
openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...
- 2018/03/27 每日一个Linux命令 之 cron
Cron 用于配置定时任务. -- 环境为 Ubuntu16-04 -- 先说说怎么配置一个简单的定时任务.直观的可以看到效果. 之前在网上查找资料,对Shell编程不熟悉的实在是很头疼,走了不少弯路 ...
- 2018.03.27 pandas duplicated 和 replace 使用
#.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...
- 2018.03.27 pandas concat 和 combin_first使用
# 连接和修补concat.combine_first 沿轴的堆叠连接 # 连接concatimport pandas as pdimport numpy as np s1 = pd.Series([ ...
- 2018.03.27 python pandas merge join 使用
#2.16 合并 merge-join import numpy as np import pandas as pd df1 = pd.DataFrame({'key1':['k0','k1','k2 ...
- 网易2019校招C++研发工程师笔试编程题
丰收? (忘了题目了QAQ) 题目描述: 又到了丰收的季节,恰逢小易去牛牛的果园里游玩. 牛午常说他对整个果园的每个地方都了如指掌,小易不太相信, 所以他想考考牛牛. 在果园里有N堆苹果,每堆苹果的数 ...
- Python基础编程题100列目录
实例001:数字组合 实例002:"个税计算" 实例003:完全平方数 实例004:这天第几天 实例005:三数排序 实例006:斐波那契数列 实例007:copy 实例008:九 ...
随机推荐
- php中编码转换方法
php里经常用到编码转换,在这记录一个常用的编码转换方法,字符串.数组.对象都可以使用,使用了递归来解决,比较普通 /* * php中编码转换 * @param $param 需要转换的数据 * @p ...
- D-Link DIR-600 - Authentication Bypass
#Exploit Title: D-Link DIR-600 - Authentication Bypass (Absolute Path Traversal Attack) # CVE - http ...
- 个人技术博客——linux服务器配置以及flask框架
本次的软件工程实践,我负责我们组后台服务的搭建,我选用了bandwagon的服务器,安装的是Debian GNU/Linux,全程在root用户下操作,后端服务是用python的flask框架,数据库 ...
- python框架面试题联系
1.对 MVC,MVT 解读的理解? M:Model,模型,和数据库进行交互 V:View,视图,负责产生 Html 页面 C:Controller,控制器,接收请求,进行处理,与 M 和 V 进行交 ...
- 1095 Anigram单词
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词 ...
- M600 (1)飞行注意事项
- 数据库连性池性能测试(hikariCP,druid,tomcat-jdbc,dbcp,c3p0)
文章转自 https://www.tuicool.com/articles/qayayiM 摘要: 本文主要是对这hikariCP,druid,tomcat-jdbc,dbcp,c3p0几种连接池的 ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- JavaScript高级程序设计学习(四)之引用类型
在javascript中也是有引用类型的,java同样如此. javascript常见也比较常用的引用类型就熟Object和Array. 一个对象和一个数组,这个在前后端分离开发中也用的最多.比如aj ...
- MATLAB:控制系统模型变换
1.多项式转换为零极点 [z,p,k]=tf2zp(num,den); 2.零极点转换为多项式 [num,den]=zp2tf(z,p,k); 3.状态空间转换成多项式传递函数 [num,den]=s ...