(牛客)2023年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛(同步赛)部分可达题补题代码

2023年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛(同步赛)补题代码

  • 特别感谢思路与代码提供者:PotremZ

比赛网址: https://ac.nowcoder.com/acm/contest/52244#question

A.A Xor B Problem

// Problem: A Xor B Problem
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/A
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<int> num(N);
	set<int> ans;
	for(int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		num[a]++;
		ans.insert(a);
	}
	
	long long sum = 0;
	for(auto x : ans)
	{
		sum += 1ll * num[x] * num[x];
	}
	
	cout << sum << "\n";
	return 0;
}

B.吃苹果(贪心)

// Problem: 吃苹果
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/B
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define a first
#define b second
using namespace std;

typedef pair<int, int> PII;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k;
	cin >> n >> k;
	vector<PII> p(n);
	
	for(auto &[a, b]:p) cin >> a >> b;
	sort(p.begin(), p.end(), [&](PII x, PII y){
		return x.b - x.a > y.b - y.a;
	});
	int res = 0;
	for(int i = 0; i < k; i++) res += p[i].b;
	for(int i = k; i < n; i++) res += p[i].a;
	cout << res;
	return 0;
}

C.n皇后问题

// Problem: n皇后问题
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/C
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

const int N = 2e6 + 10;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    
    int n, t;
    cin >> n >> t;
    vector<int> g(N), ug(N), r(N), c(N);
    
    while(t--)
    {
        int a, b;
        cin >> a >> b;
        if(!r[a] && !c[b] && !g[b - a + n] && !ug[b + a])
        {
            puts("Yes");
            r[a] = 1;
            c[b] = 1;
            g[b - a + n] = 1;
            ug[b + a] = 1;
        }
        else puts("No");
    }
    return 0;
}

D.分苹果(简单计算几何)

// Problem: 分苹果
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/D
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

typedef double db;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	db Ae, Be, Ce;
	cin >> Ae >> Be >> Ce;
	db Ar, Br, Cr;
	cin >> Ar >> Br >> Cr;
	function<bool (db, db, db, db, db)> check = [&](db A, db B, db C, db x, db y)
	{
		db ny = (-C - A * x) / B;
		return ny > y;
	};
	
	
	vector<int> ans(4);
	while(n--)
	{
		db x, y;
		cin >> x >> y;
		int p = check(Ae, Be, Ce, x, y) + 2 * check(Ar, Br, Cr, x, y);
		ans[p]++;
	}
	
	sort(ans.begin(), ans.end());
	for(auto x : ans) cout << x << " ";
	return 0;
}

E.完型填空(dp)

// Problem: 完型填空
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/E
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 30;

int f[N][N][N][N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		
		for(int A = 0; A <= min(i, n / 4); A++)
		{
			for(int B = 0; B <= min(i - A, n / 4);B++)
			{
				for(int C = 0; C <= min(i - A - B, n / 4); C++)
				{
					int D = i - A - B - C;
					if(D < 0) continue;
					if(A) f[A][B][C][D] = max(f[A][B][C][D], f[A - 1][B][C][D] + a);
					if(B) f[A][B][C][D] = max(f[A][B][C][D], f[A][B - 1][C][D] + b);
					if(C) f[A][B][C][D] = max(f[A][B][C][D], f[A][B][C - 1][D] + c);
					if(D) f[A][B][C][D] = max(f[A][B][C][D], f[A][B][C][D - 1] + d);
				}
			}
		}
	}
	
	int k = n / 4;
	cout << f[k][k][k][k] << "\n";
	return 0;
}

F.坐火车(最短路)

// Problem: 坐火车
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/F
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> PII;
vector< vector<PII> > e(N);
int dist[N], sum[N];
bool st[N];
int n, m;

