首页 > 其他分享 >CodeForces 1239E Turtle

CodeForces 1239E Turtle

时间:2024-01-29 22:23:14浏览次数:28  
标签:Turtle typedef int ll CodeForces long maxn 1239E define

洛谷传送门

CF 传送门

放放/ll/ll/ll。

这题是个性质题。

首先第一排一定是升序,第二排一定是降序。考虑第一排若存在 \(i < j\) 使得 \(a_{1, i} > a_{1, j}\),那么交换这两个数不会变劣。第二排类似。

然后发现在 \(1\) 走下去或在 \(n\) 走下去最优。考虑先求出从 \(1\) 走下去的答案 \(k\),然后令 \(w_i = a_{1, i + 1} - a_{2, i}\),那么从位置 \(x\) 走下去的答案就是 \(k + \sum\limits_{i = 1}^{x - 1} w_i\)。容易发现 \(w_i\) 单增,所以只可能在 \(1\) 或 \(n\) 处取到最值。

最后发现 \(a_{1, 1}, a_{2, n}\) 一定是最小值或次小值。若不是则把最小值(或次小值)交换过去不会变劣。

然后直接写个背包 dp,bitset 优化一下就完了。

时间复杂度 \(O(n^2 \sum a) = O(n^3 \max a)\)。

code
// Problem: E. Turtle
// Contest: Codeforces - Codeforces Round 594 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1239/E
// Memory Limit: 512 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 27;

int n, a[maxn << 1], b[2][maxn];
bitset<maxn * 50000> f[maxn << 1][maxn];

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n * 2; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + n * 2 + 1);
	b[0][1] = a[1];
	b[1][n] = a[2];
	f[2][0].set(0);
	int s = 0;
	for (int i = 3; i <= n * 2; ++i) {
		s += a[i];
		for (int j = 0; j <= i - 2; ++j) {
			f[i][j] = f[i - 1][j];
			if (j) {
				f[i][j] |= (f[i - 1][j - 1] << a[i]);
			}
		}
	}
	vector<int> A, B;
	int p = -1;
	for (int i = s / 2; ~i; --i) {
		if (f[n * 2][n - 1].test(i)) {
			p = i;
			break;
		}
	}
	for (int i = n * 2, j = n - 1; i >= 3; --i) {
		if (p >= a[i] && j && f[i - 1][j - 1].test(p - a[i])) {
			--j;
			A.pb(a[i]);
			p -= a[i];
		} else {
			B.pb(a[i]);
		}
	}
	sort(A.begin(), A.end());
	for (int i = 2; i <= n; ++i) {
		b[0][i] = A[i - 2];
	}
	sort(B.begin(), B.end(), greater<int>());
	for (int i = 1; i < n; ++i) {
		b[1][i] = B[i - 1];
	}
	for (int i = 0; i < 2; ++i) {
		for (int j = 1; j <= n; ++j) {
			printf("%d ", b[i][j]);
		}
		putchar('\n');
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:Turtle,typedef,int,ll,CodeForces,long,maxn,1239E,define
From: https://www.cnblogs.com/zltzlt-blog/p/17995487

相关文章

  • Codeforces Round 921 (Div. 2)
    目录写在前面ABCDE写在最后写在前面比赛地址:https://codeforces.com/contest/1925。在床上躺了两天终于复活了妈的。A发现字符串\(s\)合法当且仅当\(s\)可以被分为至少\(n\)段,其中每段都包含\(k\)种字符至少1次。正确性可以归纳证明,这里懒得写了感性理解下。于是......
  • Codeforces Round 921 (Div. 2)
    A-WeGotEverythingCovered!难度:⭐题目大意给定n和k两个整数,要求用前k个小写字母组成一个字符串;该字符串的子串应包含所有由前k个字母组成的长度为n的字符串全排列;要求输出最短的满足条件的字符串;解题思路这题题目挺唬人,但其实是个水题;所谓最短,其实......
  • Codeforces Round 921 (Div. 2)(A~E)
    好久不打用小号水了一场,赛时坑坑拌拌勉强四题,以为美美上分,结果重测时卡掉了我没细想复杂度就交了的B题,这下小丑了 A.WeGotEverythingCovered!直接输出n次连续前k个字母即可#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongsignedmain(){ ios......
  • Codeforces Round 921 (Div. 2)
    CodeforcesRound921(Div.2)A-WeGotEverythingCovered!解题思路:以前\(k\)个字符都出现过至少一次为一轮,构造\(n\)轮即可。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecon......
  • Codeforces Round 920 (Div. 3)
    A-Square难度:⭐题目大意给定正方形的四个顶点,求该正方形的面积;解题思路根据四个点找出长和宽即可,因为数据范围有负数,为了方便我都进行了移位;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio(false);cin.tie(0......
  • Codeforces Round 921 (Div. 2) A-D
    倒叙讲题预警()传送门:https://codeforces.com/contest/1925D.GoodTrip题意:有n个小盆友,其中m对是好盆友,每队好盆友有友谊度fi。老师要举办k次远足,每次挑一对小盆友去,被挑到的小盆友友谊值+1。如果一对儿童不是朋友,那么他们的友谊值为0,在以后的游览中不会改变。求所有被选中参......
  • Codeforces Round 921 (Div. 1) 记录(A-D)
    比赛链接:https://codeforces.com/contest/1924官解链接:https://codeforces.com/blog/entry/125137这场整体来说表现还可以,最终performance\(2431\),delta\(+33\)。A.DidWeGetEverythingCovered?还不错的贪心题。进入状态有点慢,写了几个小错误B.SpaceHarbourC.Frac......
  • Codeforces Round 921 (Div. 2) 赛后总结
    搜索替换int->longlong是一个好习惯赛后5分钟就改对E题了,好可惜。不过1个小时都没能做出来,也说明自己不太熟练吧线段树善于维护满足区间可加性的一类信息,这与本题中的代价和相契合。特殊之处在于其修改方式。每个区间会在线段树上被划分为\(O(log_{2}n)\)个小区间即使是最......
  • Codeforces Round 921 (Div. 2)
    CodeforcesRound921(Div.2)比赛地址A.WeGotEverythingCovered!思路这个就是一个简单的拼接,这里很容易的发现我们最后最优的方法就是将所要拼写的字母按顺序拼接成n份就好了,但是这里需要主义一下简单的优化Code#include<bits/stdc++.h>usingnamespacestd;#define......
  • CodeForces 1924C Fractal Origami
    洛谷传送门CF传送门对这种题一点办法都没有。。。可以手动折叠发现\(n=3\)时\(M=2+2\sqrt{2},V=2+4\sqrt{2}\)。于是大胆猜结论,第二次折叠开始,每次产生的山谷和山峰的长度相等。为什么呢?考虑从第二次折叠开始,设当前纸的层数为\(k\)(事实上若当前是第\(i\)......