每日算法 - day 15
每日算法
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的更多相关文章
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- A*算法解决15数码问题_Python实现
1问题描述 数码问题常被用来演示如何在状态空间中生成动作序列.一个典型的例子是15数码问题,它是由放在一个4×4的16宫格棋盘中的15个数码(1-15)构成,棋盘中的一个单元是空的,它的邻接单元中的数 ...
- 每日一道 LeetCode (15):二进制求和
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- 红黑树——算法导论(15)
1. 什么是红黑树 (1) 简介 上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...
- 51nod算法马拉松15
智力彻底没有了...看来再也拿不到奖金了QAQ... A B君的游戏 因为数据是9B1L,所以我们可以hash试一下数据... #include<cstdio> #include<c ...
- 每日算法---Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 每日英语: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 ...
- 【每日算法】排序算法总结(复杂度&稳定性)
一.插入排序:稳定,时间复杂度O(n^2) 想象你在打扑克牌,一開始左手是空的,接着右手開始从桌上摸牌,并将其插入到左手的一把牌中的正确位置上.为了找到这个正确位置,我们须要从右到左将它与手中的牌比較 ...
- CISP/CISA 每日一题 15
CISA 每日一题(答) 作业记帐: 监控和记录信息系统资源的使用,这些信息可被信息系统审计师用来执行: 1.将资源使用和相关用户挂钩以便实行计费: 2.通过改变系统软件的默认设置来最优化硬件性能 作 ...
随机推荐
- Hadoop之伪分布式安装
一.Hadoop的安装模式有3种 ①单机模式:不能使用HDFS,只能使用MapReduce,所以单击模式主要用于测试MR程序. ②伪分布式模式:用多个线程模拟真实多台服务器,即模拟真实的完全分布式环境 ...
- mui 获取本地APP本版号的两种方式
第一种 mui.plusReady(function () { mui.getJSON("manifest.json", null, function (data) { vum = ...
- docker部署-windows环境
docker部署-windows环境 1. docker windows 1.1. 安装 win7或者win8需要利用docker toolbox来安装,其是一个docker工具集,w ...
- mysql_pw 指令 数据库创建过程
------------------pw_db数据库创建过程各表创建指令-------------------------- create database pw_db; #创建一个数据库use pw ...
- java里自定义分页查询的尝试
public String list(){ try { LoginUser loginUser = getLoginUser();//获取当前登录用户 if(curpage<=0){ curpa ...
- ipfs camp course c demo exercise 1
目录 aim: my bugs 解决ipfs 的 cros 问题的方法 result final code for c1 aim: 首先咱们把 broswer 和 自己的api 连接起来(要显示出来自 ...
- kafka 分区
1. 默认存储 /tmp/kafka-logs test-0 0:分区数
- mybatis源码探索笔记-3(使用代理mapper执行方法)
前言 前面两章我们构建了SqlSessionFactory,并通过SqlSessionFactory创建了我们需要的SqlSession,并通过这个SqlSession获取了我们需要的代理mapper ...
- Android开发Intent应用概述
原文: http://bbs.gfan.com/android-93448-1-1.html 今天,我们来研究一下Intent,没错,就是前面说过的比较难理解的那个东西,希望通过这篇文章之后,你发现前 ...
- Mac 系统上有趣的插件
1.微信小助手:https://github.com/TKkk-iOSer/WeChatPlugin-MacOS 作用:开启消息撤回拦截,设置自动回复,远程登录Mac,微信多开,免认证登录.... 2 ...