每日算法 - 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;
}
}
最新文章
- 《CLR via C#》---枚举标志和标志位
- Android项目,从web上取下汉字,中文部分乱码
- 使用SftpDrive+SourceInsight阅读开源代码
- 详解Bootstrap媒体对象
- 浪首登录浮层增加收藏入口项目学到的几点知识-IE7 bug、relatedTarget、字符串换行
- UI2_视图切换
- mysql 数据库备份,恢复。。。。
- PureMVC(JS版)源码解析(八):Proxy类
- .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)
- Phpstorm中使用SFTP
- 在Ubuntu上安装arm-linux-gcc的问题
- 蓝桥杯java 关于大范围时间的
- audio元素和video元素在ios和andriod中无法自动播放
- Content Provider的启动过程
- 第七篇 Flask 中路由系统以及参数
- P2178 [NOI2015]品酒大会
- 【Solidity】学习(1)
- 浅谈MVVM
- python的时间差计算
- static理解
热门文章
- 树状数组(fenwick tree)
- centsos 7 删除自带jdk安装自定义jdk8
- LeetCode练题——53. Maximum Subarray
- 运行时Runtime的API
- splash-简介及入门
- 服务器(2)——IIS(2)——IIS Express(1)——IIS跟IIS Express之间的区别和关系
- ubuntu资料
- Educational Codeforces Round 82 A. Erasing Zeroes
- java里自定义分页查询的尝试
- 后端——框架——持久层框架——Mybatis——补充——pageHelper(分页)插件