A. Snowball

链接:http://codeforces.com/contest/1099/problem/A

思路:模拟

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1 const int M = 1e5 + ;
int n,m,x[],y[];
int main()
{
cin>>n>>m;
for(int i = ;i <= ;i ++){
cin>>x[i]>>y[i];
}
for(int i = m;i >= ;i --){
n += i;
for(int j = ;j <= ;j ++){
if(i == y[j])
n-=x[j];
}
n = max(n,);
}
cout<<n<<endl;
}

B. Squares and Segments

链接:http://codeforces.com/contest/1099/problem/B

思路:思维题

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1 const int M = 1e5 + ;
int n,k,cnt;
int main()
{
cin>>n;
for(int i = ;i <= n;i ++){
k = *i*i;
if(n <= k){
cnt = *i;break;
}
}
int ans = k - n;
if(ans < cnt) cout<<cnt*<<endl;
else if(ans < *cnt-) cout<<cnt*-<<endl;
else if(ans < *cnt-) cout<<cnt*-<<endl;
else cout<<cnt*-<<endl;
}

C. Postcard

链接:http://codeforces.com/contest/1099/problem/C

思路:题意可得,*号可以无限复制可以删除也可以不动,那么会让长度变长的只有*,*和?都可以使长度变短。那么根据当前序列长度和想要的长度,改一下就好了,答案不唯一

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1 const int M = 1e5 + ;
int n,ans,ans2,ans1;
int main()
{
string s;
cin>>s;
cin>>n;
int len = s.size();
for(int i = ;i < len;i ++){
if(s[i]=='*')
ans1++;
else if(s[i]=='?')
ans2++;
else
ans++;
}
if(ans-ans1-ans2 > n) cout<<"Impossible"<<endl;
else if(ans1==&&ans < n) cout<<"Impossible"<<endl;
else{
if(ans >= n){
int cnt = ans - n;
for(int i = ;i < len;i ++){
if((s[i+]=='?'||s[i+]=='*')&&cnt) cnt--;
else if(s[i]=='?'||s[i]=='*') continue;
else cout<<s[i];
}
cout<<endl;
}
else{
int cnt = n - ans;
for(int i = ;i < len;i ++){
if(s[i]=='*'&&cnt){
for(int j = ;j <= cnt;j ++){
cout<<s[i-];
}
cnt = ;
}
else if(s[i]!='*'&&s[i]!='?')cout<<s[i];
}
cout<<endl;
}
}
return ;
}

D. Sum in the tree

链接:http://codeforces.com/contest/1099/problem/D

思路:比较简单的dfs吧。。结果dfs的调用写捞了应该取完值再去调用,写成先调用再取值了。

因为题目给的赋值其实是当前节点到根节点的前缀和,那么对于偶数层为了使总体最优肯定是将他的值假设为子节点中最小的值,奇数层已经有赋值就可以不用处理,对于每个节点来说本身的值就是当前点的前缀和减去父节点的前缀和,最后统计一遍所有的点的本身值就好了

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1 const ll M = 1e5 + ;
const ll inf = 1e18+;
ll head[M],cnt,flag;
ll ans,a[M],b[M];
vector<ll>g[M];
void dfs(ll u,ll fa,ll dep){
ll mn = inf;
if(dep%==){
for(ll i = ;i < g[u].size();i ++){
ll v = g[u][i];
mn = min(mn,a[v]);
}
if(mn != inf) a[u] = mn,b[u] = a[u] - a[fa];
else a[u] = a[fa],b[u] = ;
}
else{
b[u] = a[u] - a[fa];
}
for(ll i = ;i < g[u].size();i ++){
ll v = g[u][i];
dfs(v,u,dep+);
}
} int main()
{
ll n,x;
scanf("%lld",&n);
for(ll i = ;i <= n;i ++){
scanf("%lld",&x);
g[x].push_back(i);
}
for(ll i = ;i <= n;i ++){
scanf("%lld",&a[i]);
}
dfs(,,);
for(ll i = ;i <= n;i ++){
ans += b[i];
if(b[i] < ) flag = ;
}
if(!flag) printf("%lld\n",ans);
else printf("-1\n");
}

Codeforces Round #530 (Div. 2) A,B,C,D的更多相关文章

  1. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  2. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...

  3. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  4. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  5. Codeforces Round #530 (Div. 2) C D

    C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...

  6. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  7. Codeforces Round #530 Div. 1 自闭记

    A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...

  8. Codeforces Round #530 (Div. 2) F - Cookies

    F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...

  9. Codeforces Round #530 (Div. 1)

    A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...

随机推荐

  1. 了解可执行的NPM包

    NPM是Node.js的包管理工具,随着Node.js的出现,以及前端开发开始使用gulp.webpack.rollup以及其他各种优秀的编译打包工具(大多数采用Node.js来实现),大家都开始接触 ...

  2. python-BeautifulSoup库详解

    快速使用 通过下面的一个例子,对bs4有个简单的了解,以及看一下它的强大之处: from bs4 import BeautifulSoup html = ''' <html><hea ...

  3. VUE工程上线首页加载慢问题优化

    使用webpack-bundle-analyzer工具 下面介绍几种压缩文件的方式. 1.vue-router懒加载 2.工程文件打包的时候不生成.map文件 3.gzip压缩 4.CDN 5.VUE ...

  4. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  5. eclipse中不能保存汉字的解决方法

    首先分清是打开jsp页面的问题还是java文件的问题?    对于java文件,只要在你的项目上点击右键选择“Propertise”(属性)然后点击“Info”标签将里面的Text file enco ...

  6. Python之random模块

    random模块 产生随机数的模块 是Python的标准模块,直接导入即可 import random 1)随机取一个整数,使用.randint()方法: import random print(ra ...

  7. 多线程系列之九:Worker Thread模式

    一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...

  8. [转帖]一个ip对应多个域名多个ssl证书配置-Nginx实现多域名证书HTTPS

    一个ip对应多个域名多个ssl证书配置-Nginx实现多域名证书HTTPS https://home.cnblogs.com/u/beyang/ 一台服务器,两个域名 首先购买https,获取到CA证 ...

  9. [转帖]HTTP 头部解释

    HTTP 头部解释 https://www.cnblogs.com/poissonnotes/p/4844014.html 之前看的太粗了 同事闻起来 referer 才知道自己所知甚少.. ==== ...

  10. python之tips(三)--为什么Python有相同的不可变对象id不同?

    参考 : https://www.jianshu.com/p/0f6f0db0ce8f