A.题目:http://codeforces.com/contest/740/problem/A

 题意:现有n本书,买一本书需要花a元,两本书b元,三本书c元,问买够书是4的倍数所需要的最小花费

 思路:n%4=1的时候可以3a a+b c n%4=2 2a b 2c n%4==3 a b+c 3c 取个最小值就好了

  

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
long long n,a,b,c;
while(scanf("%lld %lld %lld %lld",&n,&a,&b,&c)!=EOF){
long long ans;
if(n%==){
printf("0\n");
continue;
}
else if(n%==){
ans=min(*a,c);
ans=min(a+b,ans);
}
else if(n%==){
ans=min(*a,b);
ans=min(ans,c*);
}
else if(n%==){
ans=min(a,b+c);
ans=min(ans,*c);
}
printf("%lld\n",ans);
}
return ;
}

B:题目:http://codeforces.com/contest/740/problem/B

  题意:有n朵花每朵花有自己的价值ai,正或者负,现在有m个建议的区间,选择其中一些区间,计算花被选中的次数ki,求s=(a1*k1+a2*k2...+an*kn)的最大值

  思路:最后总的值是看每个区间的贡献,也就是区间的价值如果大于0就加上去

  代码:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=;
int a[maxn];
int n,m;
struct node{
int l,r;
};
node num[maxn];
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int sum=;
for(int i=;i<=m;i++){
scanf("%d %d",&num[i].l,&num[i].r);
int val=;
for(int j=num[i].l;j<=num[i].r;j++){
val+=a[j];
}
if(val>) sum+=val;
}
printf("%d\n",sum);
}
return ;
}

C:题目:http://codeforces.com/contest/740/problem/C

  题意:有m个区间,构造一个长度为n的序列,使得m个区间中的mex(a[i]..a[j])的最小值最大,mex(a[i]..a[j])代表i到j这个区间中最小的没有出现过的非负整数

  思路:m个区间中最小mex肯定是看区间长度最小的那一个并且mex=区间长度,之后的区间长度都大于或者等于它,而我们只需要mex最小的尽量大就行了,也就是其他区间的只要一定有区间长度最小的那个区间里面的数就行了,那么我们循环输出最小的区间长度,那么m个区间里面肯定都会包括这些数

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=1e5+;
int n,m;
int a[maxn];
set<int> s;
struct node{
int l,r;
int len;
};
node num[maxn];
bool cmp(const node&a,const node&b){
return a.len<b.len;
}
bool cmp1(const node&a,const node&b){
return a.l<b.l;
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=;i<=m;i++){
int x,y;
scanf("%d %d",&x,&y);
num[i].l=x,num[i].r=y;
num[i].len=y-x+;
}
sort(num+,num++m,cmp);
int tmp=num[].len;
printf("%d\n",tmp);
for(int i=;i<=n;i++){
printf("%d%c",i%tmp,i==n?'\n':' ');
} }
return ;
}

D:题目:http://codeforces.com/contest/740/problem/D

  题意:有n个节点的树,以1为根,每个点有权值a[i],每条边也有权值w[i],如果v是u的父亲节点并且dis(u,v)<=au 则表示v能控制u点,输出每个点能控制多少个点

  思路:注意到u是儿子节点,也就是找到最远能控制u的父亲节点v,那么这一条路上所有的u的父亲都能控制u,也就是给这条路径上所有的节点+1,那么输出的时候只要统计每个节点的子树和就行了,当前节点是u,最远的能控制u的父亲节点v,假设v的父亲节点是v0,dfs序给树编号,那么一棵子树的编号肯定都是连着的,用树状数组维护和,查询v能控制多少个节点的时候只要查询v的子树和,但v0控制不了u,也就是在v0的位置-1,倍增找节点。

 #include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=2e5+;
