当前位置:网站首页>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 .
边栏推荐
- Win10+vs2019 Community Edition compiling OpenSSL
- Redis -- data type and operation
- What is web application security testing technology?
- Cassette helicopter and alternating electric field magnetic manometer DPC
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- Static timing analysis (STA) in ic/fpga design
- Database - MySQL advanced SQL statement (I)
- Irradiance, Joule energy, exercise habits
- Calculation of intersection of two line segments
- ISO 27001 Information Security Management System Certification
猜你喜欢

Product service, operation characteristics

Mysql database design

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes

Mujoco XML modeling

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

Depth first search - DFS (burst search)

transform. Forward and vector3 Differences in the use of forward

What are the legal risks of NFT brought by stars such as curry and O'Neill?

LeetCode 148. Sort linked list

MySQL connection tools
随机推荐
Develop those things: add playback address authentication to easycvr platform
Sword finger offer II 105 Maximum area of the island
Extract the compressed package file and retrieve the password
证券开户安全么,有没有什么样的危险呢
ArrayList扩容详解
Slider verification code identification gadget display
Nielseniq found that 60% of the re launched products had poor returns
網上股票開戶安全嗎?是否可靠?
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
Is it safe to open a securities account? Is there any danger
golang中的select详解
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
About selenium element positioning being overwritten
Explain in detail the process of realizing Chinese text classification by CNN
MySQL + JSON = King fried
网上股票开户安全吗?是否可靠?
[noip2015] jumping stone
Operation of cmake under win
Step size of ode45 and reltol abstol
Apache iceberg source code analysis: schema evolution