Codeforces #381(div2)
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)的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 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 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- 腾讯云centos7远程连接配置
1.申请腾讯云 注册腾讯云账号,申请一个centos7的服务器,1G内存,1核处理器,1M网速. 对于这种入门级配置,建议还是别用windows server了,不然不装任何东西,光运行系统就需要60 ...
- (办公)mysql连接不上(java.sql.SQLException: null, message from server: "Host 'LAPTOP-O0GA2P8J' is not allowed to connect to this MySQL server")(转)
转载自csdn文章:https://blog.csdn.net/Tangerine_bisto/article/details/803461511.对所有主机进行访问授权 GRANT ALL PRIV ...
- MySQL5.5.51启用网络远程连接
在其它电脑主机上访问时提示host ip is not allowed to connect to this mysql 下面代码为解决该问题的方法: :\Program Files\mysql-\b ...
- Web开发人员学习路线图
http://www.runoob.com/w3cnote/2018-web-developer.html
- python爬虫 | 一条高效的学习路径
数据是创造和决策的原材料,高质量的数据都价值不菲.而利用爬虫,我们可以获取大量的价值数据,经分析可以发挥巨大的价值,比如: 豆瓣.知乎:爬取优质答案,筛选出各话题下热门内容,探索用户的舆论导向. 淘宝 ...
- new 和 newInstance 的区别
初始化一个类,生成一个实例的时候:newInstance() 和 new 有什么区别? 用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有两 ...
- C# -- 使用委托 delegate 执行异步操作
C# -- 使用委托 delegate 执行异步操作 委托是一种安全地封装方法的类型,它与 C 和 C++ 中的函数指针类似. 与 C 中的函数指针不同,委托是面向对象的.类型安全的和保险的. 委托的 ...
- LeetCode算法题-Employee Importance(Java实现)
这是悦乐书的第291次更新,第309篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第159题(顺位题号是690).定义员工信息的数据结构,其中包括员工的唯一ID,他的重要 ...
- 【Python 20】BMR计算器4.0(异常处理)
1.案例描述 基础代谢率(BMR):我们安静状态下(通常为静卧状态)消耗的最低热量,人的其他活动都建立在这个基础上. 计算公式: BMR(男) = (13.7*体重kg)+(5.0*身高cm)-(6. ...
- c#语法学习
自动属性.隐试类型.命名参数和自动初始化器. note:这里说的这些,是语法糖.按照一定的格式写,部分代码编译器帮我们实现了, 1.自动属性:自动属性是非常有用的语法糖,帮我我们做了两件事:1.自动帮 ...