博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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中的闹铃时间,输出即可。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string.h>
  6. /*
  7. 3
  8. 5 0
  9. 6 0
  10. 7 0
  11. 59
  12. 6 59
  13. */
  14. using namespace std;
  15. const int maxn=;
  16. struct Node{
  17. int h;
  18. int m;
  19. bool operator<(const Node tmp)const{
  20. if(h==tmp.h)
  21. return m<tmp.m;
  22. else
  23. return h<tmp.h;
  24. }
  25. };
  26. Node clocks[maxn];
  27. int n;
  28. int main()
  29. {
  30. int a,b;
  31. scanf("%d",&n);
  32. for(int i=;i<n;i++){
  33. scanf("%d %d",&clocks[i].h,&clocks[i].m);
  34. }
  35. sort(clocks,clocks+n);
  36. int x;
  37. scanf("%d",&x);
  38. scanf("%d %d",&a,&b);
  39. for(int i=n-;i>=;i--){
  40. if(clocks[i].h>a)
  41. continue;
  42. if(clocks[i].h==a){
  43. if(b-clocks[i].m>=x){
  44. printf("%d %d\n",clocks[i].h,clocks[i].m);
  45. break;
  46. }
  47. }
  48. else{
  49. if(-clocks[i].m+(a-clocks[i].h-)*+b>=x){
  50. printf("%d %d\n",clocks[i].h,clocks[i].m);
  51. break;
  52. }
  53. }
  54. }
  55.  
  56. return ;
  57. }

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)]是构不成矩阵的。

  1. for(int k=lx+;k<=rx;k++){
  2. for(int p=ly+;p<=ry;p++){
  3. cnt[k][p]++;
  4. }
  5. }

完整代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string.h>
  6. using namespace std;
  7. /*
  8. 2
  9. 0 90
  10. 0 90
  11. 100 200
  12. 100 200
  13. */
  14. const int maxn=;
  15. int n;
  16. int xx1[maxn],xx2[maxn],yy1[maxn],yy2[maxn];
  17. int idx=;
  18. int a[maxn];
  19. int hash_x[maxn];
  20. int cnt[maxn][maxn];
  21.  
  22. int binarySearch(int *a,int t,int n){
  23. int l=,r=n-;
  24. int mid;
  25. while(l<=r){
  26. mid=(l+r)>>;
  27. if(a[mid]==t)
  28. return mid;
  29. if(t<a[mid])
  30. r=mid-;
  31. else
  32. l=mid+;
  33. }
  34. return -;
  35. }
  36.  
  37. int main()
  38. {
  39. scanf("%d",&n);
  40. for(int i=;i<n;i++){
  41. scanf("%d",&xx1[i]);
  42. a[i*+]=xx1[i];
  43. }
  44. for(int i=;i<n;i++){
  45. scanf("%d",&yy1[i]);
  46. a[i*+]=yy1[i];
  47. }
  48. for(int i=;i<n;i++){
  49. scanf("%d",&xx2[i]);
  50. a[i*+]=xx2[i];
  51. }
  52. for(int i=;i<n;i++){
  53. scanf("%d",&yy2[i]);
  54. a[i*+]=yy2[i];
  55. }
  56. //离散化,将-10^9~10^9的数据映射为0~200以内,因为最多出现4*50不同的数,映射值即为索引。
  57. sort(a,a+*n);
  58. hash_x[]=a[];
  59. idx=;
  60. for(int i=;i<*n;i++){
  61. if(a[i]!=a[i-]){
  62. hash_x[idx]=a[i];
  63. idx++;
  64. }
  65. }
  66. memset(cnt,,sizeof(cnt));
  67. for(int i=;i<n;i++){
  68. int lx,ly,rx,ry;
  69. //二分查找对应离散化后的索引。
  70. lx=binarySearch(hash_x,xx1[i],idx);
  71. ly=binarySearch(hash_x,yy1[i],idx);
  72. rx=binarySearch(hash_x,xx2[i],idx);
  73. ry=binarySearch(hash_x,yy2[i],idx);
  74. for(int k=lx+;k<=rx;k++){
  75. for(int p=ly+;p<=ry;p++){
  76. cnt[k][p]++;
  77. }
  78. }
  79. }
  80. int ans=;
  81. for(int i=;i<=idx;i++){
  82. for(int j=;j<=idx;j++){
  83. ans=max(ans,cnt[i][j]);
  84. }
  85. }
  86. if(ans==)
  87. ans=;
  88. printf("%d\n",ans);
  89. return ;
  90. }

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,才不会溢出,否则样例会有不过。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string.h>
  6. using namespace std;
  7. const int maxn=;
  8. int n;
  9. int v[maxn];
  10. long long sum[maxn]; //sum[i]统计v[1]~v[i]的和
  11. int w;
  12. long long tot=;
  13. long long two[]; //two[i]即为2^i值
  14.  
  15. //dfs搜索所有的方案数
  16. void dfs(int idx,int left){
  17. if(idx<= || left<=){
  18. tot+=;
  19. return;
  20. }
  21. if(sum[idx]<=left){
  22. tot+=two[idx];
  23. return;
  24. }
  25. if(left>=v[idx]){
  26. dfs(idx-,left-v[idx]);
  27. }
  28. dfs(idx-,left);
  29. }
  30. int main()
  31. {
  32. two[]=;
  33. for(int i=;i<=;i++)
  34. two[i]=two[i-]*;
  35. scanf("%d %d",&n,&w);
  36. for(int i=;i<=n;i++){
  37. scanf("%d",&v[i]);
  38. }
  39. sort(v+,v+n+);
  40. sum[]=;
  41. for(int i=;i<=n;i++){
  42. sum[i]=sum[i-]+v[i];
  43. }
  44. dfs(n,w);
  45. printf("%lld\n",tot);
  46. return ;
  47. }

