• Meta Hacker Cup 2023 Round 1 题解


    Problem A: Here Comes Santa Claus

    给一个数列,要求分成若干组,要求每组至少2个数,使得所有组中位数的最大值与最小值之差尽量大,求这个值。

    #include 
    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;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    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 
    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<
    	if(!s) {
    //		For(i,l-1) cout<
    //		cout<<":"<
    		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;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    Problem C1: Back in Black (Chapter 1)

    #include 
    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;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    Problem D: Today is Gonna be a Great Day

    给一个长度为n的数列,维护2个操作,批量 a i = a i ∗ ( 1 e 9 + 6 )   m o d   ( 1 e 9 + 7 ) ai=ai*(1e9+6)\bmod (1e9+7) ai=ai(1e9+6)mod(1e9+7),求全局最大值的位置。

    同余系下,操作等价于乘-1,乘2次会变回原来的值。答案在2n个数中。
    用2个线段树分别维护乘1次和不乘时哪些数在数列中并维护最大值位置

    #include 
    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;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155

    Problem E: Bohemian Rap-sody

    TBD
    给n个字符串,Q 个询问。
    每个询问 a i , b i , K a_i,b_i,K ai,bi,K
    每次选取一个区间中的字符串,求这些字符串

  • 相关阅读:
    这 20 道 Redis 经典面试题你还不会,就别去面试了!
    《Thinking In Java》作者:不要使用并发!
    网页标签在html中的显示+单标记换行操作
    【一天一个设计模式】—— 抽象工厂模式
    redis7 搭建集群
    Day17:稀疏数组的超细详解
    Hudi源码|bootstrap源码分析总结(写Hudi)
    【建议背诵】软考高项考试案例简答题汇总~
    vue-springboot 前后端传参的形式
    【教3妹学算法-每日一题】竞赛题:6160. 和有限的最长子序列
  • 原文地址:https://blog.csdn.net/nike0good/article/details/133749671