首页 > 其他分享 >2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest

2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest

时间:2024-04-28 16:15:05浏览次数:33  
标签:Provincial Multi Contest int cin long vector Vec using

A. Maximum Element In A Stack

按照题目模拟就好,栈内的最大值可以维护单调栈解决。

#include <bits/stdc++.h>

using namespace std;


using i64 = long long;
using ui32 = unsigned int;

ui32 SA, SB, SC;


ui32 rng61(){
    SA ^= SA << 16;
    SA ^= SA >> 5;
    SA ^= SA << 1;
    ui32 t = SA;
    SA = SB;
    SB = SC;
    SC ^= t ^ SA;
    return SC;
}

void solve() {
	int n, p, q, m;
	cin >> n >> p >> q >> m >> SA >> SB >> SC;
	stack<ui32> s;
	i64 res = 0;
	for(ui32 i = 1; i <= n ; i ++) {
		if(rng61() % (p + q) < p) {
			ui32 tmp = (rng61() % m + 1);
			if(s.empty()) s.push(tmp);
			else s.push(max(s.top(), tmp));
		} else if(not s.empty()) s.pop();
		if(not s.empty()) res ^= (i64)i * (i64)s.top();
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int TC;
	cin >> TC;
	for(int i = 1; i <= TC; i ++)
		cout << "Case #" << i << ": ", solve();
	return 0;
}

B. Rolling The Polygon

偏模板的题目,通过三个点算出夹角,然后根据半径算一下弧长就好了。

#include<bits/stdc++.h>

using namespace std;

using i32 = int32_t;

#define int long long

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9;

using db = long double;

struct Point {
    db x, y;

    Point(db x = 0, db y = 0) : x(x), y(y) {};
};

using Vec = Point;

const db pi = acosl(-1);

Vec operator+(Vec u, Vec v) { return Vec(u.x + v.x, u.y + v.y); }

Vec operator-(Vec u, Vec v) { return Vec(u.x - v.x, u.y - v.y); }

db operator*(Vec u, Vec v) { return u.x * v.x + u.y * v.y; }

db len(Vec v) { return sqrt(v.x * v.x + v.y * v.y); }

db cos_t(Vec u, Vec v) { return u * v / len(u) / len(v); }


using Points = vector<Point>;

ostream &operator<<(ostream &os, Point x) {
    return os << "(" << x.x << "," << x.y << ")";
}

void solve() {
    int n;
    cin >> n;
    Points ps(n);
    for (auto &[x, y]: ps) cin >> x >> y;
    Point po;
    cin >> po.x >> po.y;
    db res = 0;
    for (int i = 0, lst = n - 1, nxt = 1; i < n; i++, lst = (lst + 1) % n, nxt = (nxt + 1) % n) {
        Vec v1 = ps[nxt] - ps[i], v2 = ps[lst] - ps[i];
        db theta = acosl(cos_t(v1, v2));
        res += (pi - theta) * len(po - ps[i]);
    }
    cout << fixed << setprecision(3) << res << "\n";
    return;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T = 1;
    cin >> T;
    for (int i = 1; i <= T; i++)
        cout << "Case #" << i << ": ", solve();
}

C. Caesar Cipher

#include <bits/stdc++.h>

using namespace std;


void solve() {
    int n, m;
    cin >> n >> m;
    string a, b, c;
    cin >> a >> b >> c;
    int d = (a.front() - b.front() + 26) % 26;
    for (auto i: c)
        cout << char((i - 'A' + d) % 26 + 'A');
    cout << "\n";
    return ;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T = 1;
    cin >> T;
    for( int i = 1 ; i <= T ; i ++ )
        cout << "Case #" << i << ": ", solve();
    return 0;
}

D. Take Your Seat

打表找找规律就好了

#include <bits/stdc++.h>

using namespace std;

#define ll long long

using vi = vector<int>;

void solve() {
    int n, m;
    cin >> n >> m;
    long double x = 1, y = 0;
    for (int i = 1; i <= m; i++)
        x += 1, y += 2;
    cout << fixed << setprecision(6) << (n == 1 ? 1.0 : 0.5) << " " << x / y << "\n";
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;


    for (int i = 1; i <= T; i++) {
        cout << "Case #" << i << ": ";
        solve();
    }

    return 0;
}

F. Moving On

离线做,询问按照\(w\)进行排序。然后每次加入一个点之后,把进行一次松弛操作。

#include<bits/stdc++.h>

using namespace std;

using i32 = int32_t;

#define int long long

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<pii> a;
    for (int i = 1, x; i <= n; i++)
        cin >> x, a.emplace_back(x, i);
    sort(a.begin(), a.end());
    vector g(n + 1, vi(n + 1));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) cin >> g[i][j];
    vector<array<int, 4>> op(q);
    for (int id = 0; auto &[w, u, v, idx]: op)
        cin >> u >> v >> w, idx = id++;
    sort(op.begin(), op.end());
    vi res(q);
    for (int t = 0; const auto &[w, u, v, idx]: op) {
        while (t < n and a[t].first <= w) {
            int x = a[t].second;
            for (int i = 1; i <= n; i++)
                for (int j = 1; j < i; j++)
                    g[i][j] = g[j][i] = min(g[i][j], g[i][x] + g[x][j]);
            t++;
        }
        res[idx] = g[u][v];
    }
    for (auto &i: res) cout << i << "\n";
    return;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T = 1;
    cin >> T;
    for (int i = 1; i <= T; i++)
        cout << "Case #" << i << ":\n", solve();
    return 0;
}

