每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.2.27


luogu-P1420 最长连号

最长上升子序列 可以用 dp 也可以 BF

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 100010;
long long a[N] , n ,f[N];
int main()
{
cin >> n;
for(int i = 1;i <= n ;i ++)
{
scanf("%d",&a[i]);
f[i] = 1;
}
int maxc = -1;
for(int i = 2;i <= n ;i ++)
{
if(a[i] == a[i-1] + 1)f[i] = f[i-1] + 1;
if(f[i] > maxc)maxc = f[i];
}
cout << maxc << endl;
return 0;
}

luogu-P1008 三连击

暴力dfs

我们可以一次枚举每一个合法的三位数a ,然后检验

2 * a 和 3 * a 是否合法即可

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
const int N = 10;
bool vis[N];
bool check(int a, int b)
{
int book[10];
for(int i = 1;i <= 9 ;i ++)
{
if(vis[i])book[i] = 1;
else book[i] = 0;
}
while(a > 0)
{
int t = a % 10;
if(t == 0)return false;
book[t]++;a /= 10;
}
while(b > 0)
{
int t = b % 10;
if(t == 0)return false;
book[t]++;b /= 10;
}
for(int i = 1;i <= 9; i++)
{
if(book[i] != 1)return false;
}
return true;
}
void dfs(int cur, int sum)
{
if(cur == 3)
{
int a = sum * 2,b = sum * 3;
if(check(a,b))printf("%d %d %d\n",sum,a,b);
return ;
}
else {
for(int i = 1;i <= 9 ;i ++)
{
if(vis[i] == false)
{
vis[i] = 1;int t = sum ;sum = t * 10 + i;
dfs(cur + 1,sum);
sum = t;vis[i] = 0;
}
}
}
}
int main()
{
for(int i = 1;i <= 9 ;i ++)
{
vis[i] = 1;
dfs(1,i);
vis[i] = 0;
}
return 0;
}

luogu -P1157 组合输出

dfs 没看到题目说不能用递归 回头补上

C++ 中控制输出宽度应该用setw(你想要的宽度)

同时需要引入头文件#include

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <iomanip>
using namespace std;
const int N = 100000;
int n ,r , a[N];
void dfs(int now,int cur)
{
if(cur == r)
{
for(int i = 0;i < cur;i ++)
cout << setw(3) << a[i];
puts("");
return;
}else{
for(int i = now + 1;i <= n ;i ++)
{
a[cur] = i;
dfs(i,cur + 1);
}
}
}
int main()
{
cin >> n >> r;
for(int i = 1;i <= n - r + 1;i ++)
{
a[0] = i;dfs(i,1);a[0] = 0;
}
return 0;
}

leetcode 1351. 统计有序矩阵中的负数

思路: 二分

C++ 代码如下:

class Solution {
public:
int merge(vector<int> num)
{
int l = 0,r = num.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
if(num[mid] < 0)r = mid;
else l = mid + 1;
}
cout << l << endl;
if(num[l] >= 0)return 0;
else return num.size() - l;
}
int countNegatives(vector<vector<int>>& grid) {
int ans = 0;
for(int i = 0;i < grid.size() ;i ++)
ans += merge(grid[i]);
return ans;
}
};

Java 代码

执行用时 :0 ms, 在所有 Java 提交中击败了100.00% 的用户 hhh

class Solution {
public static int merge(int [] num)
{
int l = 0, r = num.length - 1;
while(l < r)
{
int mid = (l + r) / 2;
if(num[mid] < 0)r = mid;
else l = mid + 1;
}
if(num[l] >= 0)return 0;
else return num.length - l;
}
public int countNegatives(int[][] grid) {
int ans = 0;
for(int i = 0;i < grid.length; i++)
ans += merge(grid[i]);
return ans;
}
}

