ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #744 (Div. 3)

2022-05-17 23:02:36  阅读:155  来源: 互联网

标签:744 int Codeforces long xs Div 贪心 define


比赛链接

Codeforces Round #744 (Div. 3)

E1. Permutation Minimization by Deque

输入 \(n\) 个数,按照输入顺序根据双端队列的插入规则使得字典序最小

解题思路

思维,贪心

贪心策略:按顺序遍历数组元素,模拟双端队列,如果当前元素大于队首元素则放队尾否则放队首

证明:对于当前数,有两种选择,放队尾或队首,按贪心策略得到的部分要比不按贪心策略得到的部分的字典序要小,对于后面的数而言当前整体贪心解要比非贪心解要好

  • 时间复杂度:\(O(n)\)

代码

// Problem: E1. Permutation Minimization by Deque
// Contest: Codeforces - Codeforces Round #744 (Div. 3)
// URL: https://codeforces.com/contest/1579/problem/E1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}


int main()
{
    int t;
    for(cin>>t;t;t--)
    {
    	int n;
    	cin>>n;
    	deque<int> q;
    	while(n--)
    	{
    		int x;
    		cin>>x;
    		if(q.size()&&x<q.front())q.push_front(x);
    		else
    			q.pb(x);
    	}
    	while(q.size())cout<<q.front()<<' ',q.pop_front();
    	puts("");
    }
    return 0;
}

E2. Array Optimization by Deque

输入 \(n\) 个数,按照输入顺序根据双端队列的插入规则使得逆序对最小

解题思路

贪心

遍历每个数,而前面的数已经固定,当前数要么放前面要么放后面,考虑当前数对答案的贡献,由于当前数与之前的数组成的整体对后面数的逆序对没有影响,所以当前数对答案的贡献越小越好,即选择放前面和放后面逆序对较小的位置

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: E2. Array Optimization by Deque
// Contest: Codeforces - Codeforces Round #744 (Div. 3)
// URL: https://codeforces.com/contest/1579/problem/E2
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}


const int N=2e5+5;
int t,n,a[N],tr[N];
vector<int> xs;
int find(int x)
{
	return lower_bound(xs.begin(),xs.end(),x)-xs.begin()+1;
}
int ask(int x)
{
	int res=0;
	for(;x;x-=x&-x)res+=tr[x];
	return res;
}
void add(int x,int y)
{
	for(;x<=n;x+=x&-x)tr[x]+=y;
}
int main()
{
	help;
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	xs.clear();
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    		xs.pb(a[i]);
    		tr[i]=0;
    	}
    	sort(xs.begin(),xs.end());
    	xs.erase(unique(xs.begin(),xs.end()),xs.end());
    	for(int i=1;i<=n;i++)a[i]=find(a[i]);
    	LL res=0;
    	for(int i=1;i<=n;i++)
    	{
    		int x=ask(a[i]-1),y=i-1-ask(a[i]);
    		res+=min(x,y);
    		add(a[i],1);
    	}
    	cout<<res<<'\n';
    }
    return 0;
}

标签:744,int,Codeforces,long,xs,Div,贪心,define
来源: https://www.cnblogs.com/zyyun/p/16282761.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有