A - Min Difference
#include <iostream>
#include <cstring>
#include <algorithm>
#include<unordered_map>
typedef long long LL;
using namespace std;
const int N = 2e5+10;
LL a[N];
LL b[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++) cin>>b[i];
sort(a+1,a+1+n);
sort(b+1,b+1+m);
LL y=0x3f3f3f3f3f;
for(int i=1;i<=n;i++)
{
int x=a[i];
int l=1;int r=m;
while (l < r)
{
int mid = l + r >> 1;
if (b[mid] >= x) r = mid;
else l = mid + 1;
}
y=min(y,abs(a[i]-b[l]));
if(l-1>=1)
y=min(y,abs(a[i]-b[l-1]));
if(l+1<=m)
y=min(y,abs(a[i]-b[l+1]));
}
cout<<y<<endl;
}
B - Querying Multiset
#include <iostream>
#include <cstring>
#include <algorithm>
#include<unordered_map>
#include<queue>
typedef long long LL;
using namespace std;
const int N = 2e5+10;
LL a[N];
LL b[N];
LL ans=0;
int main()
{
LL t;
cin>>t;
priority_queue<LL, vector<LL>, greater<LL>> heap;
while(t--)
{
LL n,m;
cin>>n;
if(n==1)
{
cin>>m;
heap.push(m-ans);
}
else if(n==2)
{
cin>>m;
ans+=m;
}
else if(n==3)
{
if(heap.size())
{
LL k=heap.top();
heap.pop();
cout<<k+ans<<endl;
}
}
}
}
B - XOR Matching 2
题意:给两个数组a,b。看是否可以通过重新排列b数组,使得
a
1
a_1
a1 ^
b
1
b1
b1
=
a
2
=a_2
=a2 ^
b
2
b2
b2
=
a
3
=a_3
=a3 ^
b
3
b3
b3
=
.
.
.
.
.
.
=......
=......
=
a
n
=a_n
=an ^
b
n
=
x
bn=x
bn=x,问
x
x
x 的可能方案数。
思路:我们可以先把x的所有可能放入一个集合A中,这个集合A是由 : 所有的 x 的可能取值构成,
A
=
A=
A= {a1 ^ b1, a1^ b2, a1^ b3…}。对于
A
A
A集合中的某一个元素
A
i
A_i
Ai,我们要查询是否存在一个b数组的全排列,使得与a数组异或得到的结果恰好都是
A
i
A_i
Ai。为了方便判断:我们知道异或是一种逆运算,所以
A
i
A_i
Ai^
a
i
a_i
ai可以得到
c
i
c_i
ci,我们称这样得到的集合为集合C,而刚刚的原数组b不妨称为集合B。我们只关心,集合C是不是集合B的一种全排列,而不关系他们之间的顺序,因为如果存在一个
c
i
c_i
ci不是原数组B的元素,那说明存在
b
i
b_i
bi^
a
i
a_i
ai !=
A
i
A_i
Ai,说明这样的序列不可能使得a,b数组每一个元素满足题意。所以判断的条件就是集合C是否与集合B相等,我们只需要给b数组,c数组同时排序,把元素对应比较即可实现判断两集合是否相等。如果集合相等,那么这个
x
x
x就是符合题意的一种答案。
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
typedef long long LL;
LL a[N], b[N];
vector<LL> res;
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
for (int i = 0; i < n; i++)
scanf("%lld", &b[i]);
vector<LL> c;
sort(b, b + n);
for (int i = 0; i < n; i++)
{
LL x = a[0] ^ b[i];
c.clear();
for (int j = 0; j < n; j++)
{
LL y = x ^ a[j];
c.push_back(y);
}
sort(c.begin(), c.end());
int flag = 1;
for (int j = 0; j < n; j++)
{
if (b[j] != c[j])
{
flag = 0;
break;
}
}
if (flag) res.push_back(x);
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++)
printf("%d\n", res[i]);
return 0;
}
Safety Journey
题意:给定一个完全图,但是图中有m条边失去连通性。再给k天和n个点,让你求k天从1号点出发回到1号点的方案。
思路:显然是dp求方案数,定义
g
[
i
]
[
j
]
为
经
过
i
天
,
身
处
j
城
时
的
最
大
方
案
数
g[i][j]为经过i天,身处j城时的最大方案数
g[i][j]为经过i天,身处j城时的最大方案数。
那么对于任何一个点,显然它可以由前一天的任何一个其他的点经过,所以
g
[
i
]
[
j
]
+
=
∑
k
=
1
n
g
[
i
−
1
]
[
k
]
g[i][j]+=\sum_{k=1}^ng[i-1][k]
g[i][j]+=∑k=1ng[i−1][k], 这里面要排除原地踏步的点,所以
g
[
i
]
[
j
]
g[i][j]
g[i][j]要减去
g
[
i
−
1
]
[
j
]
g[i-1][j]
g[i−1][j],还要减去当前
g
[
i
]
[
j
]
的
邻
边
g[i][j]的邻边
g[i][j]的邻边,因为刚刚算的是完全图的全部方案,题目有m条边失去连通性,需要减去。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
const int mod =998244353;
int e[N],ne[N],h[N],idx;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
typedef long long LL;
LL g[5010][5010];
int main()
{
memset(h,-1,sizeof h);
int n,m,k;
cin>>n>>m>>k;
while(m--)
{
int a,b;
cin>>a>>b;
add(a, b);
add(b, a);
}
g[0][1]=1;
for(int i=1;i<=k;i++)
{
LL sum=0;
for(int j=1;j<=n;j++)
{
sum+=g[i-1][j];
}
for(int j=1;j<=n;j++)
{
g[i][j]=sum-g[i-1][j];
for(int ii=h[j];ii!=-1;ii=ne[ii])
{
int u=e[ii];
g[i][j]-=g[i-1][u];
}
g[i][j]=g[i][j]%mod;
}
}
cout<<g[k][1]<<endl;
}