当前位置:网站首页>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 .
边栏推荐
- Mujoco's biped robot Darwin model
- At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
- Length of learning and changing
- Zabbix报警执行远程命令
- Session layer of csframework, server and client (1)
- Flex layout
- . Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
- Penetration practice vulnhub range Tornado
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- t10_ Adapting to Market Participantsand Conditions
猜你喜欢
Cloud computing - make learning easier
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
PCL learning materials
The method of real-time tracking the current price of London Silver
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
[PHP foundation] realize the connection between PHP and SQL database
t10_ Adapting to Market Participantsand Conditions
MySQL connection tools
Thinkphp6 - CMS multi wechat management system source code
Length of learning and changing
随机推荐
[acnoi2022] color ball
MES production equipment manufacturing execution system software
Rotation order and universal lock of unity panel
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
Redis主从实现10秒检查与恢复
Bug of QQ browser article comment: the commentator is wrong
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
Classpath classpath
Zabbix报警执行远程命令
[CF1476F]Lanterns
How to learn automated testing?
期货账户的资金安全吗?怎么开户?
EasyCVR设备录像出现无法播放现象的问题修复
Apk signature process introduction [easy to understand]
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Euler function: find the number of numbers less than or equal to N and coprime with n
Step size of ode45 and reltol abstol
On the language internationalization of Yongzhong Office
Bernoulli distribution (a discrete distribution)