每日算法
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
",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;
}
}