TTTTTTTTTTTTT CF#365 div2 B 统计点

本文探讨了一种特殊类型的图论问题,其中包含n个节点,部分节点具有特殊属性,并与其他所有节点相连。文章详细解释了如何计算这些连接边的权值总和,通过巧妙的方法避免重复计算。
B. Mishka and trip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.

Here are some interesting facts about XXX:

  1. XXX consists of n cities, k of whose (just imagine!) are capital cities.
  2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals toci.
  3. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.
  4. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i.
  5. There is at most one road between any two cities.
  6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.

Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b(a < b), such that there is a road between a and b you are to find sum of products ca·cb. Will you help her?

Input

The first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them.

The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities.

The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.

Output

Print the only integer — summary price of passing each of the roads in XXX.

Examples
input
4 1
2 3 1 2
3
output
17
input
5 2
3 5 2 2 4
1 4
output
71
Note

This image describes first sample case:

It is easy to see that summary price is equal to 17.

This image describes second sample case:

It is easy to see that summary price is equal to 71.

 题意:给你n个点,n个点按标号一次首位相接,其中有m个特殊的点,这m个点跟其他的所有点都相连接,点之间连接的线

的权值为点的权值之积,任意两个点之间至多一条边,最后问你整个图边的权值之和

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef  long long  ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e5+100;

int ncap[N],flag[N];
ll a[N];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        ll all=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            all+=a[i];
        }
        a[n+1]=a[1];
        a[0]=a[n];
        for(int i=1;i<=m;i++) scanf("%d",&ncap[i]);
        ll ans=0,tmp=0;
        for(int i=1;i<=n;i++) ans+=a[i+1]*a[i];
        MM(flag,0);
        for(int i=1;i<=m;i++)
            {
               int cur=ncap[i],l=cur-1,r=cur+1;
               flag[cur]=1;
               if(l==0) l=n;
               if(r==n+1) r=1;
               ans+=a[cur]*(all-a[cur]-tmp);
               if(!flag[l]) ans-=a[cur]*a[l];
               if(!flag[r]) ans-=a[cur]*a[r];
               tmp+=a[cur];
            }
        printf("%lld\n",ans);
    }
    return 0;
}

  分析:有点锻炼思维,首先ans初始值为n个点围成的一个圈的边权值之和,然后对于每个特殊的点,它所能增加的边权值之和

为   该点权值( 所有点的权值之和-该点权值-先前遍历过的特殊的点的权值-与其在环上直接相连的两点权值之和)

 不过有时候因为先前遍历过的点可能跟与在环上直接相连的点有重合,所以要设置一个flag,避免再被减一

次。

转载于:https://www.cnblogs.com/smilesundream/p/5742743.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值