Codeforces Round #365 (Div. 2) Mishka and trip
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的更多相关文章
- Codeforces Round #365 (Div. 2)  C - Chris and Road 二分找切点
		
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
 - Codeforces Round #365 (Div. 2)
		
A题 Mishka and Game 水..随便统计一下就A了 #include <cstdio> #include <map> #include <set> #i ...
 - 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 ...
 - 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 ...
 - Codeforces Round #365 (Div. 2) A
		
Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...
 - Codeforces Round #365 (Div. 2) B - Mishka and trip
		
http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...
 - 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 ...
 - Mishka and Divisors[CodeForces Round #365 Div.2]
		
http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...
 - Codeforces Round #365 (Div. 2)-D  Mishka and Interesting sum(树状数组)
		
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
 
随机推荐
- linux之du命令
			
du命令:disk usage,顾名思义,是关于目录使用情况的.对了- 它的作用就是计算目录大小的. 1. 想看当前目录下所有目录以及子目录的大小: # du -h . “.”代表当前目录下.也可以换 ...
 - Windows Server 2012学习文档
			
1.Windows Server 2012版本 Windows Server 2012 实际只有两个版本(标准版和数据中心版),其他仅是OEM的相关名称 这两个版本的功能内容完全一样,唯一不同的是标准 ...
 - poj3169 最短路(差分约束)
			
题意:一个农夫有n头牛,他希望将这些牛按照编号 1-n排成一条直线,允许有几头牛站在同一点,但是必须按照顺序,有一些牛关系比较好,希望站的距离不超过某个值,而有一些牛关系不太好,所以希望站的距离大于等 ...
 - C++@重载函数
			
关于重载详细分析参考: http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html 内部机制涉及重载函数如何解决命名冲突,调用匹配的问题 ...
 - java编程之:org.apache.commons.lang3.text.StrTokenizer
			
第一个api测试:按特殊符号进行分词,并遍历每一个分词部分 public static void main(String[] args) { String aString="AB-CD-EF ...
 - java编程之:按位与运算,等运算规则
			
按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1; 即:两位 ...
 - MiniCRT 64位 linux 系统移植记录:64位gcc的几点注意
			
32位未修改源码与修改版的代码下载: git clone git@github.com:youzhonghui/MiniCRT.git MiniCRT 64位 linux 系统移植记录 MiniCRT ...
 - C# Regex.IsMatch (正则表达式验证:数字、小数点、邮件、计算表达式)
			
public bool isInt(string str) { //^([+-]?)表示加减号只能出现在字符串开头且只有一位 ///d*表示后面可以有多个或一个十进制数 //$表示字符串结尾 retu ...
 - Lucene 对文档打分的规则整理记录
			
摘引自:http://www.cnblogs.com/forfuture1978/archive/2010/02/08/1666137.html Lucene的搜索结果默认按相关度排序,这个相关度排序 ...
 - Unity3d 无网络权限下打开网站
			
有人问“更多游戏”没有网络权限怎么实现,其实调用浏览器访问外部链接不需要网络通讯权限,代码如下: Uri moreGame = Uri.parse("http://wapgame.189.c ...