当前位置:网站首页>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 .
边栏推荐
- Smart factory digital management system software platform
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- JS how to convert a string with a delimiter into an n-dimensional array
- Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
- Blue Bridge Cup real topic: the shortest circuit
- How to retrieve the password for opening Excel files
- Source code of new campus errand / campus task platform on mutual station
- Htt [ripro network disk link detection plug-in] currently supports four common network disks
- Domestic spot silver should be understood
- 【Try to Hack】vulnhub DC4
猜你喜欢

PCL learning materials

Check log4j problems using stain analysis

PTA year of birth

2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers

Penetration practice vulnhub range Keyring

Happy new year | 202112 monthly summary

New 95 community system whole station source code

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)

Penetration practice vulnhub range Tornado

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
随机推荐
Yolov5 practice: teach object detection by hand
On the language internationalization of Yongzhong Office
February 16, 2022 Daily: graph neural network self training method under distribution and migration
Three dimensional anti-terrorism Simulation Drill deduction training system software
Oracle TRUNC function processing date format
Win10+vs2019 Community Edition compiling OpenSSL
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Equipment simulation and deduction training system software
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
[CF559E]Gerald and Path
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Extract the compressed package file and retrieve the password
PMP daily three questions (February 15, 2022)
What are the six steps of the software development process? How to draw software development flow chart?
How to learn automated testing?
[C supplement] [string] display the schedule of a month by date
Batch export all pictures in PPT in one second
Development cost of smart factory management system software platform
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?