Coderforces 703B Mishka and trip
2016-08-15 20:12
344 查看
Mishka and trip
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
703B
Appoint description:
System Crawler (Aug 15, 2016 11:19:01 AM)
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 cities, k of whose (just imagine!) are capital cities.
All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 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.
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.
There is at most one road between any two cities.
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
betweena 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.
Sample Input
Input
Output
Input
Output
Hint
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.
思路:
首都城市与各个城市之间都有路,花费为 a[i] *( sum - a[i] ),sum为各个城市beauty values值之和,但是这样计算的话,首都城市与首都城市之间的路计算了两次,每次都要减去,#include<stdio.h>
#include<string.h>
int c[100010];
int id[100010];
bool vis[100010];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
long long sum=0;
long long ans=0;
long long cnt=0;//用int型会出错;
//int sum=0,ans=0,cnt=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%d",&c[i]);
sum+=c[i];
}
for(int i=1;i<=k;i++)
{
scanf("%d",&id[i]);
ans+=c[id[i]];
}
for(int i=1;i<=k;i++)
{
vis[id[i]]=1; //标记首都城市;
cnt+=(sum-c[id[i]])*c[id[i]];
cnt-=(ans-c[id[i]])*c[id[i]];
ans-=c[id[i]];//每次减去计算过的首都城市;
}
for(int i=1;i<n;i++)
{
if(!vis[i]&&!vis[i+1])
{
cnt+=c[i]*c[i+1];
}
}
if(!vis[1]&&!vis
)//1和n的时候要特判;
{
cnt+=c[1]*c
;
}
printf("%lld\n",cnt);
}
return 0;
}
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
703B
Appoint description:
System Crawler (Aug 15, 2016 11:19:01 AM)
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 cities, k of whose (just imagine!) are capital cities.
All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 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.
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.
There is at most one road between any two cities.
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
betweena 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.
Sample Input
Input
4 1 2 3 1 2 3
Output
17
Input
5 2 3 5 2 2 4 1 4
Output
71
Hint
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.
思路:
首都城市与各个城市之间都有路,花费为 a[i] *( sum - a[i] ),sum为各个城市beauty values值之和,但是这样计算的话,首都城市与首都城市之间的路计算了两次,每次都要减去,#include<stdio.h>
#include<string.h>
int c[100010];
int id[100010];
bool vis[100010];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
long long sum=0;
long long ans=0;
long long cnt=0;//用int型会出错;
//int sum=0,ans=0,cnt=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%d",&c[i]);
sum+=c[i];
}
for(int i=1;i<=k;i++)
{
scanf("%d",&id[i]);
ans+=c[id[i]];
}
for(int i=1;i<=k;i++)
{
vis[id[i]]=1; //标记首都城市;
cnt+=(sum-c[id[i]])*c[id[i]];
cnt-=(ans-c[id[i]])*c[id[i]];
ans-=c[id[i]];//每次减去计算过的首都城市;
}
for(int i=1;i<n;i++)
{
if(!vis[i]&&!vis[i+1])
{
cnt+=c[i]*c[i+1];
}
}
if(!vis[1]&&!vis
)//1和n的时候要特判;
{
cnt+=c[1]*c
;
}
printf("%lld\n",cnt);
}
return 0;
}
相关文章推荐
- Codeforces#365 (Div.2) Mishka and trip【思维】
- (模拟+数学)codeforces - 703B Mishka and trip
- Codeforces-703B Mishka and trip
- CodeForces 703B Mishka and trip【水题】
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforeces 703B Mishka and trip(路径权值的计算)
- Mishka and trip(CF #365 Div. 2)
- codeforces - 703B - Mishka and trip(数学)
- Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题
- 暑假练习赛 003 F Mishka and trip
- Mishka and trip 道路权值和
- Mishka and trip(CF 703B)
- Mishka and trip
- Codeforces Round #365 (Div. 2) B - Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- CodeForces703B Mishka and trip 数学
- Codeforces Round #365 (Div. 2) Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- 703B - Mishka and trip
- 【打CF,学算法——二星级】Codeforces 703B Mishka and trip (统计)