G. Factories

这本身是一个无根树,所以先找一个不是叶子的点做根。

\(dp[i][j]\)表示\(i\)的子树中选择\(k\)个叶子点的最小代价,\(u\)是根节点,\(v\)是子节点,则转移如下

\[dp[u][i] = dp[u][i-1] + dp[v][j] + w * (k - j) * j \]

其中\(w*(k-j)*j\)表示从\(v\)到\(u\)的边产生的贡献。

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll inf = 1e18;

const int N = 1e5 + 6;

int n,k;
ll dp[N][110];
vector<pair<int,ll>> e[N];

void dfs(int u,int prv){
    for (auto [v,w] : e[u]) if (v != prv){
        dfs(v,u);
        for (int i = k;i >= 0;i--){
            for (int j = i;j >= 0;j--){
                dp[u][i] = min(dp[u][i],dp[u][i - j] + dp[v][j] + w * (k - j) * (j));
            }
        }
    }
}

void solve(){
    cin >> n >> k;

    for (int i = 1;i <= n;i++){
        e[i].clear();
    }

    for (int i = 1;i <= n - 1;i++){
        int u,v,w;cin >> u >> v >> w;
        e[u].push_back({v,w});
        e[v].push_back({u,w});
    }
    int root = -1;
    if (k == 1){
        cout << 0 << endl;
        return;
    }
    for (int i = 1;i <= n;i++){
        if (e[i].size() != 1){
            root = i;
            break;
        }
    }
    for (int i = 1;i <= n;i++){
        for (int j = 0;j <= k;j++){
            dp[i][j] = inf;
        }
    }
    for (int i = 1;i <= n;i++){
        dp[i][0] = 0;
    }
    for (int i = 1;i <= n;i++){
        if (e[i].size() == 1){
            dp[i][1] = 0;
        }
    }
    if (root == -1){
        int ans = 0;
        for (int i = 1;i <= n;i++){
            for (auto [v,w] : e[i]){
                ans += w;
            }
        }
        cout << ans / 2 << endl;
    }else{
        dfs(root,-1);
        cout << dp[root][k] << endl;
    }


}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;cin >> T;


    for (int i = 1;i <= T;i++){
        cout << "Case #" << i << ": ";
        solve();
    }

    return 0;
}

H. Fight Against Monsters

贪心做,先计算出击败一个怪物需要的次数\(cnt\),然后按照\(\frac {stk} {cnt}\)从大到小逐个击杀。

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;