网易2018.03.27算法岗,三道编程题100%样例AC题解的更多相关文章

  1. 【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】

    --------– 2018.03.27 更新--------- 便携版已更新,点此获取便携版 已知BUG:中文目录无法正常调试 用于cpptools 0.15.0插件的配置文件更新 新的launch ...

  2. 2017 CVTE春招内推专场 C/C++软件开发岗笔试编程题

    先来一波吐槽:选择题全是不定项选择,考的内容在我看来,"反正我接受唔到咯". 比如: 1.Windows操作系统某个通信机制(具体题目忘了,反正答案我选了个熟悉的名词"消 ...

  3. 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】

    openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...

  4. 2018/03/27 每日一个Linux命令 之 cron

    Cron 用于配置定时任务. -- 环境为 Ubuntu16-04 -- 先说说怎么配置一个简单的定时任务.直观的可以看到效果. 之前在网上查找资料,对Shell编程不熟悉的实在是很头疼,走了不少弯路 ...

  5. 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, ...

  6. 2018.03.27 pandas concat 和 combin_first使用

    # 连接和修补concat.combine_first 沿轴的堆叠连接 # 连接concatimport pandas as pdimport numpy as np s1 = pd.Series([ ...

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

  8. 网易2019校招C++研发工程师笔试编程题

    丰收? (忘了题目了QAQ) 题目描述: 又到了丰收的季节,恰逢小易去牛牛的果园里游玩. 牛午常说他对整个果园的每个地方都了如指掌,小易不太相信, 所以他想考考牛牛. 在果园里有N堆苹果,每堆苹果的数 ...

  9. Python基础编程题100列目录

    实例001:数字组合 实例002:"个税计算" 实例003:完全平方数 实例004:这天第几天 实例005:三数排序 实例006:斐波那契数列 实例007:copy 实例008:九 ...

随机推荐

  1. php中编码转换方法

    php里经常用到编码转换,在这记录一个常用的编码转换方法,字符串.数组.对象都可以使用,使用了递归来解决,比较普通 /* * php中编码转换 * @param $param 需要转换的数据 * @p ...

  2. D-Link DIR-600 - Authentication Bypass

    #Exploit Title: D-Link DIR-600 - Authentication Bypass (Absolute Path Traversal Attack) # CVE - http ...

  3. 个人技术博客——linux服务器配置以及flask框架

    本次的软件工程实践,我负责我们组后台服务的搭建,我选用了bandwagon的服务器,安装的是Debian GNU/Linux,全程在root用户下操作,后端服务是用python的flask框架,数据库 ...

  4. python框架面试题联系

    1.对 MVC,MVT 解读的理解? M:Model,模型,和数据库进行交互 V:View,视图,负责产生 Html 页面 C:Controller,控制器,接收请求,进行处理,与 M 和 V 进行交 ...

  5. 1095 Anigram单词

      基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词 ...

  6. M600 (1)飞行注意事项

  7. 数据库连性池性能测试(hikariCP,druid,tomcat-jdbc,dbcp,c3p0)

    文章转自  https://www.tuicool.com/articles/qayayiM 摘要: 本文主要是对这hikariCP,druid,tomcat-jdbc,dbcp,c3p0几种连接池的 ...

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

  9. JavaScript高级程序设计学习(四)之引用类型

    在javascript中也是有引用类型的,java同样如此. javascript常见也比较常用的引用类型就熟Object和Array. 一个对象和一个数组,这个在前后端分离开发中也用的最多.比如aj ...

  10. MATLAB:控制系统模型变换

    1.多项式转换为零极点 [z,p,k]=tf2zp(num,den); 2.零极点转换为多项式 [num,den]=zp2tf(z,p,k); 3.状态空间转换成多项式传递函数 [num,den]=s ...