枚举 a 串中的每个数 u,之后在 b 串中找到一个尽量大的数字 v,且(u+v <=x), 如果在枚举某个 u 的时候无法找到与匹配的 v,就输出 no
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/const ll mxn =2e3+10;
ll ar[mxn], br[mxn];intmain(){Run();
ll T;sd(T);while(T --){
ll n, x;sc("%lld %lld",&n,&x);for_(i,1, n){sd(ar[i]);}for_(i,1, n){sd(br[i]);}sort(ar +1, ar +1+ n);sort(br +1, br +1+ n, greater<ll>());
ll mk =1;for_(i,1, n){
ll flag =0;
ll cha = x - ar[i];for_(j,1, n){if(br[i]==-1|| br[i]> cha)continue;
br[i]=-1;
flag =1;break;}if(! flag){
mk =0;break;}}if(mk)pr("Yes\n");elsepr("No\n");}return0;}
B. Elimination(贪心)
思路
在第一场比赛中可能产生前 100 名的最低分是 a+b,
在第二场比赛中可能产生前 100 名的最低分是 c+d,
所有答案是 max (a+b, c+d).
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/const ll mxn =2e3+10;
ll ar[mxn], br[mxn];intmain(){Run();
ll T;sd(T);while(T --){
ll a, b, c, d;sc("%lld %lld %lld %lld",&a,&b,&c,&d);pr("%lld\n",max(a + b, c + d));}return0;}
C. Division(贪心、分解质因数)
思路
x 能整除 p,说明 x 是 p 的因子,
q 不能整除 x,说明 q 不是 x 的因子,
x 的最大取值是 p,要想获得 x 尽量大,就要从 p 中减少尽可能少的质因子,从而形成 x,
我们对 q 进行质因子分解(唯一分解定理)
q
=
a
1
b
1
∗
a
2
b
2
∗
.
.
.
∗
a
i
b
i
q=a_1^{b1}*a_2^{b2}*...*a_i^{bi}
q=a1b1∗a2b2∗...∗aibi(ai 为素数),我只需要改变某一个质因子
a
x
a_x
ax 在 p 中出现的次数小于 q 中出现的次数, 那么改变之后形成的
p
′
p'
p′ 就不能被 q 整除了,这个时候
p
′
p'
p′ 就是一个可能的答案,但不是最有的答案,
我枚举 q 中的所有质因子
a
i
ai
ai, 对所有得出的
p
′
p'
p′ 维护一个最优值就是答案了,
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/const ll mxn =2e3+10;
ll ar[mxn], br[mxn];
vector<Pir> fac;voidget_fac(ll n){
fac.clear();for(ll i =2; i * i <= n; i ++){if(n % i ==0){
ll num =0;while(n % i ==0) n /= i, num ++;
fac.pb(m_p(i, num));}}if(n !=1) fac.pb(m_p(n,1));}
ll q_pow(ll a, ll b){
ll res =1;while(b){if(b &1) res = res * a;
a = a * a;
b >>=1;}return res;}intmain(){Run();
ll T;sd(T);while(T --){
ll p, q;sc("%lld %lld",&p,&q);if(p % q){pr("%lld\n", p);continue;}
ll ans =0;get_fac(q);for(auto x : fac){
ll t = p;
ll num =0;while(t % x.fi ==0) t /= x.fi, num ++;
ans =max(ans, t *q_pow(x.fi,max(min(num, x.se -1),0LL)));}pr("%lld\n", ans);}return0;}
D. Divide and Sum
题意
给我 2n 个数存放在 ar 数组中,我们任选 n 个数形成一个 p 数组,剩下的 n 个元素形成 q 数组,
对 p 数组进行从小到大排序,
对 q 数组进行从大到小排序,
p、q 中对应位置的元素为 {
x
i
,
y
i
x_i, y_i
xi,yi},
定义
∑
i
=
1
i
<
=
n
f
(
p
,
q
)
=
∣
x
i
−
y
i
∣
\sum _{i=1}^{i<=n}f(p,q)=|x_i-y_i|
∑i=1i<=nf(p,q)=∣xi−yi∣,
因为 p 有
C
2
n
n
中
选
择
方
案
,
C_{2n}^{n} 中选择方案,
C2nn中选择方案,现在让我们求
∑
f
(
p
,
q
)
\sum f(p,q)
∑f(p,q)
思路
一位一位的考虑每个数对答案的贡献,
说结论,
当 i>n 的时候,ar [i] 总是在
f
(
p
,
q
)
=
∣
x
i
−
y
i
∣
f(p,q)=|x_i-y_i|
f(p,q)=∣xi−yi∣ 中总是作为被减数,
当 i<=n 的时候,ar [i] 总是在
f
(
p
,
q
)
=
∣
x
i
−
y
i
∣
f(p,q)=|x_i-y_i|
f(p,q)=∣xi−yi∣ 中总是作为减数,
所以最终答案就是:
C
2
n
n
∗
(
∑
i
=
n
+
1
i
<
=
2
∗
n
−
∑
i
=
1
i
<
=
n
)
C_{2n}^{n}*(\sum_{i=n+1}^{i<=2*n}-\sum_{i=1}^{i<=n})
C2nn∗(∑i=n+1i<=2∗n−∑i=1i<=n)
给出一个理解,
因为的打当 i<=n 的时候,ar [i] 我们无论把它选择 p 还是 q 中,假设它被选进了 p 数组中,那么在对应位置上的 q 数组的那个元素的值一定 >=ar [i];
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(998244353)
using namespace std;/*=========================ACMer===========================*/const ll mxn =1e6;
ll p[mxn], ar[mxn];voidpre_do(ll n){
p[0]=1;for_(i,1, n) p[i]= p[i -1]* i % mod;}
ll q_pow(ll a, ll b){
ll res =1;while(b){if(b &1) res = res * a % mod;
a = a * a % mod;
b >>=1;}return res;}
ll inv(ll n){returnq_pow(n, mod -2);}
ll C(ll m, ll n)//从 m 中选择 n 个物品的方案数{return p[m]*inv(p[n])% mod *inv(p[m - n])% mod;}intmain(){Run();
ll n;sd(n);pre_do(2* n);for_(i,1,2* n){sd(ar[i]);}sort(ar +1, ar +1+2* n);
ll ans =0;for_(i,1,2* n){if(i <= n){
ans =(ans - ar[i]+ mod)% mod;}else{
ans =(ans + ar[i])% mod;}}pr("%lld\n", ans *C(2* n, n)% mod);return0;}