void solve() {
    int n;
    cin >> n;
    vector<tuple<double, int, int>> a;
    int sum = 0;
    for (int i = 1, hp, atk; i <= n; i++) {
        cin >> hp >> atk, sum += atk;
        int l = 1, r = hp, cnt = -1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (mid * (mid + 1) / 2 >= hp) cnt = mid, r = mid - 1;
            else l = mid + 1;
        }
        a.emplace_back(double(atk) / (double) cnt, atk, cnt);
    }
    sort(a.begin(), a.end(), greater<>());
    int res = 0;
    for (auto &[x, atk, cnt]: a) {
        res += cnt * sum;
        sum -= atk;
    }
    cout << res << "\n";
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int TC;
    cin >> TC;
    for (int i = 1; i <= TC; i++)
        cout << "Case #" << i << ": ", solve();
    return 0;
}

K. Vertex Covers

我们可以把点分成两部分,然后可以先计算左边的部分,如果左边有一个点不在集合中,则左边所有与他相连的点都必须在集合内,否则不合法。

同理,在枚举右边的集合。对于一个合法的集合,当右边一个点不在集合中,则与这个点所有相连的左侧的点必须在左侧集合中,我们可以根据这个求出左边集合的必选点,这左边集合必须是必选点的超集。

我们可以用高维前缀和,处理出左边集合每个集合超集的和。

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;

#define int long long

using vi = vector<int>;


void solve(){
	int n, m, p;
	cin >> n >> m >> p;

	vi val(n);
	for(auto &i : val) cin >> i;

	vector e(n ,vi(n));
	for(int u, v; m; m --) {
		cin >> u >> v, u --, v --;
		e[u][v] = e[v][u] = 1;
	}

	int n1 = n / 2, n2 = n - n1;
	vi f(1<<n1);
	for(int i = 0; i < (1<<n1); i ++ ){
		auto tmp = 1;
		for(int j = 0; j < n1 and tmp; j ++){
			if(i & (1<<j)) tmp = tmp * val[j] % p;
			else {
				for(int k = 0; k < n1 and tmp; k ++)
					if(not (i & (1<<k)) and e[j][k] ) // k 也不在集合中,且 j,k 之间有边 
						tmp = 0;
			}
		}
		f[i] = tmp;
	}

	// SOS DP, 枚举超集
	for(int j = 0; j < n1; j ++)
		for(int i = 0; i < (1<<n1); i ++)
			if(not(i &(1<<j))) f[i] = (f[i] + f[i ^ (1<<j)]) % p;

	int res = 0;
	for(int i = 0; i < (1<<n2); i ++){
		int tmp = 1;
		for( int j = 0; j < n2 and tmp; j ++){
			if(i & (1<<j)) tmp = tmp * val[n1 + j] % p;
			else {
				for(int k = 0; k < n2 and tmp; k ++)
					if(not (i & (1<<k)) and e[n1 + j][n1 + k])
						tmp = 0;
			}
		}
		if(tmp == 0) continue;
		int need = 0;
		for(int j = 0; j < n2; j ++){
			if(i & (1<<j)) continue;
			for(int k = 0; k < n1; k ++) 
				if(e[n1 + j][k]) // j 没被选 且 j,k 之间存在边,则 k 必选
					need |= (1<<k);
		}
		res = (res + tmp * f[need] % p) % p;
	}
	cout << res << "\n";
	return;
}

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	for(int i = 1; i <= T; i ++)
		cout << "Case #" << i << ": ", solve();
	return 0;
}

标签:Provincial,Multi,Contest,int,cin,long,vector,Vec,using
From: https://www.cnblogs.com/PHarr/p/18163922

