其他分享
首页 > 其他分享> > Codeforces Round #744 (Div. 3)

Codeforces Round #744 (Div. 3)

作者:互联网

比赛链接

Codeforces Round #744 (Div. 3)

E1. Permutation Minimization by Deque

输入 \(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\) 个数,按照输入顺序根据双端队列的插入规则使得逆序对最小

解题思路

贪心

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

代码

// 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