每日算法

每日算法

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.3.4


P1464 Function

记忆化搜索

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>

using namespace std;
typedef long long ll;
const int N = 25;

int w[N][N][N];

// 不能赋值得时候return  
long long f(int a,int b,int c)
{
	if(a <= 0 || b <= 0 || c <= 0)return 1;
	else if(w[a][b][c] != 0)return w[a][b][c];
	else if(a > 20 || b > 20 || c > 20) w[a][b][c] = f(20,20,20);
	else if(a < b && b < c)w[a][b][c] = f(a,b,c-1) + f(a,b-1,c-1) - f(a,b-1,c);
	else w[a][b][c] = f(a-1,b,c)+f(a-1,b-1,c)+f(a-1,b,c-1)-f(a-1,b-1,c-1);
	return w[a][b][c];
}
int main()
{
	 ll a, b , c;
	while(1)
	{
		cin >> a >> b >> c;
		if(a == -1 && b == -1 && c == -1)break;
		printf("w(%lld, %lld, %lld) = ",a,b,c);
		if(a > 20)a = 21;if(b > 20)b = 21;if(c > 20)c = 21;
		printf("%lld
",f(a,b,c));
	}
	return 0; 
} 

P1400 塔

第一眼想到了贪心+乘法,于是果断排序,但是推规律的时候发现公式推错了,太菜了

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>

using namespace std;
const int N = 600005;

long long h[N] , n ,d ,f[N], maxc = 1, ans = 1;
int main()
{
	cin >> n >> d;
	for(int i = 1;i <= n ;i ++)scanf("%lld",&h[i]);
	
	sort(h + 1, h + n + 1);
	
	for(int i = 2;i <= n ;i ++)
	{
		while(h[i] - d > h[maxc] && maxc < i)maxc++;
		
		ans *= i - maxc + 1;
		ans %= 1000000009;
	}	
	cout << ans << endl;
	return 0;
}

P1449 后缀表达式

#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdio>

using namespace std;

stack<int> st;
int main()
{
	string s , t;
	cin >> s;
	for(int i = 0;i < s.size();i ++)
	{
		if(s[i] == '@')break;
		else if(s[i] == '.')
		{
			st.push(stoi(t)); 
			t = "";
		}
		else if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
		{
			int a = st.top();st.pop();
			int b = st.top();st.pop();
			int c = 0;
			if(s[i] == '+')c = b + a;
			if(s[i] == '-')c = b - a;
			if(s[i] == '*')c = b * a;
			if(s[i] == '/')c = b / a;
			st.push(c);
		}
		else if(s[i] >= '0' && s[i] <= '9')t += s[i];
	}	
	cout << st.top() << endl;
	return 0;
} 

数列区间最大值

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <climits>
using namespace std;
const int N = 100010;


// INT_MIN  和 INT_MAX 属于 climits头文件下 
int n , m;
int w[N];
struct node{
	int l, r;
	int maxv;
}tr[N * 4];

void build(int u,int l,int r)
{
	if(l == r)tr[u] = {l,r,w[r]};
	else{
		tr[u] = {l,r};
		int mid = l + r >> 1;
		build(u << 1,l ,mid),build(u << 1 | 1,mid + 1, r);
		tr[u].maxv = max(tr[u << 1].maxv, tr[u << 1 | 1].maxv);
	}
}

int query(int u,int l,int r)
{
	if(tr[u].l >= l && tr[u].r <= r)return tr[u].maxv;
	int mid = tr[u].l + tr[u].r >> 1;
	
	int maxv = INT_MIN;
	if(l <= mid) maxv = query(u << 1,l ,r);
	if(r > mid) maxv = max(maxv,query(u << 1 | 1,l,r));
	return maxv;
}

int main()
{
	cin >> n >> m;
	
	for(int i = 1;i <= n;i ++)scanf("%d",&w[i]);
	
	build(1,1,n);
	
	int l,r;
	while(m--)
	{
		scanf("%d%d",&l,&r);
		printf("%d
",query(1,l,r));
	}
	return 0;	
} 
原文地址:https://www.cnblogs.com/wlw-x/p/12416931.html