Mishka and trip

题意:

有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值。

题解:

先把n个城市存下来,之后开一个标记数组,来标记k个首都(这题这块很巧妙,正因为开了标记首都的数组,所以才把O(N^2)的算法降到了O(N)) 把所有城市的美丽值都加起来,遍历首都,每次遍历完就sum-首都,这样,最后求环的剩下的边就好了,环的剩下的边就是i不是首都,i+1也不是首都,那么就i*i+1

还有这题的输入都是从1开始的,如果你从0输入的要注意下,但如果从1输入的话,取模的时候更难弄,所以建议从0开始读入。

这题还有一个转化,就是乘积相加变为和乘乘积

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
#define PI(A) cout<<(A)<<endl
#define SI(N) cin>>(N)
#define SII(N,M) cin>>(N)>>(M)
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
#define dbg(x) cout <<#x<<" = "<<(x)<<endl
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS= 1e-9 ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAXN= 100000+ 9 ;
int a[MAXN];
bool used[MAXN];
int n,k; int main()
{
while(SII(n,k))
{
ll sum=0,ans=0,x;
cle(used,0);
rep(i,n)SI(a[i]),sum+=a[i];
rep(i,k)SI(x),used[x]=1;
rep(i,n)
{
if (used[i+1])
{
sum-=a[i];
ans+=a[i]*sum;
}
}
rep(i,n-1) if (!used[i+1]&&!used[i+2]) {ans+=a[i]*a[i+1];}
if (!used[n]&&!used[1]) ans+=a[n-1]*a[0];
PI(ans);
} return 0;
}

Codeforces Round #365 (Div. 2) Mishka and trip的更多相关文章

  1. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  2. Codeforces Round #365 (Div. 2)

    A题 Mishka and Game 水..随便统计一下就A了 #include <cstdio> #include <map> #include <set> #i ...

  3. Codeforces Round #365 (Div. 2) B 前缀和

    B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #365 (Div. 2) A 水

    A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #365 (Div. 2) A

    Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...

  6. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...

  7. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  8. Mishka and Divisors[CodeForces Round #365 Div.2]

    http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...

  9. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

随机推荐

  1. JS 排序算法

    冒泡排序: 核心思想:从第一个开始遍历数组,遍历完成后,让最小的值放在第一位:然后从第二个开始遍历数组,遍历完成后将最小值放在第二位:以此类推,所有位的数组遍历完成后就排序完成. var arr = ...

  2. A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning

    A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on S ...

  3. 一个非常简单的RPC服务

    1.servicefunctions.php <?php class ServiceFunctions { public static function getDisplayName($f,$l ...

  4. linux服务之ntp与chrony

    centos7.2变成了chrony RHEL从7.0开始改用chrony同步时间,原ntp同步方式也可以使用,但要安装ntp服务. [root@1st-kvm plymouth]# rpm -qa| ...

  5. shell算术运算与进制运算

    (())与let是等效的 arithmetic expression type 与[是等效的 source与.是等效的 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b ...

  6. jQuery Mobile_表单元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. eclipse 打开其他项目的jar源码 乱码解决

    步骤1.在eclipse菜单栏中,Window–>Preferences–>General–>Content types 将JAR Content , Java Class File ...

  8. sql常用语句--转载

    一.基础 .说明:创建数据库 CREATE DATABASE database-name .说明:删除数据库 drop database dbname .说明:备份sql server --- 创建 ...

  9. EV电池指标及特点

    在电池的大家族中,蓄电池的种类是最多的,共同的特点是可以经历多次充电.放电循环,反复使用,这也正是蓄电池作为电动汽车动力源的基础.当然,并不是所有的蓄电池都适合应用于电动汽车,从全球新能源汽车的发展来 ...

  10. linux下的chmod,chown和chgrp

    对于linux的权限掌握以下几个命令就可以非常熟练的操作系统中的各种权限了. 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode f ...