Problem A: Here Comes Santa Claus
给一个数列,要求分成若干组,要求每组至少2个数,使得所有组中位数的最大值与最小值之差尽量大,求这个值。
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (100000+10)
ll a[MAXN];
int main()
{
freopen("here_comes_santa_claus_input.txt","r",stdin);
freopen("A.out","w",stdout);
int T=read();
For(kcase,T) {
printf("Case #%d: ",kcase);
int n=read();
For(i,n) a[i]=read();
sort(a+1,a+1+n);
if(n==5) {
ll p1=(a[2]+a[1]);
ll p2=(a[5]+a[3]);
ll p=p2-p1;
ll p3=(a[3]+a[1]);
ll p4=(a[5]+a[4]);
ll _p=p4-p3;
p=max(p,_p);
if(p%2) cout<<p/2<<".5"<<endl;
else cout<<p/2<<endl;
}else{
ll p1=(a[2]+a[1]);
ll p2=(a[n]+a[n-1]);
ll p=p2-p1;
if(p%2) cout<<p/2<<".5"<<endl;
else cout<<p/2<<endl;
}
}
return 0;
}
Problem B1: Sum 41 (Chapter 1)
Given a positive integer P, please find an array of at most 100 positive integers which have a sum of 41 and a product of P, or output −1 if no such array exists.
If multiple such arrays exist, you may output any one of them.
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (100000+10)
typedef long long ll;
int p[MAXN],tot;
bool b[MAXN]={0};
void make_prime(int n)
{
tot=0;
Fork(i,2,n)
{
if (!b[i]) p[++tot]=i;
For(j,tot)
{
if (i*p[j]>n) break;
b[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
}
int st[MAXN];
map<ll,vector<int> > h;
void dfs(int x,int l,int s,ll p) {
// cout<<x<<" "<<l<<' '<<s<<' '<<p<<endl;
if(!s) {
// For(i,l-1) cout<<st[i]<<" ";
// cout<<":"<<p<<endl;
if(!h.count(p)|| SI(h[p])>l-1) {
vi v;
For(i,l-1) v.pb(st[i]);
h[p]=v;
}
return ;
}
if(x>s || p>1e9) return;
Fork(k,x,s) {
st[l]=k;
dfs(k,l+1,s-k,p*k);
}
}
int main()
{
freopen("sum_41_chapter_2_input.txt","r",stdin);
freopen("sum_41_chapter_2_output.txt","w",stdout);
dfs(1,1,41,1);
make_prime(1e4);
int T=read();
For(kcase,T) {
printf("Case #%d:",kcase);
ll n=read();
if(h.count(n)) {
int sz=h[n].size();cout<<' '<<sz;
Rep(i,sz) cout<<' '<<h[n][i];
cout<<endl;
}else {
puts(" -1");
}
}
return 0;
}
Problem C1: Back in Black (Chapter 1)
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
string s;
bool b[4000000+10];
bool op[4000000+10];
int main()
{
freopen("back_in_black_chapter_2_input.txt","r",stdin);
freopen("back_in_black_chapter_2_output.txt","w",stdout);
int T=read();
For(kcase,T) {
printf("Case #%d: ",kcase);
int n=read();
ll ans=0;
cin>>s;
For(i,n) b[i]=s[i-1]=='1';
For(i,n) if(b[i]) {
op[i]=1;++ans;
for(int j=i;j<=n;j+=i) b[j]^=1;
}else op[i]=0;
int q=read();
ll ans2=0;
For(i,q) {
int p=read();
if(op[p]) --ans;else ++ans;
op[p]^=1;
ans2+=ans;
}
cout<<ans2<<endl;
}
return 0;
}
Problem D: Today is Gonna be a Great Day
给一个长度为n的数列,维护2个操作,批量,求全局最大值的位置。
同余系下,操作等价于乘-1,乘2次会变回原来的值。答案在2n个数中。
用2个线段树分别维护乘1次和不乘时哪些数在数列中并维护最大值位置。
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (1000004+10)
class seg{
public:
ll mulv[MAXN<<2];
pair<ll,int> maxv[MAXN<<2],minv[MAXN<<2];
void pushUp(int o) {
maxv[o]=max(maxv[Lson],maxv[Rson]);
minv[o]=min(minv[Lson],minv[Rson]);
}
void pushDown(int o) {
if (mulv[o]==-1) {
mulv[Lson]*=-1;mulv[Rson]*=-1;
maxv[Lson].fi*=-1;minv[Lson].fi*=-1;
if(maxv[Lson].fi!=minv[Lson].fi) {
swap(minv[Lson],maxv[Lson]);
}
maxv[Rson].fi*=-1;minv[Rson].fi*=-1;
if(maxv[Rson].fi!=minv[Rson].fi) {
swap(minv[Rson],maxv[Rson]);
}
mulv[o]=1;
}
}
ll a[MAXN];
void build(int l,int r,int o) {
mulv[o]=1;
if (l==r) {
maxv[o]=minv[o]=mp(a[l],-l);
return;
}
int m=(l+r)>>1;
build(l,m,Lson);
build(m+1,r,Rson);
pushUp(o);
}
void updmul(int l,int r,int o,int L,int R) {
if (L<=l&&r<=R) {
mulv[o]*=-1;
maxv[o].fi*=-1;minv[o].fi*=-1;
if(maxv[o].fi!=minv[o].fi) {
swap(minv[o],maxv[o]);
}
return;
}
pushDown(o);
int m=(l+r)>>1;
if (L<=m) updmul(l,m,Lson,L,R);
if (m<R) updmul(m+1,r,Rson,L,R);
pushUp(o);
}
pair<ll,int> query(int l,int r,int o,int L,int R) {
if (L<=l && r<=R) {
return maxv[o];
}
pushDown(o);
int m=(l+r)>>1;
pair<ll,int> ret=mp(-100,-1);
if (L<=m) gmax(ret,query(l,m,Lson,L,R))
if (m<R) gmax(ret,query(m+1,r,Rson,L,R))
return ret;
}
}A,B;
int main()
{
freopen("today_is_gonna_be_a_great_day_input.txt","r",stdin);
freopen("D.out","w",stdout);
int T=read();
For(kcase,T) {
printf("Case #%d: ",kcase);
int n=read();
For(i,n) A.a[i]=read();
For(i,n) B.a[i]=-A.a[i]*(1000000006ll)%(1000000007ll);
A.build(1,n,1);
B.build(1,n,1);
int m=read();
ll ans=0;
For(i,m) {
int l=read(),r=read();
A.updmul(1,n,1,l,r);
B.updmul(1,n,1,l,r);
auto pa2=A.query(1,n,1,1,n);
auto pa3=B.query(1,n,1,1,n);
auto pa=max(pa2,pa3);
ans+=pa.se;
}
cout<<-ans<<endl;
}
return 0;
}
Problem E: Bohemian Rap-sody
TBD
给n个字符串,Q 个询问。
每个询问
每次选取一个区间中的字符串,求这些字符串