struct node{
int u;
int v;
long long dis;
};
vector<node> G[maxn];
int id[maxn];
int outId[maxn];
int fa[maxn][];
long long dis[maxn];
long long tree[maxn<<];
int n;
void add(int i,int k){
for(;i<=maxn;i+=(i&(-i))) tree[i]+=k;
}
long long query(int i){
long long sum=;
for(;i>;i-=(i&(-i))) sum+=tree[i];
return sum;
}
int cnt=;
void dfs(int x){
id[x]=++cnt;
for(int i=;i<=;i++){
fa[x][i]=fa[fa[x][i-]][i-];
}
for(int i=;i<G[x].size();i++){
int v=G[x][i].v;
if(fa[v][]) continue;
fa[v][]=x;
dis[v]=dis[x]+G[x][i].dis;
dfs(v);
}
outId[x]=cnt;
}
int a[maxn];
int main(){
scanf("%d",&n);
cnt=;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++){
int x,y;
scanf("%d %d",&x,&y);
node tmp;
tmp.dis=y,tmp.v=i;
G[x].push_back(tmp);
}
fa[][]=;
dis[]=;
dfs();
long long Dis;
for(int i=;i<=n;i++){
int now=i;
add(id[i],);
for(int j=;j>=;j--){
if (dis[i]-dis[fa[now][j]]<=a[i]) now=fa[now][j];
}
if (now!=) add(id[fa[now][]],-);
}
for(int i=;i<=n;i++){
printf("%lld ",query(outId[i])-query(id[i]-)-);
}
printf("\n");
}

不用倍增 二分栈的写法

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=2e5+;
long long stk[maxn];
struct node{
int v;
long long dis;
};
int a[maxn];
long long dis[maxn];
int ans[maxn];
int stkid[maxn];
vector<node> G[maxn];
void dfs(int x,int cnt){
stk[cnt]=dis[x];
stkid[cnt]=x;
int v0=lower_bound(stk,stk+cnt+,dis[x]-a[x])-stk-;
ans[x]++,ans[stkid[v0]]--;
for(int i=;i<G[x].size();i++){
node it=G[x][i];
dis[it.v]=dis[x]+it.dis;
dfs(it.v,cnt+);
ans[x]+=ans[it.v];
}
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
G[i].clear();
}
for(int i=;i<=n;i++){
int x;
node tmp;
tmp.v=i;
scanf("%d %lld",&x,&tmp.dis);
G[x].push_back(tmp);
}
dis[]=;
dfs(,);
for(int i=;i<=n;i++){
printf("%d ",ans[i]-);
}
printf("\n");
}
return ;
}

E,F留坑

Codeforces #381(div2)的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. codeforces Round#381 div2

    第一题: 按余数分类,1,2,3分别由哪些基数组成 1->[1][2+3][3+3+3] 2->[1+1][2][3+3] 3->[1+1+1][1+2][3] #include&l ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. java 线程方法 ---- yiled()

    class MyThread3 implements Runnable{ @Override public void run() { for (int i = 0; i < 3; i++){ / ...

  2. SpringBoot Web学习笔记

    一.资源的访问: 情形一.所有的  /webjars/**  都会去 classpath:/META_INFO/resource/webjars/ 下找资源: webjars:以jar包的方式引入静态 ...

  3. GNUstep 快捷键编译

    $ gcc `gnustep-config --objc-flags` -L /GNUorld.m -o helloworld -lgnustep-base -lobjc $ ./helloworld ...

  4. Vysor Pro破解助手

    Vysor更新到1.7.7版本后,原来提供的VysorPro助手无法正常破解了. 针对新版本的改动,更新了一下Vysor破解助手,支持破解Vysor 1.6.6和Vysor1.7.7之间的版本. Vy ...

  5. ionic3 Modal组件

     Modal组件主要用来弹出一些临时的框,如登录,注册的时候用 弹出页面html页面 <button ion-button small outline color="he" ...

  6. JHipster技术栈定制 - 基于UAA的微服务之间安全调用

    本文通过代码实例演示如何通过UAA实现微服务之间的安全调用. uaa: 身份认证服务,同时也作为被调用的资源服务.服务端口9999. microservice1: 调用uaa的消费者服务,服务端口80 ...

  7. 容器化系列 - Zookeeper启动和配置 on Docker

    本文简要说明了如何在Docker容器中启动和配置Zookeeper. 1 准备工作 1.1 下载zookeeper镜像 $ docker pull zookeeper:3.4 1.2 单点模式 安装D ...

  8. Linux电源管理(9)_wakelocks【转】

    1. 前言 wakelocks是一个有故事的功能. wakelocks最初出现在Android为linux kernel打的一个补丁集上,该补丁集实现了一个名称为"wakelocks&quo ...

  9. 文件操作命令(rename)

    Rename 命令: // 描述: 重命名文件或目录. // 语法: rename [<Drive>:][<Path>]<FileName1> <FileNa ...

  10. Ubuntu 16.04 启用 点击Launcher图标,窗口实现最小化 功能

    安装了Ubuntu之后,要是每次都点击最小化按钮来实现窗口的最小化,操作起来很不方便,那么怎么样才能方便操作呢, Ubuntu 16.04 本身支持 点击应用程序Launcher图标实现最小化 功能, ...