Supplementary questions for the fifth session of 2021 Niuke multi school

Posted by evildj on Sat, 01 Jan 2022 01:22:36 +0100

B-Boxes

Link: https://ac.nowcoder.com/acm/contest/11256/B
Source: niuke.com
 

Title Description

There're nn_{}n​ boxes in front of you. You know that each box contains a ball either in white or in black. The probability for a ball to be white is 12\frac{1}{2}21​, and the colors of balls are independent of each other. The PJ King invites you to guess the colors of all balls. PJ King has assigned some costs to the boxes. If we number the boxes from 11_{}1​ to nn_{}n​, the cost to open the box ii_{}i​ is wiw_iwi​, and after a box is opened you can see the ball inside this box.

For sure, there's no way to know all the colors except by opening all boxes. However, Gromah wants to give you some hints. Gromah can tell you secretly the number of black balls among all boxes that have not been opened yet, but you have to pay CC_{}C​ cost to get one such hint from Gromah. Anyway, if you're superpowered, you can do it without any hint. What's the mathematical expectation of the minimum cost to figure out all colors of balls?

Enter Description:

The first line contains an integer n (1≤n≤105)n~(1\le n \le 10^5)n (1≤n≤105) and a decimal C (0<C≤109)C~(0 < C \le 10^9)C (0<C≤109), representing the number of boxes and the cost to get a hint from Gromah.

The second line contains nn_{}n​ decimals w1,w2,⋯ ,wn (0<wi≤109)w_1, w_2, \cdots, w_n~(0 < w_i \le 10^9)w1​,w2​,⋯,wn​ (0<wi​≤109).

All decimal numbers in the input have at most six decimal places.

Output Description:

Output one line with the expected minimum cost. Your answer will be considered to be correct if the relative or absolute error is less than 10−610^{-6}10−6.

General idea of the topic

The probability of black balls and white balls in n boxes is equal. There is a chance to spend c yuan to tell you how many boxes are black balls

You can also avoid this opportunity

What's your mathematical expectation of what ball is in all the boxes

thinking

The first strategy does not consume the opportunity of c yuan, so the total cost is Σ wi

The second strategy is to sort wi from front to back. Only sumi needs to be opened if the box balls from i to n are of the same color

The probability is 1.0/pow(2,n-i)

Expected sum[i] * 1.0/pow(2,n-i)

The total expectation is Σ sum[i] * 1.0/pow(2,n-i)

PS: the quickpow function needs to use double. If you add a bracket when applying the qpow call of longlong, double will be used

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n;
double a[maxn];
double sum[maxn];
double c;
double quickpow(double a,ll b)
{
	double  ans=1;
	while(b>0)
	{
    if(b&1)
    {
        ans*=a;
        //ans%=mod1;
    }
    a*=a;//Use self multiplication to quickly power.
    //a%=mod1;
    b>>=1;
	}
	return ans;
}
int main()
{
	cin>>n>>c;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
	double ans=c;
	double t=1.0;
	for(int i=1;i<n;i++)
	{
		//ans+=sum[i]*pow(0.5,n-i);
		 ans+=sum[i]*(1.0/(double)quickpow(2,n-i));
		//ans+=a[i]*(1.0-1.0/((double)quickpow(2,n-i)))/t;
		//t=(1.0-1.0/((double)quickpow(2,n-i)))/t;
	}
	ans=min(ans,sum[n]);
	printf("%.8lf\n",ans);
	return 0;
}

J-Jewels

Link: https://ac.nowcoder.com/acm/contest/11256/J
Source: niuke.com
 

Title Description

There are nn_{}n​ jewels under the sea, and you want to salvage all the jewels. Image that the sea is a 3D coordinate system, and that you are at (0,0,0)(0,0,0)_{}(0,0,0)​ while the ii_{}i​-th jewel is at (xi,yi,zi) (zi≥0)(x_i, y_i, z_i)~(z_i \ge 0)(xi​,yi​,zi​) (zi​≥0) initially. You can salvage one jewel immediately(regardless of other jewels wherever they are) with strength d2d^2d2 at every non-negative integer moment where d denotes the distance between you and the jewel you salvage at that moment. However, the jewels sink. Specifically, for the ii_{}i​-th jewel, it will be at (xi,yi,zi+t×vi)(x_i, y_i, z_i + t\times v_i)(xi​,yi​,zi​+t×vi​) after tt_{}t​ seconds. So you want to know the minimum possible total strength to salvage all the jewels.

