当前位置:网站首页>Codeforces 1637 D. yet another minimization problem - Mathematics, DP
Codeforces 1637 D. yet another minimization problem - Mathematics, DP
2022-06-12 13:32:00 【Tianyi City*】
The question :
Give you a length of n Of a Array and b Array , You can choose one location at a time i, In exchange for a[i],b[i]. In the end, make ∑ i = 1 n ∑ j = i + 1 n ( a [ i ] + a [ j ] ) 2 + ∑ i = 1 n ∑ j = i + 1 n ( b [ i ] + b [ j ] ) 2 \sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^{n}(a[i]+a[j])^2+\sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^{n}(b[i]+b[j])^2 i=1∑nj=i+1∑n(a[i]+a[j])2+i=1∑nj=i+1∑n(b[i]+b[j])2 Minimum .
What is the minimum value .
Answer key :
This is a 1800? I can't even think of it …
It seems that the problem of this formula has not been encountered for some time , I didn't even think about that .
It's almost such a process , So at the end of the formula a and b We already know the sum of the squares of , So what we are looking for is the smallest of the last two items .
hypothesis a And for x, that b The sum of is the sum of all the numbers -x Chant . See, for all a Is it possible that
dp[i][j] It means No i When we count ,a And for j The possibility of
So there are two cases of state transition :
if(j>=a[i])dp[1-i%2][j]|=dp[i%2][j-a[i]];
if(j>=b[i])dp[1-i%2][j]|=dp[i%2][j-b[i]];
#include<bits/stdc++.h>
using namespace std;
const int N=105;
bool dp[2][N*N];
int a[N],b[N];
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n,ans=0,r=0,sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),ans+=a[i]*a[i]*(n-2),sum+=a[i];
for(int i=1;i<=n;i++)scanf("%d",&b[i]),ans+=b[i]*b[i]*(n-2),sum+=b[i];
memset(dp[1],0,sizeof dp[1]);
dp[1][0]=1;
for(int i=1;i<=n;i++){
r+=max(a[i],b[i]);
memset(dp[1-i%2],0,sizeof dp[1-i%2]);
for(int j=r;j>=min(a[i],b[i]);j--){
if(j>=a[i])dp[1-i%2][j]|=dp[i%2][j-a[i]];
if(j>=b[i])dp[1-i%2][j]|=dp[i%2][j-b[i]];
}
}
int mi=1e9;
for(int i=0;i<=r;i++)
if(dp[1-n%2][i])
mi=min(mi,i*i+(sum-i)*(sum-i));
printf("%d\n",ans+mi);
}
return 0;
}
边栏推荐
- imagemagick:a gentle introduction to magick++
- [wechat applet development] Part 1: development tool installation and program configuration
- 软件构造 03 正则表达式
- 2065: [example 2.2] sum of integers
- Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
- A brief introduction to Verilog mode
- Encryptor and client authenticate with each other
- m1 pod install pod lint 失败解决方案
- 【云原生 | Kubernetes篇】深入了解Deployment(八)
- Octopus network progress monthly report | may 1-May 31, 2022
猜你喜欢

C#DBHelper_FactoryDB_GetConn
![[embedded] serial communication and its case](/img/5c/2e691e5ef03c7d65fd514e8b940e7b.jpg)
[embedded] serial communication and its case

D1 哪吒开发板 了解基本的启动加载流程

import torch_ Data view of geometric

Application of binary search -- finding the square root sqrt of a number

【刷题篇】抽牌获胜的概率

Automatic Generation of Visual-Textual Presentation Layout

VGA display color bar and picture (FPGA)

Innovation training (x) advanced interface beautification

Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
随机推荐
关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
[cloud native | kubernetes] in depth understanding of deployment (VIII)
5V升压到12.6V的锂电池充电IC芯片方案FS4062B
xcode 调试openGLES
创新实训(十一)开发过程中的一些bug汇总
C#DBHelper_FactoryDB_GetConn
[cloud native | kubernetes] learn more about ingress
[cloud native | kubernetes] kubernetes networkpolicy
import torch_geometric 的Data 查看
Application of binary search -- finding the square root sqrt of a number
Implementing pytorch style deep learning framework similartorch with numpy
"New continent" of mobile application going to sea
Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
Will the next star of PPT for workplace speech be you [perfect summary] at the moment
达梦数据库DM8 windows环境安装
1005: estimation of the earth's population carrying capacity
AVFoundation
Overview of embedded system 2- composition and application of embedded system
字节序数据读写
C language array and pointer