每日算法

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. C语言:将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边。-在m行m列的二维数组中存放如下规律的数据,

    //将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边. #include <stdio.h> #define M 3 #define N 5 ...

  2. 计算机二级-C语言-程序设计题-190118记录-通过数组和指针两种方式对字符串进行处理。

    //编写一个函数fun,比较两个字符串的长度,(不使用C语言提供的求字符串长度的函数),函数返回较长的字符串.若两个字符长度相同,则返回第一个字符串. //重难点:通过数组处理和通过指针进行处理的不同 ...

  3. php编译安装扩展redis及swoole

    一.安装redis扩展 下载redis扩展包以及解压 wget https://github.com/edtechd/phpredis/archive/php7.zip unzip php7.zip ...

  4. Euler Sums系列(四)

    \[\Large\displaystyle \sum_{n=1}^\infty (-1)^n \frac{H_n}{2n+1}=\mathbf{G}-\frac{\pi}{2}\ln(2)\] \(\ ...

  5. Django安装数据库MySQLdb

    在使用Django搭建与树莓派智能终端时,使用mysql作为数据库管理,遇到如下问题: django.core.exceptions.ImproperlyConfigured: Error loadi ...

  6. [经验] 如何在虚拟机上安装 CentOS

    环境配置 虚拟机 vmware 15.5 Pro 操作系统 CentOS-7-x86_64-DVD-1908.iso 第一步: 在虚拟机上打开操作系统的镜像文件并配置硬件信息 这里的操作就是一本道  ...

  7. connection String加密

    aspnet_regiis -pe "connectionStrings" -app "/HG" -prov "ChrisProvider" ...

  8. SpringBoot与Jpa自定义增删查改

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. PAT T1016 Uniqueness of MST

    dfs判断连通块的数量,prim算法建立最小生成树并判断是否唯一~ #include<bits/stdc++.h> using namespace std; ; const int inf ...

  10. IntelliJ IDEA常用快捷键大全

    如果想要非常高效的使用IDEA这款工具,应该掌握图中已被标记的快捷键. 另: 代码实时模板生成:psvm/sout/ifn等 按Tab键快速生成模板. 转载请保留或注明出处:http://www.cn ...