Enter Description:

The first line contains one integer n (1≤n≤300)n~(1\le n \le 300)n (1≤n≤300), denoting the number of jewels.

Following nn_{}n​ lines each contains four integers xi,yi,zi,vi (0≤∣xi∣,∣yi∣,zi,vi≤1000)x_i, y_i, z_i, v_i~(0 \le |x_i|,|y_i|,z_i,v_i \le 1000)xi​,yi​,zi​,vi​ (0≤∣xi​∣,∣yi​∣,zi​,vi​≤1000), denoting the jewels' initial positions and sinking velocities.

Output Description:

Output one line containing one integer, denoting the minimum possible total strength to salvage all the jewels.

General idea of the topic

There are n gems and N times to catch them. Each gemstone has a vi. for time j, the z axis of the gemstone is equal to vi*j+zi

Ask the distance you have to go to get all the gemstones, where di=xi2+yi2+zi'2

 

thinking

In fact, this problem is very simple. The consumption of n gemstones at each time can be easily calculated and then become the edge weight of the bipartite graph. As long as you think of the bipartite graph, you can solve this problem. Because it is a complete match, you can use the km algorithm to solve this problem

At first, I knocked on the dfs algorithm in lyd blue book, believed his evil, and then optimized two rounds or T with his blog

 

Finally, I didn't listen to him and found a bfs board to relax for 50ms

ps: commented out is the dfs code

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll inf_ll = 1ll * inf * inf;
const int maxn=3e2+10;
const int N=3e2+10;
int n,m;
ll w[maxn][maxn];
struct ndoe{
	int x,y,z,v;
}e[maxn];
ll mb[N+7],vb[N+7],ka[N+7],kb[N+7],p[N+7],c[N+7];
ll qf,qb,q[N+7];
//bool dfs(int x,int fa)
//{
//	va[x]=1;
//	for(int y=1;y<=n;y++)
//	{
//		if(!vb[y])
//		{
//			if(la[x]+lb[y]-w[x][y]==0) / / equal subgraph 
//			{
//				vb[y]=1;
//				last[y] = fa;
//				//cout<<y<<" "<<match[y]<<endl;
//				if(!match[y]||dfs(match[y],y))
//				{
//					match[y]=x;
//					//cout<<1<<endl ;
//					return 1;
//				}
//			}
//			 else if (upd[y]>la[x] + lb[y] - w[x][y] ) {
//                upd[y] = la[x] + lb[y] - w[x][y];
//                last[y] = fa;
//            }
//		}
//	}
//	
//	return 0;
//}
//ll KM()
//{
//	for(int i=1;i<=n;i++)
//	{
//		la[i]=-(1<<30);//inf
//		lb[i]=0;
//		for(int j=1;j<=n;j++)
//		{
//			la[i]=max(la[i],w[i][j]);
//		}
//	}
//	
//	for(int i=1;i<=n;i++)
//		{
//			for(int j=1;j<=n;j++)upd[j]=1e10;
//			memset(va,0,sizeof(va));
//			memset(vb,0,sizeof(vb));
//			int st = 0; match[0] = i;
//			while(match[st])
//			{
//				if(dfs(match[st],st))break;
//				delta=1e11;
//				for(int j=1;j<=n;j++)
//				{
//					if (!vb[j] && delta > upd[j]) {
//                    delta = upd[j];
//                    st = j; //  Next time, start DFS directly from the smallest edge
//               		}
//				} 
//				for(int j=1;j<=n;j++)
//				{
//					if(va[j])la[j]-=delta;
//					if(vb[j])lb[j]+=delta;
//					else upd[j]-=delta;
//				 } 
//				vb[st]=1;
//			}
//			while (st) {/ / push backward to update Zengguang Road
//            	match[st] = match[last[st]];
//            	st = last[st];
//            }
//		}	
//	ll ans=0;
//	for(int i=1;i<=n;i++)ans+=w[match[i]][i];
//	return ans; 
//}
void Bfs(int u){
    ll a,v=0,vl=0,d;
    for(int i=1;i<=n;i++) p[i]=0,c[i]=inf_ll;
    mb[v]=u;
    do {
        a=mb[v],d=inf_ll,vb[v]=1;
        for(int b=1;b<=n;b++)if(!vb[b]){
            if(c[b]>ka[a]+kb[b]-w[a][b])
                c[b]=ka[a]+kb[b]-w[a][b],p[b]=v;
            if(c[b]<d) d=c[b],vl=b;
        }
        for(int b=0;b<=n;b++)
            if(vb[b]) ka[mb[b]]-=d,kb[b]+=d;
            else c[b]-=d;
        v=vl;
    } while(mb[v]);
    while(v) mb[v]=mb[p[v]],v=p[v];
}
ll KM(){
    for(int i=1;i<=n;i++) mb[i]=ka[i]=kb[i]=0;
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++) vb[b]=0;
        Bfs(a);
    }
    ll res=0;
    for(int b=1;b<=n;b++) res+=w[mb[b]][b];
    return res;
}
int main()
{
	//cin>>n;
	scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d%d",&e[i].x,&e[i].y,&e[i].z,&e[i].v);
    }
	for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            w[i][j] = 1ll * e[i].x * e[i].x + 1ll * e[i].y * e[i].y + 1ll * (e[i].z+(j-1)*e[i].v) * (e[i].z+(j-1)*e[i].v);
            w[i][j] *= -1;
        }
    }
    
    cout<<-KM()<<endl;
	return 0;
}

