每日算法

每日算法

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


luogu -P1980 计数问题

不用字符串也能做

#include <iostream>
#include <cstdio>

using namespace std;
int n , x;
long long ans;
void check(int s)
{
	int m = s;
	while(m)
	{
		if(m % 10 == x)ans++;
		m /= 10;
	}
}
int main()
{
	cin >> n >> x;
	for(int i = 1;i <= n; i++)
	{
		check(i);
	}
	cout << ans << endl;
	return 0;
} 

luogu- P1908 逆序对

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

using namespace std;
const int N = 500005;
int q[N],tmp[N];
long long ans; 
void merge(int q[],int l,int r)
{
	if(l >= r)return ;
	int mid = l + r >> 1;
	
	merge(q,l,mid),merge(q,mid+1,r);
	int i = l,j = mid + 1,k = 0;
	while(i <= mid && j <= r)
	{
		if(q[i] <= q[j])tmp[k++] = q[i++];
		else {
			ans += mid - i + 1;
			tmp[k++] = q[j++];
		}
	}
	while(i <= mid) tmp[k++] = q[i++];
	while(j <= r)tmp[k++] = q[j++];
	
	for(int i = l,j = 0;i <= r;j ++ ,i ++)q[i] = tmp[j];
}
int main()
{
	int n;
	cin >> n;
	for(int i = 0;i < n ;i ++)
	{
		scanf("%d",&q[i]);
	}
	merge(q,0,n-1);
	cout << ans << endl;
	return 0;
}

luogu -P3374 树状数组1

树状数组只能进行单点修改和单点查询
但是通过变形我们可以扩展它的应用

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

using namespace std;
const int N = 500005;

int tr[N], a[N] , n , m;

int lowbit(int x)
{
	return x & -x;
}

void add(int x,int v)
{
	for(int i = x;i <= n ;i += lowbit(i)) tr[i] += v;
}

int query(int x)
{
	int res = 0;
	for(int i = x;i;i-=lowbit(i))res += tr[i];
	return res;
}

int main()
{
	
	cin >> n >> m;
	
	for(int i = 1;i <= n;i ++)scanf("%d",&a[i]);
	
	for(int i = 1;i <= n ;i ++)add(i,a[i]);
	int a ,b , c;
	for(int i = 1;i <= m;i ++)
	{
		scanf("%d %d %d",&a,&b,&c);
		if(a == 1)add(b,c);
		else {
			cout << query(c) - query(b-1) << endl;
		}
	}
	
	return 0;
}
原文地址:https://www.cnblogs.com/wlw-x/p/12392918.html