当前位置:网站首页>Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
2022-07-06 00:14:00 【Find a derivative first】
subject
The question : Given array a、b, You can exchange any number i Location a[i] and b[i], Make the formula in the following figure minimum .(n<=100,a[i]、b[i]<=100)
Ideas : We must simplify the formula first , Otherwise, I don't know what to do . After simplification, I found that I didn't care about the square term , Just focus on the minimum sum of squares of the sum of two arrays . Can pass dp Find out all the values that can be obtained by one of the arrays , You can find the minimum value .
Method 1: f[i][j]: Before use only i Number , Make and for j.true It means you can come up with . Because the maximum is 100, most 1e6. ( The first dimension can be optimized as 0、1)
Method 2: You might as well change the small numbers to a in , Big numbers are put in b in . adopt dp Find out through exchange ,a Array new sum , Record minimum . It can be used bitset maintain a Arrays are swapped to get new sums .
Time complexity : O(nmaxmax = 1e6), It can be used bitset Optimize ,emmm Feeling is 8 times ? But the explanation is 64 times , Don't know much about .
Code :
bitset
#include<bits/stdc++.h>
using namespace std;
const int MAXSUM = 100*100+10;
int n,m,k,T;
int fun(int x)
{
return x * x ;
}
void solve()
{
scanf("%d",&n);
vector<int> a(n+1),b(n+1);
long long ans = 0; int l = 0,r = 0; // and
for(int i=1;i<=n;++i) scanf("%d",&a[i]),ans += fun(a[i]);
for(int i=1;i<=n;++i) scanf("%d",&b[i]),ans += fun(b[i]);
for(int i=1;i<=n;++i)
{
if(a[i] > b[i]) swap(a[i],b[i]);
l += a[i];
r += b[i];
}
ans *= (n-2);
int res = 1e9;
bitset<MAXSUM> dp;
dp[0] = 1;
for(int i=1;i<=n;++i)
{
dp |= (dp<<(b[i]-a[i]));
}
for(int i=0;i<=r-l;++i)
{
if(dp[i])
res = min(res,fun(l+i)+fun(r-i));
}
ans += res;
printf("%lld\n",ans);
}
signed main(void)
{
scanf("%d",&T);
while(T--)
solve();
return 0;
}
Ordinary dp
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() rounding ceil() Rounding up floor() Rounding down
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) First less than or equal to
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 102;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){
return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){
if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){
x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
bool f[N][N*N];
int a[N];
int b[N];
int fun(int x)
{
return x * x ;
}
void solve()
{
read(n);
int ans = 0;
int res = 1e9;
int sum = 0;
for(int i=1;i<=n;++i)
{
read(a[i]); ans += fun(a[i]); sum += a[i];
}
for(int i=1;i<=n;++i)
{
read(b[i]); ans += fun(b[i]); sum += b[i];
}
ans *= (n-2);
f[0][0] = true;
for(int i=1;i<=n;++i)
{
for(int j=0;j<=sum;++j)
{
if(f[i-1][j])
{
f[i][j+a[i]] = true;
f[i][j+b[i]] = true;
}
}
}
for(int j=0;j<=sum;++j)
{
if(f[n][j])
{
res = min(res,fun(j)+fun(sum-j));
}
}
ans += res;
write(ans); puts("");
}
signed main(void)
{
// T = 1;
// OldTomato; cin>>T;
read(T);
while(T--)
{
solve();
}
return 0;
}
边栏推荐
- Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
- Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- PV static creation and dynamic creation
- Learn PWN from CTF wiki - ret2libc1
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- Senparc. Weixin. Sample. MP source code analysis
- "14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- NSSA area where OSPF is configured for Huawei equipment
- [designmode] adapter pattern
猜你喜欢

MySql——CRUD

AtCoder Beginner Contest 258【比赛记录】

Permission problem: source bash_ profile permission denied
![[designmode] Decorator Pattern](/img/65/457e0287383d0ca9a28703a63b4e1a.png)
[designmode] Decorator Pattern

MySql——CRUD

Browser local storage

亲测可用fiddler手机抓包配置代理后没有网络

About the slmgr command

What are the functions of Yunna fixed assets management system?

XML configuration file (DTD detailed explanation)
随机推荐
wx. Getlocation (object object) application method, latest version
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
[designmode] Decorator Pattern
[online chat] the original wechat applet can also reply to Facebook homepage messages!
剖面测量之提取剖面数据
【二叉搜索树】增删改查功能代码实现
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
LeetCode 6004. Get operands of 0
Redis high availability - master-slave replication, sentinel mode, cluster
Wechat applet -- wxml template syntax (with notes)
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
FFmpeg学习——核心模块
About the slmgr command
SQLServer连接数据库读取中文乱码问题解决
Priority queue (heap)
传输层协议------UDP协议
Open source CRM customer relationship system management system source code, free sharing
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
7.5 simulation summary