当前位置:网站首页>D. Yet Another Minimization Problem
D. Yet Another Minimization Problem
2022-07-01 18:27:00 【Foolish last words】
Portal :CF
Preface : After the new year, so many , Lose big points , from 1850 Transfer to 1700, It's really sour . This scene D Of dp It's a wonderful point ( At least I won't ), I saw Small t After the code , be filled with wisdom ( Small t Such as medicine , Good reading can cure fools ).
Text :
First look at the data range , Give it to 2 Second implementation plus n by 100, When I first solved it, I thought it was a problem of violent enumeration , So there is no simplification at all +dp It's like . So I lost a lot of points .
By simplifying the formula , You can get , The best time is
Of course , It's hard to get the most perfect situation , So just get as close as possible .
However, how can we find this optimal sum ?
My initial thought was violence , however MLE 了 ( I am a idiot. )
The positive solution is through iteration ,AC The code is as follows :
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
vector<int>a(n),b(n);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>b[i];
if(n==1){
cout<<0<<'\n';
continue;
}
ll sum = accumulate(a.begin(),a.end(),0)+accumulate(b.begin(),b.end(),0);
vector<ll>dp(sum+1);
dp[0]=1;
for(int i=0 ;i<n;i++){
for(int j = sum ; j >= 0;j--){
if(dp[j]){
dp[j+a[i]]=1;
dp[j+b[i]]=1;
dp[j]=0;
}
}
}
ll kase = 1e18;
ll sum1=0;
for(int i=0;i<=sum;i++){
if(dp[i]==0)continue;
if(abs(sum-i*2)<kase){
sum1=i;
kase = abs(sum-i*2);
}
}
ll ans=0;
// cout<<sum1<<'\n';
ans+=sum1*sum1+(sum-sum1)*(sum-sum1);
for(int i =0 ;i<n;i++){
ans+=(n-2)*a[i]*a[i];
ans+=(n-2)*b[i]*b[i];
}
cout<<ans<<'\n';
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
vector<int>a(n),b(n);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>b[i];
if(n==1){
cout<<0<<'\n';
continue;
}
ll sum = accumulate(a.begin(),a.end(),0)+accumulate(b.begin(),b.end(),0);
vector<ll>dp(sum+1);
dp[0]=1;
for(int i=0 ;i<n;i++){
for(int j = sum ; j >= 0;j--){
if(dp[j]){
dp[j+a[i]]=1;
dp[j+b[i]]=1;
dp[j]=0;
}
}
}
ll kase = 1e18;
ll sum1=0;
for(int i=0;i<=sum;i++){
if(dp[i]==0)continue;
if(abs(sum-i*2)<kase){
sum1=i;
kase = abs(sum-i*2);
}
}
ll ans=0;
// cout<<sum1<<'\n';
ans+=sum1*sum1+(sum-sum1)*(sum-sum1);
for(int i =0 ;i<n;i++){
ans+=(n-2)*a[i]*a[i];
ans+=(n-2)*b[i]*b[i];
}
cout<<ans<<'\n';
}
return 0;
}
Here's another one STL,
It can be used accumulate() To directly solve the sum of an array .
边栏推荐
- Kernel stray cat stray dog pet adoption platform H5 source code
- [beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
- What is web application security testing technology?
- 开发那些事儿:EasyCVR平台添加播放地址鉴权
- Penetration practice vulnhub range Tornado
- 网上股票开户安全吗?是否可靠?
- JS how to convert a string with a delimiter into an n-dimensional array
- Irradiance, Joule energy, exercise habits
- golang中的select详解
- Rust language - cargo, crates io
猜你喜欢
Good looking UI mall source code has been scanned, no back door, no encryption
Wechat applet blind box - docking wechat payment
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
Common design parameters of solid rocket motor
This is the latest opportunity of the London bank trend
Penetration practice vulnhub range Tornado
Set the style of QT property sheet control
Mujoco XML modeling
Penetration practice vulnhub range Nemesis
Review Net 20th anniversary development and 51aspx growth
随机推荐
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Penetration practice vulnhub range Keyring
Irradiance, Joule energy, exercise habits
Development cost of smart factory management system software platform
Apache iceberg source code analysis: schema evolution
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
[C supplement] [string] display the schedule of a month by date
Win10+vs2019 Community Edition compiling OpenSSL
Explain in detail the process of realizing Chinese text classification by CNN
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
Product service, operation characteristics
Function, condition, regular expression
js如何将带有分割符的字符串转化成一个n维数组
Is the fund of futures account safe? How to open an account?
Blue Bridge Cup real problem: word analysis
How to retrieve the password for opening Excel files
540. Single element in ordered array
Check log4j problems using stain analysis
golang中的select详解