571B. Minimization(Codeforces Round #317)

探讨如何通过重新排列数组中的整数来最小化特定数学表达式的值。针对包含n个整数的数组A及正整数k,本篇介绍了一种算法,通过将数组分为k组并适当调整各组内的元素顺序,从而实现整个数组相邻元素差值绝对值之和的最小化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Minimization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.

You need to permute the array elements so that value

became minimal possible. In particular, it is allowed not to change order of elements at all.
Input

The first line contains two integers n, k (2 ≤ n ≤ 3·1051 ≤ k ≤ min(5000, n - 1)).

The second line contains n integers A[1], A[2], ..., A[n] ( - 109 ≤ A[i] ≤ 109), separate by spaces — elements of the array A.

Output

Print the minimum possible value of the sum described in the statement.

Sample test(s)
input
3 2
1 2 4
output
1
input
5 2
3 -5 3 -5 3
output
0
input
6 3
4 3 4 3 2 5
output
3
Note

In the first test one of the optimal permutations is 1 4 2.

In the second test the initial order is optimal.

In the third test one of the optimal permutations is 2 3 4 4 3 5.


解题思路:

      将数组分成k组,分别是i,i+k,i+2*k,i+3*k...(1<=i<=k),有x=n%k个组元素个数是n/k+1个,问题就转化为k组内

相邻元素差值的和的最小值,这时就需要对数组进行排序,只有每组内的元素都是有序的,每组内的相邻元素的

差值才会最小,接着就是在k组内分x组长度为n/k+1,这时就需要dp,dp[i][j],i是分了i组,j组长度是n/k+1;dp方

程为dp[i][j]=max(dp[i-1][j]+dp[i-1][j-1])+(a[i*n/k+j+1]-a[i*n/k+j]),ans=a[n]-a[1]-dp[k][x],a[i*n/k+j+1]-a[i*n/k+j]是要

从分第i组是,第i组的第1个元素与第i-1组的最后一个元素的差值。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=500000+100;
int a[maxn];
int s[maxn];
int bbs(int x)
{
    if(x<0)
    return -x;
    return x;
}
int dp[5500][5500];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    memset(dp,-1,sizeof(dp));
    int sum=a[n]-a[1];
    int q=n/k;
    int x=n%k;
    dp[0][0]=0;
    for(int i=1;i<=k;i++)
    {
        for(int j=0;j<=x;j++)
        {
            int df;
            if(j==0)
            df=dp[i-1][j];
            else
            df=max(dp[i-1][j],dp[i-1][j-1]);
            if(df<0)
            continue;
            if(i==k&&j==x)
            dp[i][j]=df;
            else
            dp[i][j]=df+a[i*q+j+1]-a[i*q+j];
        }
    }
    int ans=sum-dp[k][x];
    cout<<ans<<endl;
    return 0;
}


