2023河南萌新联赛第(一)场

A. 你也喜欢数学吗?(数学公式,欧拉函数推理)

输入1

1

输出1

1

输入2

3

输出2

10

点击查看代码
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int p = 1e9 + 7;

signed main()
{
    int n;
    cin >> n;
    n %= p;
    cout << n * (n + 1)%p*(n+2)%p*(p+1)/2%p*(p+1)/3%p;
    return 0;
}

H. 迷宫探险

输入1

3 2
.*
#.
..
1
1 2 2

输出1

1

输入2

2 2
.*
#.
1
1 2 2

输出

-1

点击查看代码
#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;

const int N = 3010;

char s[N][N];
int a[N][N], b[N][N];
int n, m, k, x, y, z, nx, ny;
int ne[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

void solve()
{
    memset(a, 0x3f, sizeof a);
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
        cin >> (s[i] + 1);
    cin >> k;
    for(int i = 1; i <= k; i ++)
        cin >> x >> y >> z, b[x][y] = z;
    deque<pair<int, int> > q;
    q.push_front({1, 1});
    a[1][1] = 0;
    while(q.size())
    {
        x = q.front().fi, y = q.front().se;
        q.pop_front();
        if(s[x][y] == '.')
        {
            for(int i = 0; i < 4; i ++)
            {
                nx = x + ne[i][0];
                ny = y + ne[i][1];
                if(nx>=1&&nx <= n&&ny>=1&&ny <= m&&s[nx][ny] != '#'&&a[nx][ny] > a[x][y] + 1)
                {
                    a[nx][ny] = a[x][y] + 1;
                    q.push_back({nx, ny});
                }
            }
        }
        else
        {
            for(int i = 0; i < 4; i ++)
            {
                nx = x + ne[i][0] * b[x][y];
                ny = y + ne[i][1] * b[x][y];
                if(nx>=1&&nx <= n&&ny>=1&&ny <= m&&s[nx][ny] != '#'&&a[nx][ny] > a[x][y])
                {
                    a[nx][ny] = a[x][y];
                    q.push_front({nx, ny});
                }
            }
        }
    }
    if(a[n][m] == 0x3f3f3f3f)
        a[n][m] = -1;
    cout << a[n][m] << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    while(T --)
        solve();
    return T ^ T;
}

I. 松鼠采松果(典型的树上差分)

输入

7 3 3
1 2 1
1 3 2
2 4 1
2 5 2
3 6 1
3 7 2
2 3 1
1 2 1
2 3 0
2 3
4 7
1 1

输出

6
9
0

点击查看代码
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e5 + 10, M = N * 2;

int n, m, q;
int e[M], w[M], ne[M], h[N], idx;
int sc[N];
int fa[N][20], ce[N];
int sum[N];

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int query(int a, int b)
{
	if(ce[a] < ce[b]) swap(a, b);
	for(int i = 17; i >= 0; i --)
		if(ce[fa[a][i]] >= ce[b])
			a = fa[a][i];
	if(a == b) return a;
	for(int i = 17; i >= 0; i --)
		if(fa[a][i] != fa[b][i])
			a = fa[a][i], b = fa[b][i];
	return fa[a][0];
}

void LCA_init(int root)
{
	memset(ce, 0x3f, sizeof ce);
	ce[root] = 1, ce[0] = 0;
	queue<int> q;
	q.push(root);
	while(!q.empty())
	{
		int t = q.front();
		q.pop();
		for(int i = h[t]; ~i; i = ne[i])
		{
			int j = e[i];
			if(ce[j] > ce[t] + 1)
			{
				ce[j] = ce[t] + 1;
				fa[j][0] = t;
				for(int k = 1; k < 18; k ++)
					fa[j][k] = fa[fa[j][k - 1]][k - 1]; 
				q.push(j);
			}
		}
	}
}

int LCA_finish(int u, int f)
{
	int s = sc[u];
	for(int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if(j == f) continue;
		int t = LCA_finish(j, u);
		w[i] += t;
		s += t;
	}
	return s;
}

void dfs(int u, int f)
{
	for(int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if(j == f) continue;
		sum[j] = sum[u] + w[i];
		dfs(j, u);
	}
}

void solve()
{
	memset(h, -1, sizeof h);
	cin >> n >> m >> q;
	for(int i = 1; i < n; i ++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	LCA_init(1);
	for(int i = 1; i <= m; i ++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		sc[a] += c, sc[b] += c, sc[query(a, b)] -= 2 * c;
	}
	LCA_finish(1, -1);
	
	dfs(1, -1);
	
	while(q --)
	{
		int x, y;
		cin >> x >> y;
		cout << sum[x] + sum[y] - 2 * sum[query(x, y)] << "\n";
	}
}

signed main()
{
	int T = 1;
//	cin >> T;
	while(T --)
		solve();
	return T ^ T;
}

L. 中位数(正确使用map)

输入

7 3
1 2 3 4 5 6 7
2 3
4 4
7 1

输出

4
4
3

点击查看代码
#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

const int N = 1e6 + 10;

int n, m, mid, midval, s, l, x;
int a[N];
map<int, int> mp;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	mid = (n + 1) >> 1;
	for(int i = 1; i <= n; i ++)
		cin >> a[i], mp[a[i]] ++;
	map<int, int> :: iterator it;
	for(it = mp.begin(); it != mp.end(); it ++)
	{
		s += it -> se;
		if(s >= mid) break;
	}
	midval = it -> fi;
	while(m --)
	{
		cin >> l >> x;
		if(a[l] <= midval&&x <= midval||a[l] > midval&&x > midval)
		{
			mp[a[l]] --, mp[x] ++;
			a[l] = x;
		}
		else if(a[l] <= midval&&x > midval)
		{
			s --;
			mp[a[l]] --, mp[x] ++;
			a[l] = x;
		}
		else
		{
			s ++;
			mp[a[l]] --, mp[x] ++;
			a[l] = x;
		}
		if(s - it -> se >= mid)
		{
			for(; it != mp.end(); it --)
			{
				if(s - it -> se < mid) break;
				s -= it ->se;
			}
		}
		else if(s < mid)
		{
			it ++;
			for(; it != mp.end(); it ++)
			{
				s += it -> se;
				if(s >= mid) break;
			}
		}
		midval = it -> fi;
		cout << midval << "\n";
	}
}
posted @ 2023-09-24 23:53  chfychin  阅读(40)  评论(0)    收藏  举报