当前位置:网站首页>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 .
边栏推荐
- Key points on February 15, 2022
- 聊聊项目经理最爱使用的工具
- Thinkphp6 - CMS multi wechat management system source code
- Rotation order and universal lock of unity panel
- Classpath classpath
- APK签名流程介绍[通俗易懂]
- js如何将带有分割符的字符串转化成一个n维数组
- Static timing analysis (STA) in ic/fpga design
- Relationship between sensor size, pixel, dpi resolution, inch and millimeter
- Blue Bridge Cup real problem: word analysis
猜你喜欢

Mujoco XML modeling

PTA year of birth

A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database

Cloud computing - make learning easier

Penetration practice vulnhub range Nemesis

Yuancosmos game farmersworld farmers world - core content of the second conference in China!

Distributed task queue: Celery usage record

Penetration practice vulnhub range Tornado

The new server is packaged with the source code of H5 mall with an operation level value of several thousand

Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
随机推荐
Definition of rotation axis in mujoco
網上股票開戶安全嗎?是否可靠?
Operating system interview assault
Happy new year | 202112 monthly summary
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
To improve the efficiency of office collaboration, trackup may be the best choice
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
Redis -- data type and operation
Function, condition, regular expression
PTA year of birth
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
How to retrieve the password for opening Excel files
【Try to Hack】vulnhub DC4
What is web application security testing technology?
Record 3 - the state machine realizes key control and measures the number of external pulses
Redis master-slave realizes 10 second check and recovery
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Is online stock account opening safe? Is it reliable?
Bug of QQ browser article comment: the commentator is wrong