相关文章

  • AtCoder Beginner Contest 351 E - Jump Distance Sum 切比雪夫距离与曼哈顿距离的转
    先说知识点。曼哈顿距离:横纵坐标距离差的绝对值的和,即|X1-X2|+|Y1-Y2|,离(0,0)点曼哈顿距离为1的点形成的是一个旋转45度后的正方形切比雪夫距离:横纵坐标距离差的绝对值的最大值,即max(|X1-X2|,|Y1-Y2|),离(0,0)点切比雪夫距离为1的点形成的是一个不旋转的正方形曼哈......
  • [Paper Reading] DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D
    名称DETR3D:3DObjectDetectionfromMulti-viewImagesvia3D-to-2DQueries时间:21.10机构:mit/CMU/StanfordTL;DR一种利用Transformer做E2E的3D目标检测方法,在nuScenes自动驾驶数据集上取得很好效果。Method主要创新点在于2D-to-3DFeatureTransforms模块,细节如图描......
  • The 2nd GUAT Collegiate Programming Contest (Round 1)
    第二届GUAT大学生程序设计大赛第一场题解(A-M)前言比赛的内容主要包括计算机科学的常用算法,基本的计算理论,(如:离散数学,具体数学,组合数学基础),数据结构基础,程序设计语言(规定是C/C++或者是Java、Python)。在本项比赛中考察学生的不仅仅是能够完成指定任务的程序,更要求在完成程序的功......
  • The 2018 ICPC Asia Qingdao Regional Programming Contest (The 1st Universal Cup,
    Preface久违地VP一场,虽然打的挺唐但勉强写出了8题前中期EFB三开三卡属实有点难受,而且B题这个套路大合集我和徐神两个人写了快200行也占用了一定机时但好在后面把卡住的题慢慢都写出来了,然后最后40min冲刺L题成功比较可惜的是I这个开场看的题没有再细想一步,感觉想到在线段树上D......
  • The 2023 ICPC Asia Jinan Regional Contest
    目录写在前面DIAG写在最后写在前面比赛地址:https://codeforces.com/gym/104901。以下按个人向难度排序。SUA的题确实牛逼,把我这种只会套路的沙比狠狠腐乳了。D签到。直接枚举\([L,\min(R,L+10)]\)检查即可。///*By:Luckyblock*/#include<bits/stdc++.h>#defi......
  • 开源相机管理库Aravis例程学习(四)——multiple-acquisition-signal
    目录简介例程代码函数说明g_main_loop_newg_main_loop_rung_main_loop_quitg_signal_connectarv_stream_set_emit_signalsQ&A回调函数的同步调用与异步调用帧丢失问题简介本文针对官方例程中的:02-multiple-acquisition-signal做简单的讲解。并简单介绍其中调用的g_main_loop_new......
  • Converge: QoE-driven Multipath Video Conferencing over WebRTC 概略
    这是一片2023sigcomm的文章。论文指出多摄像头(multiplecamera)和高分辨率(highresolution)场景下,视频会议的QoE还有提升空间,而作者将提升QoE的目光转到多路传输(multipath)上。所以总体来看这是一篇利用多路传输来优化视频会议体验的文章。常用的多路协议包括MPTCP,MPQUIC,MPRTP经过......
  • The 2022 ICPC Asia Xian Regional Contest / ICPC 西安 2022 (ABDHJKL)
    本文搬运自本人的知乎文章。https://zhuanlan.zhihu.com/p/588162564好久没有在补题之后写题解的习惯了。但是最近感觉有些题目的思路即使在题目通过后仍然难以理清,因此觉得需要写些东西帮助自己整理思路,另外也方便以后翻看积累到的技巧。J.StrangeSum题目链接Problem-J......
  • 2022 China Collegiate Programming Contest (CCPC) Mianyang | 2022 CCPC 绵阳(MAED
    搬运自本人知乎文章。https://zhuanlan.zhihu.com/p/588646549M.Rock-Paper-ScissorsPyramid题目链接Problem-M-Codeforces题意有一个长度为\(n\)的石头剪刀布序列,每个元素是RPS(石头、布、剪刀)中的一个,我们需要用这个序列构造一个三角,三角的底层为这个序列,第\(i(......
  • The 2022 ICPC Asia Xian Regional Contest
    The2022ICPCAsiaXianRegionalContestJ.StrangeSum题意:给定n个数,选定最多不超过两个数字的和的最大值思路:签到voidsolve(){lln;cin>>n;vector<ll>a(n+1);for(inti=1;i<=n;i++)cin>>a[i];llans=0;sort(a.begin()......