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. Python-collections模块-57

    返回顶部 模块的导入和使用 模块的导入应该在程序开始的地方 更多相关内容 http://www.cnblogs.com/Eva-J/articles/7292109.html   常用模块 colle ...

  2. 【RSYSLOG】The Property Replacer【转】

    最近在调整日志平台的日志格式,一下是RSYSLOG的 Property Replacer 说明.鉴于RSYSLOG官网略坑,转发一下,原地址忘记了- - ||| The property replac ...

  3. Linux下设置MySql自动启动

    https://www.cnblogs.com/sunny3096/p/7954146.html

  4. 双击启动tomcat中的startup.bat闪退原因及解决方法

    免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动. 原因是:在启动tomcat是,需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了t ...

  5. PreparedStatement 与 Statement 的区别

    1. PreparedStatement 接口继承 Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作为 ...

  6. vue图表

    https://www.cnblogs.com/powertoolsteam/p/top-9-javascript-charting-libraries.html

  7. Day 4-4 shutil模块

    常用方法: import shutil f = open("conf.ini", "r") f1 = open("shutil.ini", ...

  8. 解决tab标签页,相同id时切换失灵的问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. python爬虫之MongoDB测试环境安装

    一.   下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二.  安装 1>设置mongoDB ...

  10. spring 标签

    */ @Slf4j @Service public class RetryService { @Autowired private MqConfig mqConfig; /** * 如果网络连接失败, ...