每日算法 - day 15的更多相关文章

  1. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  2. A*算法解决15数码问题_Python实现

    1问题描述 数码问题常被用来演示如何在状态空间中生成动作序列.一个典型的例子是15数码问题,它是由放在一个4×4的16宫格棋盘中的15个数码(1-15)构成,棋盘中的一个单元是空的,它的邻接单元中的数 ...

  3. 每日一道 LeetCode (15):二进制求和

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  4. 红黑树——算法导论(15)

    1. 什么是红黑树 (1) 简介     上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...

  5. 51nod算法马拉松15

    智力彻底没有了...看来再也拿不到奖金了QAQ... A B君的游戏 因为数据是9B1L,所以我们可以hash试一下数据... #include<cstdio> #include<c ...

  6. 每日算法---Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. 每日英语:15 places to find inspiration

    If you’re a writer or artist, you understand the power of location when it comes to creativity and f ...

  8. 【每日算法】排序算法总结(复杂度&amp;稳定性)

    一.插入排序:稳定,时间复杂度O(n^2) 想象你在打扑克牌,一開始左手是空的,接着右手開始从桌上摸牌,并将其插入到左手的一把牌中的正确位置上.为了找到这个正确位置,我们须要从右到左将它与手中的牌比較 ...

  9. CISP/CISA 每日一题 15

    CISA 每日一题(答) 作业记帐: 监控和记录信息系统资源的使用,这些信息可被信息系统审计师用来执行: 1.将资源使用和相关用户挂钩以便实行计费: 2.通过改变系统软件的默认设置来最优化硬件性能 作 ...

随机推荐

  1. 腾讯云Windows Server下nodejs websocket ssl配置

    1.从腾讯云申请SSL证书,下载解压,得到如下文件: 2.nodejs代码: // wss.js const fs = require('fs'); // 一些配置信息 const cfg = { p ...

  2. 【原创】Centos配置turn服务器

    使用ssh工具,进入命令行,安装下面的就是可以配置turn-server(coturn) 转请注明出处. 1.安装centos必须的库文件      yum install -y make gcc c ...

  3. JDK源码分析-HashMap

    一.HashMap的内部属性 1.1 成员变量 1.1.1 size: HashMap包含的KV键值对的数量,也就是我们通常调用Map.size()方法的返回值 public int size() { ...

  4. pikachu-搜索型注入 #手工注入

    1.搜索型注入漏洞产生的原因: 在搭建网站的时候为了方便用户搜索该网站中的资源,程序员在写网站脚本的时候加入了搜索功能,但是忽略了对搜索变量的过滤,造成了搜索型注入漏洞,又称文本框注入. 2.搜索型注 ...

  5. Maven学习笔记:Maven简介

    Maven的概念 Maven是基于项目对象模型(POM,Project Object Model),可以通过描述信息来管理项目的构建,报告和文档的软件管理工具 Maven除了以程序构建能力为特色之外, ...

  6. WLC exclusionlist

    Configuring Client Exclusion Configuring Client Exclusion Policies (GUI) Step 1   Choose Security &g ...

  7. lc 0228

    目录 ✅ 412. Fizz Buzz 描述 解答 c数字转字符: other's c my c tdo py ✅ 235. 二叉搜索树的最近公共祖先 描述 解答 评价者 思路: c py ✅ 412 ...

  8. 洗牌利器——random.shuffle()函数

    random.shuffle()是一个非常实用但是又非常容易被忽略的函数,shuffle在英语里是"洗牌"的意思,该函数非常形象地模拟了洗牌的过程,即: random.shuffl ...

  9. 指令——mdadm

    Mdadm命令详解 Linux内核中有一个md(multiple devices)模块在底层管理RAID设备,它会在应用层给我们提供一个应用程序的工具mdadm ,mdadm是linux下用于创建和管 ...

  10. case语句!

    1.case 语句概述(1)case 语句的作用使用 case 语句改写 if 多分支可以使脚本结构更加清晰.层次分明.针对变量的不同取值,执行不同的命令序列.2.case 语句的结构:case 变量 ...