K-King of Range

Link: https://ac.nowcoder.com/acm/contest/11256/K
Source: niuke.com
 

Title Description

Given nn_{}n​ integers a1,a2,⋯ ,ana_1,a_2,\cdots,a_na1​,a2​,⋯,an​ and mm_{}m​ queries. For each query, you are given a const kk_{}k​ and you should determine how many different pairs (l,r)(l,r)_{}(l,r)​ are there meeting the condition that the range of the subsequence al,al+1,⋯ ,ara_l,a_{l+1},\cdots,a_ral​,al+1​,⋯,ar​ is strictly greater than kk_{}k​.

Note: the range of a sequence equals the difference between the maximum and the minimum of the sequence.

Enter Description:

The first line contains two integers n,m (1≤n≤105,1≤m≤200)n,m\,(1 \le n \le 10^5, 1 \le m \le 200)n,m(1≤n≤105,1≤m≤200), denoting the number of given integers and the number of queries respectively.

The second line contains n integers a1,a2,⋯ ,an (1≤ai≤109)a_1, a_2, \cdots, a_n\,(1 \le a_i \le 10^9)a1​,a2​,⋯,an​(1≤ai​≤109), denoting the given integers.

Next m lines each contains one integer k (1≤k≤109)k\,(1 \le k \le 10^9)k(1≤k≤109), denoting the queries.

Output Description:

Print mm_{}m​ lines each contains one integer, denoting the answers.

General idea of the topic

Give you a sequence so that you can find out how many subsequences of this sequence have a range greater than K. there are m queries

thinking

At first, I wanted to use the st table to record the maximum and minimum values of the interval, because the range of this table satisfies monotonicity, and then use dichotomy to find a right endpoint r for each left endpoint L, which satisfies that the range of this subsequence (L, R) is greater than k. for each l accumulation (n-r+1)

The time complexity is about m*n*log (n)

Unfortunately, the questioner quickly increased the data volume of m, and the card dropped this practice

In the second method, the two terminal monotone queue advances r. each time the max monotone queue and min monotone queue are updated, l when the difference between the head of the queue is greater than k++

Judge whether to pop the team leader, ans+=n-r+1;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,m,k;
int a[maxn];
int max1[maxn],min1[maxn];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	while(m--)
	{
		cin>>k;
		ll ans=0;
		int l=1,b1=1,b2=1,t1=0,t2=0;
		for(int i=1;i<=n;i++)
		{
			while(b1<=t1&&a[i]>=a[max1[t1]])t1--;
			while(b2<=t2&&a[i]<=a[min1[t2]])t2--;
			max1[++t1]=i;
			min1[++t2]=i;
			while(a[max1[b1]]-a[min1[b2]]>k)
			{
				ans+=n-i+1;
				l++;
				while(max1[b1]<l&&b1<=t1)b1++;
				while(min1[b2]<l&&b2<=t2)b2++;
			}
		} 
		cout<<ans<<endl;
	}
	return 0;
}

Topics: Algorithm