Codeforces 703B (模拟) Mishka and trip

题目:这里

题意:n个城市,每个城市有个魅力值vi,首先,有n条路将这n个城市连成一个环,1号城市连2号城市,2号连3号****n号连1号城市,每条路的魅力值是其连接的两个城市

的魅力值的乘积,这n个城市其中还有k个是特殊城市,每个特殊城市和任意一条城市都有一条路连接,看题目下面的图,保证每两个城市之间最多只有一条路,问所有路的

魅力值之和是多少?

首先是连接成环的路线魅力值,很好算,然后每个特殊城市的路线,先求出所有城市的魅力值之和sum,依次求特殊城市的时候用sum减去这个特殊城市本身以及两边相邻的城市

魅力值(因为相邻两边的路已经算过了),再乘这个特殊城市本身魅力值就行,注意这样可能会有路算重复了,而题目显然每条路只能算一次,每次再减去已经出现过的特殊城市

的魅力值之和就行了,注意可能之前出现的特殊城市可能会是现在的特殊城市的左右相邻的城市之一,这样的话就不用减了,因为已经减了,标记一下就行。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 typedef long long ll;
 8 const int M = 1e5 + 10;
 9 ll a[M];bool has[M];
10
11 int main()
12 {
13     int n,m;
14     ll sum=0,ans=0;
15     scanf("%d%d",&n,&m);
16     for (int i=1 ; i<=n ; i++)
17     {
18         scanf("%I64d",&a[i]);
19         if (i>1) ans+=(a[i]*a[i-1]);
20         sum+=a[i];
21     }
22     ans+=a[1]*a[n];
23     memset(has,false,sizeof(has));
24     ll cas=0;
25     while (m--)
26     {
27         int x,l,r;
28         scanf("%d",&x);
29         has[x]=true;
30         if (x-1==0) l=n;
31         else l=x-1;
32         if (x+1==n+1) r=1;
33         else r=x+1;
34         ans+=(sum-a[x]-a[l]-a[r])*a[x];
35         ll we=cas;
36         if (has[l]) we-=a[l];
37         if (has[r]) we-=a[r];
38         ans-=we*a[x];
39         cas+=a[x];
40     }
41     printf("%I64d\n",ans);
42     return 0;
43 }
时间: 2024-10-08 15:06:01

Codeforces 703B (模拟) Mishka and trip的相关文章

【打CF,学算法——二星级】Codeforces 703B Mishka and trip (统计)

[CF简介] 题目链接:CF 703B 题面: A - Mishka and trip Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 703B Description Little Mishka is a great traveller and she visited many countries. After thinking

暑假练习赛 003 F Mishka and trip

F - Mishka and trip Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Input Output Sample Input Sample Output Hint Description Peter Parker wants to play a g

code force Mishka and trip

标题:Mishka and trip 题意:给你n个城市及其权值,城市分为普通和省会城市,普通城市为有道路通向相邻的城市,省会通向所有的城市,路的权值等于城市权值的积,问所有路的权值和. 思路:因为省会城市是积相加,所以可以化成和的积,然后去除重和的部分,加上城市环上没有连接的部分就够了. #include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #includ

Codeforces 1113A. Sasha and His Trip 题解

原题链接:Codeforces 1113A. Sasha and His Trip 题目大意:你有一个油箱容积为\(V\)的车,每从第\(i\)个城市到第\(i+1\)个城市需要耗费一个单位的油,在第\(i\)个城市加油为\(i\)元每个单位,问从\(1\)号城市到\(n\)号城市最少花费. 题解:很明显的一个贪心,毕竟我们尽量早加油,因为费用便宜,所以,就直接给代码了: #include <cstdio> int mn(int a,int b){ return a<b?a:b; } i

Codeforces 703B Mishka and trip

题目大意:有n个城市,k个省会,每个城市有一个魅力值,两个城市之间的连线的价值等于两个城市魅力值的乘积.省会城市和每个城市相连,城市i和城市i+1相连,城市n与城市1相连.求所有连线的和. 题目思路:v[k]*v[1]+v[k]*v[2]+……v[k]*v[n]=v[k]*(v[1]+v[2]+……v[n]).为了节省时间预处理城市魅力值的和. #include<iostream> #include<algorithm> #include<cstring> #inclu

CodeForces 703A Mishka and trip

Description 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: XXX consists of n 

CodeForces - 427B (模拟题)

Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of t

CodeForces - 404B(模拟题)

Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose

codeforces 703D D. Mishka and Interesting sum(树状数组)

题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present h