Social Distancing
鏈接:https://ac.nowcoder.com/acm/contest/5672/A
來源:牛客網(wǎng)
題目描述
Nowadays, the Kingdom of Dreamgrid is suffering from a national pandemic. Fortunately, president Baobao is working effectively with the Center for Disease Control (CDC) and they are trying their best to make everything under control.
President Baobao has announced a policy of Social Distancing to prevent the diffusion of the virus. As the chief of CDC, you are required to research on the following problem:
There are n n n people who need to be observed and you have already set a monitor in ( 0 , 0 ) (0,0) (0,0) on a 2 2 2-dimensional plane. Everyone should stay within the distance of r to the monitor. You also have to keep them stay away from each other as far as possible. To simplify the problem, you can only allocate them to integers coordinates.
Please maximize
∑ i = 1 n ? 1 ∑ j = i + 1 n d ( i , j ) 2 \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}d(i,j)^2 ∑i=1n?1?∑j=i+1n?d(i,j)2 ,
where d ( i , j ) d(i,j) d(i,j) means the Euclidean distance between the i i i-th and the j j j-th person.
輸入描述:
There are multiple test cases. The first line of the input contains an integer T T T ( 1 ≤ T ≤ 250 ) (1 \leq T \leq 250) (1≤T≤250) , indicating the number of test cases.
For each test case, the only line contains two integers n , r n,r n,r ( 1 ≤ n ≤ 8 , 1 ≤ r ≤ 30 ) (1 \leq n \leq 8,1 \leq r \leq 30) (1≤n≤8,1≤r≤30).
輸出描述:
Please output the answer in one line for each test case.
示例1
設(shè)第 i i i個點的橫坐標(biāo)為 x i x_i xi?,縱坐標(biāo)為 y i y_i yi?,則
∑ i = 1 n ? 1 ∑ j = i + 1 n d ( i , j ) = ∑ i = 1 n ? 1 ∑ j = i + 1 n ( x i ? x j ) 2 + ∑ i = 1 n ? 1 ∑ j = i + 1 n ( y i ? y j ) 2 \sum_{i=1}^{n-1}\sum_{j=i+1}^nd(i,j)=\sum_{i=1}^{n-1}\sum_{j=i+1}^n(x_i-x_j)^2+\sum_{i=1}^{n-1}\sum_{j=i+1}^n(y_i-y_j)^2 i=1∑n?1?j=i+1∑n?d(i,j)=i=1∑n?1?j=i+1∑n?(xi??xj?)2+i=1∑n?1?j=i+1∑n?(yi??yj?)2
由于
∑ i = 1 n ? 1 ∑ j = i + 1 n ( x i ? x j ) 2 = ∑ i = 1 n ? 1 ∑ j = i + 1 n ( x i 2 ? 2 x i x j + x j 2 ) = ( n ? 1 ) ∑ i = 1 n x i 2 ? 2 ∑ i = 1 n ? 1 ∑ j = i + 1 n x i x j = n ∑ i = 1 n x i 2 ? ( ∑ i = 1 n x i 2 + 2 ∑ i = 1 n ? 1 ∑ j = i + 1 n x i x j ) = n ∑ i = 1 n x i 2 ? ( ∑ i = 1 n x i ) 2 \begin{aligned} \sum_{i=1}^{n-1}\sum_{j=i+1}^n(x_i-x_j)^2&=\sum_{i=1}^{n-1}\sum_{j=i+1}^n(x_i^2-2x_ix_j+x_j^2) \\ &=(n-1)\sum_{i=1}^nx_i^2-2\sum_{i=1}^{n-1}\sum_{j=i+1}^nx_ix_j\\ &=n\sum_{i=1}^nx_i^2-(\sum_{i=1}^nx_i^2+2\sum_{i=1}^{n-1}\sum_{j=i+1}^nx_ix_j)\\ &=n\sum_{i=1}^nx_i^2-(\sum_{i=1}^nx_i)^2 \end{aligned} i=1∑n?1?j=i+1∑n?(xi??xj?)2?=i=1∑n?1?j=i+1∑n?(xi2??2xi?xj?+xj2?)=(n?1)i=1∑n?xi2??2i=1∑n?1?j=i+1∑n?xi?xj?=ni=1∑n?xi2??(i=1∑n?xi2?+2i=1∑n?1?j=i+1∑n?xi?xj?)=ni=1∑n?xi2??(i=1∑n?xi?)2?
因此 ∑ i = 1 n ? 1 ∑ j = i + 1 n d ( i , j ) = n ∑ i = 1 n ( x i 2 + y i 2 ) ? ( ∑ i = 1 n x i ) 2 ? ( ∑ i = 1 n y i ) 2 \sum_{i=1}^{n-1}\sum_{j=i+1}^nd(i,j)=n\sum_{i=1}^n(x_i^2+y_i^2)-(\sum_{i=1}^nx_i)^2-(\sum_{i=1}^ny_i)^2 i=1∑n?1?j=i+1∑n?d(i,j)=ni=1∑n?(xi2?+yi2?)?(i=1∑n?xi?)2?(i=1∑n?yi?)2
令 d p [ i ] [ ∑ x ] [ ∑ y ] = m a x ( ∑ ( x 2 + y 2 ) ) dp[i][\sum x][\sum y]=max(\sum (x^2+y^2)) dp[i][∑x][∑y]=max(∑(x2+y2))
即 d p dp dp表示當(dāng)前點數(shù)為 i i i,所有點的橫坐標(biāo)之和為 ∑ x \sum x ∑x,縱坐標(biāo)之和為 ∑ y \sum y ∑y時所有點距離原點和 ∑ ( x 2 + y 2 ) \sum (x^2+y^2) ∑(x2+y2)的最大值。
狀態(tài)轉(zhuǎn)移方程為:
d p [ i ] [ ∑ x ] [ ∑ y ] = m i n ( d p [ i ] [ ∑ x ] [ ∑ y ] , d p [ i ? 1 ] [ ∑ x ? x j ] [ ∑ y ? y j ] + ( x j 2 + y j 2 ) ) dp[i][\sum x][\sum y]=min(dp[i][\sum x][\sum y],dp[i-1][\sum x-x_j][\sum y-y_j]+(x_j^2+y_j^2)) dp[i][∑x][∑y]=min(dp[i][∑x][∑y],dp[i?1][∑x?xj?][∑y?yj?]+(xj2?+yj2?))
對于圓的半徑 r r r從小到大各 d p dp dp一次。每次 d p dp dp完,更新 a n s [ i ] [ r ] = m a x ( a n s [ i ] [ r ] , i ? d p [ i ] [ ∑ x ] [ ∑ y ] ? ( ∑ x ) 2 ? ( ∑ y ) 2 ) ans[i][r]=max(ans[i][r],i·dp[i][\sum x][\sum y]-(\sum x)^2-(\sum y)^2) ans[i][r]=max(ans[i][r],i?dp[i][∑x][∑y]?(∑x)2?(∑y)2)
總結(jié)
以上是生活随笔為你收集整理的Social Distancing的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 帆软FineReport_下拉菜单内容并
- 下一篇: visibility,display,o