ICode9

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

2022/1/5

2022-01-05 23:34:55  阅读:146  来源: 互联网

标签:ch hash ll 2022 pair const define


2022/1/5

[ Paimon Sorting ]( D (codeforces.com) )

思路

设前i-1个数的最大值是 Max

  1. 当a[i]<Max 时,只有在最后一轮交换时才产生贡献,贡献为前i个大于a[i]的数量,(去重后
  2. 当a[i]==Max时,不产生贡献
  3. 当a[i]>Max时,产生的贡献为 2+cnt. cnt为第二次出现Max的位置到i的数个数。

参考代码

#include<bits/stdc++.h>
#define ll  long long
#define pii pair<int, int >
#define se second
#define pb push_back
#define pf push_front
#define si size()
#define db double
#define ls (p<<1)
#define rs (p<<1|1)

#define fi first
#define se second
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void Prin(ll x){if(x < 0){putchar('-');x = -x;}if(x > 9) Prin(x / 10);putchar(x % 10 + '0');}

const int qs=2e5+7;
const ll mod=998244353;


ll T,n,k,u[qs],c[qs],a[qs]; 

ll lb(ll x){
	return x&(-x);
}

void add(ll x,ll val){
	for(;x<=n;x+=lb(x)) c[x]+=val;
}

ll ask(ll x){
	ll ret=0;
	for(;x>0;x-=lb(x)) ret+=c[x];
	return ret; 
}

int main(){
	T=read();
	while(T--){
		n=read();
		for(int i=1;i<=n;++i) a[i]=read(),u[i]=0,c[i]=0;
		ll Max=-1,ans=0,flag=0,cnt=0;
		for(int i=1;i<=n;++i) {
			if(!u[a[i]]) add(a[i],1),u[a[i]]=1;
			if(i==1){
				Max=a[i];
				cout<<"0";
				continue; 
			}
			if(a[i]==Max||flag) cnt++;
			if(a[i]>Max){
				flag=0;
				ans=ans+2+(cnt ? cnt-1 : 0);
				Max=a[i];
				cnt=0;
			}	
			else if(a[i]==Max) {
				//cout<<"i="<<i<<" Max="<<Max<<"\n";
				flag=1;
			}
			else{
				ans=ans+ask(Max)-ask(a[i]);
			}
			cout<<" "<<ans;
		}
		cout<<"\n";
		
	}

	return 0;
}



[ J. Xingqiu's Joke ]( Problem - J - Codeforces )

思路

设 a<b ,c=b-a,g是c的质因子。

根据同余定理有: \(a \equiv b(modg)\)

那么对于\((a,b,c)\),我们需要将其转换为$ (\lfloor { \frac ag } \rfloor ,\lfloor { \frac bg } \rfloor,\frac cg)$ 或者我们需要将其转换为$ (\lceil { \frac ag } \rceil ,\lceil { \frac bg } \rceil,\frac cg)$ 。

在两者中取步数小的。

注意用unordered_map,map会超时。

unordered_map 对 pair 存储

struct hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const
    { 
        auto hash1 = hash<T1>{}(p.first); 
        auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2; 
    } 
}; 
unordered_map< pii ,ll,hash_pair> mp;

参考代码

#include<bits/stdc++.h>
#define ll  long long
#define pii pair<int, int >
#define se second
#define pb push_back
#define pf push_front
#define si size()
#define db double
#define ls (p<<1)
#define rs (p<<1|1)

#define fi first
#define se second
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void Prin(ll x){if(x < 0){putchar('-');x = -x;}if(x > 9) Prin(x / 10);putchar(x % 10 + '0');}

const int qs=2e5+7;
const ll mod=998244353;
struct hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const
    { 
        auto hash1 = hash<T1>{}(p.first); 
        auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2; 
    } 
}; 

ll T; 
vector<ll> v;
unordered_map< pii ,ll,hash_pair> mp;
ll dfs(ll a,ll c){
	if(a==1) return 0;
	if(c==1) return a-1;
	if(mp[{a,c}]) return mp[{a,c}];
	ll Min=a-1;
	for(int i=0;i<v.si;++i){
		ll p=v[i];
		if(c%p==0){
			ll x=a%p;
			Min=min(Min,min(x+1+dfs(a/p,c/p),p-x+1+dfs(a/p+1,c/p)));
		}
	}	
	return mp[{a,c}]=Min; 
}

int main(){
	T=read();
	while(T--){
		v.clear();
		ll a,b,c;
		a=read(),b=read();
		if(a<b) swap(a,b);
		c=a-b;
		for(int i=2;i*i<=c;++i){
			if(c%i==0){
				v.pb(i);
				while(c%i==0) c/=i;
			}
		}
		if(c>1) v.pb(c);
		cout<<dfs(b,a-b)<<"\n";
		
	}

	return 0;
}

标签:ch,hash,ll,2022,pair,const,define
来源: https://www.cnblogs.com/Suki-Sugar/p/15769302.html

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

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

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

ICode9版权所有