void Spfa()
{
	memset(dist, 0x3f, sizeof dist);
	memset(sum, 0x3f, sizeof sum);
	dist[1] = 0;
	sum[1] = 1;
	queue<int> q;
	q.push(1);
	st[1] = true;
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		st[t] = false;
		for(int i = 0; i < e[t].size(); i++)
		{
			int j = e[t][i].first;
			int w = e[t][i].second;
			if(sum[j] >= sum[t] + 1)
			{
				sum[j] = sum[t] + 1;
				dist[j] = min(dist[j], dist[t] + w);
				if(!st[j])
				{
					q.push(j);
					st[j] = true;
				}
			}
			
		}
	}
	
	cout << sum[n] << " " << dist[n] << "\n";
	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		e[a].push_back({b, c});
		e[b].push_back({a, c});
	}
	
	Spfa();

	return 0;
}

G.A Xor B Problem again(字典树)

// Problem: A Xor B Problem again
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/G
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;

ll tr[N << 4][2], sum[N << 4], tot;
void Insert(int x)
{
	int u = 0;
	do
	{
		int p = x & 1;
		if(!tr[u][p]) tr[u][p] = ++tot;
		u = tr[u][p];
		x >>= 1;
		if(!x) sum[u]++;
	}while(x);
}

ll query(int u, int x)
{
	ll res = sum[u];
	int p = x & 1;
	if(p == 0 && tr[u][p]) res += query(tr[u][p], x / 2);
	if(tr[u][p ^ 1]) res += query(tr[u][p ^ 1], x / 2);
	return res;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	vector<int> a(n);
	map<int,int > num;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		Insert(a[i]);
		num[a[i]]++;
	}
	
	ll ans = 0;
	for(auto &[x, y] : num)
	{
		ll res = query(0, x) * y;
		ans += res;
	}
	cout << ans << "\n";
	return 0;
}

H.摘苹果(线段树)

// Problem: 摘苹果
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/52244/H
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;

ll sum[N << 2];
int cnt[N << 2], used[N << 2];

struct SegTree
{
	void pushup(int o)
	{
		sum[o] = sum[o << 1] + sum[o << 1 | 1];
		cnt[o] = cnt[o << 1] + cnt[o << 1 | 1];
		used[o] = used[o << 1] & used[o << 1 | 1];
	}
	
	void build(int o, int l, int r)
	{
		if(l == r)
		{
			cin >> sum[o];
			if(sum[o] < 100) cnt[o] = 1;
			if(sum[o] < 10) used[o] = 1;
			return;
		}
		int mid = (l + r) >> 1;
		build(o << 1, l, mid);
		build(o << 1 | 1, mid + 1, r);
		pushup(o);
	}
	
	void update(int o, int l, int r, int L, int R)
	{
		if(used[o]) return ;
		if(l == r)
		{
			sum[o] -= (sum[o] + 2) /3;
			if(sum[o] < 100) cnt[o] = 1;
			if(sum[o] < 10) used[o] = 1;
			return;
		}
		
		int mid = l + r >> 1;
		if(L <= mid) update(o << 1, l, mid, L, R);
		if(R > mid) update(o << 1 | 1, mid + 1, r, L, R);
		pushup(o);
	}
	
	ll query(int o, int l, int r, int L, int R, int p)
	{
		if(L <= l && r <= R)
		{
			if(p == 2) return cnt[o];
			else return sum[o];
		}
		
		int mid = l + r >> 1;
		ll res = 0;
		if(L <= mid) res += query(o << 1, l, mid, L, R, p);
		if(R >  mid) res += query(o << 1 | 1, mid + 1, r, L, R, p);
		return res;
	}
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, m;
	cin >> n >> m;
	SegTree segtree;
	segtree.build(1, 1, n);
	while(m--)
	{
		int op, l, r;
		cin >> op >> l >> r;
		if(op == 1) segtree.update(1, 1, n, l, r);
		else if(op == 2) cout << segtree.query(1, 1, n, l, r, op) << "\n";
		else cout << segtree.query(1, 1, n, l, r, op) << "\n";
	}
	
	return 0;
}
posted @ 2023-03-12 16:13  哲远甄骏  阅读(346)  评论(0)    收藏  举报