LAMMPS (27 May 2021) OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98) using 1 OpenMP thread(s) per MPI task # CPU-only模式测试脚本 (24线程) units metal atom_style charge boundary p p p neighbor 0.3 bin neigh_modify delay 5 every 1 check yes package omp 24 # 启用24个OpenMP线程 set 24 OpenMP thread(s) per MPI task using multi-threaded neighbor list subroutines # 创建原子结构 region box block 0 200 0 200 0 200 create_box 6 box Created orthogonal box = (0.0000000 0.0000000 0.0000000) to (200.00000 200.00000 200.00000) 1 by 1 by 1 MPI processor grid create_atoms 1 random 1030 8763 box Created 1030 atoms create_atoms CPU = 0.000 seconds create_atoms 2 random 1213 23456 box Created 1213 atoms create_atoms CPU = 0.000 seconds create_atoms 3 random 1833 34567 box Created 1833 atoms create_atoms CPU = 0.000 seconds create_atoms 4 random 697 45678 box Created 697 atoms create_atoms CPU = 0.000 seconds create_atoms 5 random 88 56789 box Created 88 atoms create_atoms CPU = 0.000 seconds create_atoms 6 random 138 67890 box Created 138 atoms create_atoms CPU = 0.000 seconds # 设置原子属性和电荷 mass 1 40.08 # Ca mass 2 19.00 # F mass 3 16.00 # O mass 4 26.98 # Al mass 5 24.30 # Mg mass 6 28.09 # Si set type 1 charge +2.0 # Ca2+ Setting atom values ... 1030 settings made for charge set type 2 charge -1.0 # F- Setting atom values ... 1213 settings made for charge set type 3 charge -2.0 # O2- Setting atom values ... 1833 settings made for charge set type 4 charge +3.0 # Al3+ Setting atom values ... 697 settings made for charge set type 5 charge +2.0 # Mg2+ Setting atom values ... 88 settings made for charge set type 6 charge +4.0 # Si4+ Setting atom values ... 138 settings made for charge # 势函数设置 pair_style buck/coul/long/omp 10.0 10.0 kspace_style pppm 0.0001 pair_coeff 1 1 329193.320 0.16 4.3369 # Ca-Ca pair_coeff 1 2 496191.600 0.165 8.67 # Ca-F pair_coeff 1 3 718136.080 0.165 8.67 # Ca-O pair_coeff 1 4 36918.570 0.16 0.0 # Ca-Al pair_coeff 1 5 67720.910 0.16 0.8674 # Ca-Mg pair_coeff 1 6 26674.680 0.16 0.0 # Ca-Si pair_coeff 2 2 730722.800 0.17 17.34 # F-F pair_coeff 2 3 1046135.400 0.17 17.34 # F-O pair_coeff 2 4 59481.584 0.165 0.0 # F-Al pair_coeff 2 5 107085.519 0.165 1.7347 # F-Mg pair_coeff 2 6 43406.000 0.165 0.0 # F-Si pair_coeff 3 3 1497693.500 0.17 17.34 # O-O pair_coeff 3 4 86057.580 0.165 0.0 # O-Al pair_coeff 3 5 154984.640 0.165 1.7347 # O-Mg pair_coeff 3 6 62821.410 0.165 0.0 # O-Si pair_coeff 4 4 4142.149 0.16 0.0 # Al-Al pair_coeff 4 5 7600.000 0.16 0.0 # Al-Mg pair_coeff 4 6 2990.000 0.16 0.0 # Al-Si pair_coeff 5 5 13931.400 0.16 0.1735 # Mg-Mg pair_coeff 5 6 5489.810 0.16 0.0 # Mg-Si pair_coeff 6 6 2163.320 0.16 0.0 # Si-Si # 测试流程 reset_timestep 0 velocity all create 4000.0 12345 minimize 1.0e-5 1.0e-7 1000 10000 WARNING: Using &#39;neigh_modify every 1 delay 0 check yes&#39; setting during minimization (src/min.cpp:189) PPPM initialization ... using 12-bit tables for long-range coulomb (src/kspace.cpp:340) G vector (1/distance) = 0.25738067 grid = 96 96 96 stencil order = 5 estimated absolute RMS force accuracy = 0.001834551 estimated relative force accuracy = 0.00012740252 using double precision FFTW3 3d grid and FFT values/proc = 1030301 884736 Last active /omp style is pair_style buck/coul/long/omp Neighbor list info ... update every 1 steps, delay 0 steps, check yes max neighbors/atom: 2000, page size: 100000 master list distance cutoff = 10.3 ghost atom cutoff = 10.3 binsize = 5.15, bins = 39 39 39 1 neighbor lists, perpetual/occasional/extra = 1 0 0 (1) pair buck/coul/long/omp, perpetual attributes: half, newton on, omp pair build: half/bin/atomonly/newton/omp stencil: half/bin/3d bin: standard Per MPI rank memory allocation (min/avg/max) = 139.2 | 139.2 | 139.2 Mbytes Step Temp E_pair E_mol TotEng Press 0 4000 161444.12 0 164028.28 30975.418 1000 4000 -73046.543 0 -70462.374 -72.475049 Loop time of 74.6223 on 24 procs for 1000 steps with 4999 atoms 35.5% CPU use with 1 MPI tasks x 24 OpenMP threads Minimization stats: Stopping criterion = max iterations Energy initial, next-to-last, final = 161444.115470866 -73017.6575217809 -73046.5432936927 Force two-norm initial, final = 703323.08 66.571396 Force max component initial, final = 473215.26 23.702297 Final line search alpha, max atom move = 0.0033242332 0.078791963 Iterations, force evaluations = 1000 1008 MPI task timing breakdown: Section | min time | avg time | max time |%varavg| %total --------------------------------------------------------------- Pair | 0.41604 | 0.41604 | 0.41604 | 0.0 | 0.56 Kspace | 73.642 | 73.642 | 73.642 | 0.0 | 98.69 Neigh | 0.18951 | 0.18951 | 0.18951 | 0.0 | 0.25 Comm | 0.035384 | 0.035384 | 0.035384 | 0.0 | 0.05 Output | 7.41e-05 | 7.41e-05 | 7.41e-05 | 0.0 | 0.00 Modify | 0.19129 | 0.19129 | 0.19129 | 0.0 | 0.26 Other | | 0.1481 | | | 0.20 Nlocal: 4999.00 ave 4999 max 4999 min Histogram: 1 0 0 0 0 0 0 0 0 0 Nghost: 1756.00 ave 1756 max 1756 min Histogram: 1 0 0 0 0 0 0 0 0 0 Neighs: 7283.00 ave 7283 max 7283 min Histogram: 1 0 0 0 0 0 0 0 0 0 Total # of neighbors = 7283 Ave neighs/atom = 1.4568914 Neighbor list builds = 505 Dangerous builds = 15 # 预热运行 fix warmup all nve run 10 PPPM initialization ... using 12-bit tables for long-range coulomb (src/kspace.cpp:340) G vector (1/distance) = 0.25738067 grid = 96 96 96 stencil order = 5 estimated absolute RMS force accuracy = 0.001834551 estimated relative force accuracy = 0.00012740252 using double precision FFTW3 3d grid and FFT values/proc = 1030301 884736 Last active /omp style is pair_style buck/coul/long/omp Per MPI rank memory allocation (min/avg/max) = 138.1 | 138.1 | 138.1 Mbytes Step Temp E_pair E_mol TotEng Press 1000 4000 -73046.543 0 -70462.374 -72.475049 1010 3601.0323 -72784.93 0 -70458.512 -307.51509 Loop time of 0.71717 on 24 procs for 10 steps with 4999 atoms Performance: 1.205 ns/day, 19.921 hours/ns, 13.944 timesteps/s 37.0% CPU use with 1 MPI tasks x 24 OpenMP threads MPI task timing breakdown: Section | min time | avg time | max time |%varavg| %total --------------------------------------------------------------- Pair | 0.0036188 | 0.0036188 | 0.0036188 | 0.0 | 0.50 Kspace | 0.70951 | 0.70951 | 0.70951 | 0.0 | 98.93 Neigh | 0.0011555 | 0.0011555 | 0.0011555 | 0.0 | 0.16 Comm | 0.0002097 | 0.0002097 | 0.0002097 | 0.0 | 0.03 Output | 7.08e-05 | 7.08e-05 | 7.08e-05 | 0.0 | 0.01 Modify | 0.0023679 | 0.0023679 | 0.0023679 | 0.0 | 0.33 Other | | 0.0002373 | | | 0.03 Nlocal: 4999.00 ave 4999 max 4999 min Histogram: 1 0 0 0 0 0 0 0 0 0 Nghost: 1754.00 ave 1754 max 1754 min Histogram: 1 0 0 0 0 0 0 0 0 0 Neighs: 7283.00 ave 7283 max 7283 min Histogram: 1 0 0 0 0 0 0 0 0 0 Total # of neighbors = 7283 Ave neighs/atom = 1.4568914 Neighbor list builds = 2 Dangerous builds = 2 unfix warmup # 性能测试 - 使用系统时间测量 reset_timestep 0 variable start_time equal time() # 记录开始时间 fix benchmark all nve run 100 PPPM initialization ... using 12-bit tables for long-range coulomb (src/kspace.cpp:340) G vector (1/distance) = 0.25738067 grid = 96 96 96 stencil order = 5 estimated absolute RMS force accuracy = 0.001834551 estimated relative force accuracy = 0.00012740252 using double precision FFTW3 3d grid and FFT values/proc = 1030301 884736 Last active /omp style is pair_style buck/coul/long/omp Per MPI rank memory allocation (min/avg/max) = 138.1 | 138.1 | 138.1 Mbytes Step Temp E_pair E_mol TotEng Press 0 3601.0323 -72784.93 0 -70458.512 -307.51509 100 6387.3593 -74588.666 0 -70462.163 91.600785 Loop time of 7.21601 on 24 procs for 100 steps with 4999 atoms Performance: 1.197 ns/day, 20.044 hours/ns, 13.858 timesteps/s 8.7% CPU use with 1 MPI tasks x 24 OpenMP threads MPI task timing breakdown: Section | min time | avg time | max time |%varavg| %total --------------------------------------------------------------- Pair | 0.030583 | 0.030583 | 0.030583 | 0.0 | 0.42 Kspace | 7.1545 | 7.1545 | 7.1545 | 0.0 | 99.15 Neigh | 0.0067331 | 0.0067331 | 0.0067331 | 0.0 | 0.09 Comm | 0.0019693 | 0.0019693 | 0.0019693 | 0.0 | 0.03 Output | 7.72e-05 | 7.72e-05 | 7.72e-05 | 0.0 | 0.00 Modify | 0.019508 | 0.019508 | 0.019508 | 0.0 | 0.27 Other | | 0.002622 | | | 0.04 Nlocal: 4999.00 ave 4999 max 4999 min Histogram: 1 0 0 0 0 0 0 0 0 0 Nghost: 1760.00 ave 1760 max 1760 min Histogram: 1 0 0 0 0 0 0 0 0 0 Neighs: 7718.00 ave 7718 max 7718 min Histogram: 1 0 0 0 0 0 0 0 0 0 Total # of neighbors = 7718 Ave neighs/atom = 1.5439088 Neighbor list builds = 20 Dangerous builds = 20 unfix benchmark variable end_time equal time() # 记录结束时间 variable time_run equal v_end_time - v_start_time # 正确写法:使用变量名而非${} ERROR: Illegal variable command (src/variable.cpp:410) Last command: variable time_run equal v_end_time - v_start_time # 正确写法:使用变量名而非${}
最新发布
07-11
<think>我们正在处理用户关于LAMMPS模拟中出现的"Illegal variable"命令错误的问题。根据用户描述,错误发生在变量定义阶段。我们将结合引用内容,特别是引用[1]中关于variable命令的说明,来诊断和解决此问题。 引用[1]指出,variable命令用于定义多种类型的变量(常量、公式、循环变量等)。常见的错误原因包括: 1. 变量名不合法(如使用LAMMPS保留字或特殊字符) 2. 语法错误(如缺少参数、括号不匹配等) 3. 变量类型选择错误(如将公式变量误定义为常量) 4. 在变量定义中引用了尚未定义的变量 解决步骤: 1. 检查变量名:确保变量名以字母开头,仅包含字母、数字和下划线,且不是LAMMPS保留字(如step、dt等)。 2. 检查语法:根据变量类型,确认参数数量和格式正确。例如,公式变量需要用字符串表示且用引号括起,如`variable k equal "1.0/300.0"`。 3. 检查变量引用:若变量定义中引用了其他变量(如在公式中使用另一个变量),确保被引用的变量已定义。 4. 检查作用域:LAMMPS变量有全局和局部(如per-atom)之分,确保在正确的上下文中使用。 示例错误案例及修正: - 错误:`variable T constant 300` → 缺少变量名。修正:`variable myTemp constant 300` - 错误:`variable k equal 1/300` → 公式未用引号括起。修正:`variable k equal "1.0/300.0"` - 错误:`variable ratio equal v_k*2` → 若`v_k`未定义则报错。需先定义`k`。 根据引用[1]的指导,我们给出一个正确示例: ```lammps # 定义常量温度 variable T0 constant 300.0 # 定义公式计算温度随时间变化 variable T equal "v_T0 * (1 + sin(step*0.01))" ``` 如果问题仍未解决,建议用户提供具体的变量定义行以便进一步分析。 相关问题: 1. LAMMPS中如何定义依赖于时间的变量? 2. 如何正确使用LAMMPS的循环变量(loop)? 3. 在LAMMPS中,全局变量和原子变量有什么区别?</think>### LAMMPS变量定义错误分析与解决 您在LAMMPS模拟中遇到的`Illegal variable command`错误通常由以下原因引起: #### 常见错误原因及解决方案: 1. **语法格式错误** - ❌ 错误示例:`variable x = 5` - ✅ 正确格式:`variable x equal 5` - 必须明确变量类型关键字(如`equal`, `atom`, `loop`等) 2. **未定义的变量引用** - ❌ 错误示例:`variable k equal "v_temp*0.1"`(若`v_temp`未定义) - ✅ 解决方案:先定义被引用变量 ```lammps variable temp equal 300 variable k equal "v_temp*0.1" ``` 3. **非法变量名** - ❌ 错误:使用保留字(`step`, `dt`, `x`等)或特殊字符 - ✅ 规范:字母开头,仅含字母/数字/下划线 ```lammps variable my_force equal 10.0 # 合法 variable 2mass equal 5.0 # 非法(数字开头) ``` 4. **公式语法错误** - ❌ 括号不匹配:`variable f equal "(sin(time)+cos(time"` - ✅ 修正:`variable f equal "(sin(time)+cos(time))"` - ❌ 函数名错误:`variable f equal "sine(x)"` - ✅ 正确:`variable f equal "sin(x)"` 5. **作用域冲突** - ❌ 重复定义:同一作用域多次定义同名变量 - ✅ 解决方案:使用`delete`命令移除旧变量 ```lammps delete variable x variable x equal 20 ``` #### 调试建议: 1. 逐步注释变量定义,定位出错行 2. 使用`print`命令验证变量值: ```lammps variable test equal 3.14 print "测试变量值: $(test)" ``` 3. 检查依赖关系,确保变量定义顺序正确 > 引用说明:LAMMPS的`variable`命令支持多种变量类型,包括常量、公式、原子属性和循环变量等[^1]。正确理解变量类型和语法是避免错误的关键。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值