Codeforces Round #530 (Div. 2) A,B,C,D
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的更多相关文章
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- 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号结 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) C D
C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces Round #530 Div. 1 自闭记
A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...
- Codeforces Round #530 (Div. 2) F - Cookies
F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
随机推荐
- make太慢了,加快编译速度的方法 make -j
make -j 既然IO不是瓶颈,那CPU就应该是一个影响编译速度的重要因素了. 用make -j带一个参数,可以把项目在进行并行编译,比如在一台双核的机器上,完全可以用make -j4,让make最 ...
- python代码风格指南:pep8 中文版
本文档所提供的编码规范,适用于主要的Python发行版中组成标准库的Python代码.请参阅PEP关于Python的C实现的C编码风格指南的描述. 本文档和PEP257(文档字符串规范)改编自Guid ...
- 解决mysql1336
1.mysql字符集与插入数据字符集不匹配 USE 数据库名称SHOW VARIABLES LIKE 'character%'SET character_set_server=utf8;SET cha ...
- winform自定义控件开发
1.添加控件属性 //添加私有的控件属性 private string djm;//单据名 //添加属性描述 [Browsable(true)] [Description("djm" ...
- 《Effective C++》资源管理:条款13-条款17
条款13:以对象管理资源 为了防止资源泄漏,请使用RAII(Resource Acquisition Is Initialization)对象,在构造函数里面获得资源,在析构函数里面释放资源 auto ...
- 源码追踪,解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题
在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序运行. >>提君博客原创 http://www.cnblogs.com/tijun/ & ...
- 在linux命令下访问url
1.elinks - lynx-like替代角色模式WWW的浏览器 例如: elinks --dump http://www.baidu.com 2.wget 这个会将访问的首页下载到本地 [root ...
- Linux基础学习(16)--备份与恢复
第十六章——备份与恢复 一.备份概述 1.Linux系统需要备份的数据: 2.备份策略: 二.dump和restore命令 1.dump命令: 2.restore命令:
- WPF中关于对前台Xaml中Triggers的一些重要思考。
今天在做一个小Demo的时候碰到了一个比较奇怪的问题,就是其中一个Trigger始终无法执行,<Trigger Property="Popup.IsOpen" Value=& ...
- git 提交顺序
0. git branch # 查看自己是哪个分支:先确定自己现在是哪个分支 1. git fetch # 将远程主机的更新,全部取回本地.如果只想取回特定分支的更新,可以指定分支名:git ...