A. Minimizing the String

很明显,贪心之比较从前往后第一个不一样的字符,所以可以从前往后考虑每一位,如果把它删除,他这一位就变成\(str[i + 1]\),所以只要\(str[i] > str[i + 1]\),删除后一定是最优的。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 200010;
int n;
char str[N];
int main(){
scanf("%d%s", &n, str + 1);
for(int i = 1; i < n; i++){
if(str[i] > str[i + 1]){
for(int j = 1; j <= n; j++)
if(j != i) printf("%c", str[j]);
return 0;
}
}
for(int i = 1; i < n; i++)
printf("%c", str[i]);
return 0;
}

B. Divisor Subtraction

单纯暴力可以会超时,考虑到所有偶数的最小质因子都是\(2\),故可以分类讨论,对于一个数\(x\):

  1. 若\(x \% 2 = 0\),则每次的\(d = 2\),操作次数为\(x / 2\)
  2. 若\(x\)为质数,最小质因子 $ = x\(,\)x - x = 0$,操作次数为\(1\)
  3. 若\(x\)为奇合数,则其的质因子必然也是奇数,这样\((x - d) \% 2 = 0\),可以返回第一步求解。

C. Meme Problem

按照题意,将式子化成一个一元二次方程,接着依照求根公式求解即可。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int d;
int main(){
int T; scanf("%d", &T);
while(T--){
scanf("%d", &d);
double a, b, delta;
delta = d * d - 4 * d;
if(delta < 0) puts("N");
else delta = sqrt(delta), printf("Y %.9f %.9f\n", (delta + d) / 2, (-delta + d) / 2);
}
return 0;
}

D. Edge Deletion

可以先用\(Dijkstra\)跑一棵最短路的树,然后贪心,从节点\(1\)开始\(Bfs\),选择\(K\)条边即可。

(PS:没开\(long\) \(long\)被卡\(Wa\)了\(N\)次)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, int> PLI; const int N = 300010, M = 300010;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m, k, numE = 1, head[N], fa[N], faEdge[N];
bool vis[N];
vector<int> G[N], ans;
LL dis[N];
priority_queue<PLI, vector<PLI>, greater<PLI> > q;
queue<int> Q;
struct Edge{
int next, to;
LL dis;
}e[M << 1];
void addEdge(int from, int to, int dis){
e[++numE].next = head[from];
e[numE].to = to;
e[numE].dis = dis;
head[from] = numE;
}
void Dijkstra(){
memset(dis, 0x3f, sizeof dis); q.push(make_pair(0, 1)); dis[1] = 0;
while(!q.empty()){
PLI u = q.top(); q.pop();
if(vis[u.second]) continue;
vis[u.second] = true;
for(int i = head[u.second]; i; i = e[i].next){
int v = e[i].to;
if(dis[u.second] + e[i].dis < dis[v]){
dis[v] = dis[u.second] + e[i].dis;
fa[v] = u.second, faEdge[v] = i >> 1;
q.push(make_pair(dis[v], v));
}
}
}
}
void Bfs(){ Q.push(1);
while((!Q.empty()) && k > 0){
int u = Q.front(); Q.pop();
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i]; k--;
ans.push_back(faEdge[v]);
Q.push(v);
if(!k) return;
}
}
}
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= m; i++){
int u, v, w; scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w); addEdge(v, u, w);
}
Dijkstra();
for(int i = 1; i <= n; i++)
if(fa[i]) G[fa[i]].push_back(i);
Bfs();
printf("%d\n", (int)ans.size());
for(int i = 0; i < ans.size(); i++)
printf("%d ", ans[i]);
return 0;
}

E. Vasya and a Tree

考虑用树状数组维护和,现在的修改类似于"树上扫描线"的东西,因为遍历到子树之前一定会dfs到它的祖先,所以在祖先的区间处理时,进入就加上,退出就剪掉...

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 3 * 1e5 + 5;
typedef long long ll;
struct Edge{
int next,to;
}e[N * 2];
int n,m,head[N],numE=0;
ll c[N],ans[N];
vector<pair<int,int> > T[N];
void addEdge(int from,int to){
e[++numE].next = head[from];
e[numE].to = to;
head[from] = numE;
}
ll ask(int x){
ll ans = 0;
for(;x;x-=x&-x)ans += c[x];
return ans;
}
void add(int x,ll k){
for(;x<=n;x+=x&-x)c[x] += k;
}
void dfs(int u,int last,int dep){
for(int i=0;i<T[u].size();i++){
int d = T[u][i].first;
ll x = T[u][i].second;
add(dep,x);
add(dep + d + 1,-x);
}
ans[u] = ask(dep);
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v == last)continue;
dfs(v,u,dep + 1);
}
for(int i=0;i<T[u].size();i++){
int d = T[u][i].first;
ll x = T[u][i].second;
add(dep,-x);
add(dep + d + 1,+x);
}
}
int main() {
scanf("%d",&n);
for(int i=2;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
addEdge(x,y);
addEdge(y,x);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
int v,d,x;
scanf("%d%d%d",&v,&d,&x);
T[v].push_back(make_pair(d,x));
}
dfs(1,0,1);
for(int i=1;i<=n;i++){
printf("%lld ",ans[i]);
}
return 0;
}

Codeforces Edu Round 54 A-E的更多相关文章

  1. Codeforces Beta Round #54 (Div. 2)

    Codeforces Beta Round #54 (Div. 2) http://codeforces.com/contest/58 A 找子序列 #include<bits/stdc++.h ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  6. CSA Round #54 $\ $Voting

    CSA Round #54 \(\ \)Voting 题目大意: 原题网址:戳我戳我! 一次歌唱比赛中,一位歌手刚刚结束表演,评委正在打分. 一共有 \(n\) 位评委,他们每人可以打 \(1\) 分 ...

  7. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  8. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. mysql 面试100 问(精华学习)。待开始理

    https://juejin.im/post/6850037271233331208 https://juejin.im/entry/6844903681091977229

  2. TCP特点

    1.基于字节流:面向连接:可靠传输:缓冲传输:全双工:流量控制.TCP如何保证可靠性:差错:校验和丢包:超时重传+确认失序:seq(序号)重复:seq(序号)1.数据被分割成TCP认为最合适发送的数据 ...

  3. 一文带你玩转对象存储COS文档预览

    随着"互联网+"的发展,各行各业纷纷"去纸化",商务合同.会议纪要.组织公文.商品图片.培训视频.学习课件.随堂讲义等电子文档无处不在.而要查看文档一般需要先下 ...

  4. CCF-201512-消除类游戏

    问题描述 试题编号: 201512-2 试题名称: 消除类游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游 ...

  5. Python面试题_中级版

    Python 面试题 1.Python是如何进行内存管理的 对象引用机制.垃圾回收机制.内存池机制 1.1对象引用机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都有引用计数. 引 ...

  6. appium-appium的等待时间

    #三种appium设置等待时间的方法 #第一种 sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time包后就可以使用 sleep()进行 ...

  7. MOOC JAVA笔记

    MOOC JAVA笔记 1.基础了解 JDK是开发人员安装的,它提供了开发java程序的必须工具 JRE是普通用户安装的,它提供了java的运行环境 JVM是java虚拟机运行程序的核心 2.程序的移 ...

  8. python-网络安全编程第八天(实战高精度密码字典生成器)

    前言 emmmm 高精度密码字典源码 1.py import exrex import sys #url过滤处理 def host_para(host): if '://' in host: host ...

  9. ci爬坑

    1.row_array() 问题描述:没有数据返回NULL,直接foreach,报错

  10. 面试官:连Spring AOP都说不明白,自己走还是我送你?

    前言 因为假期原因,有一段时间没给大家更新了!和大家说个事吧,放假的时候一位粉丝和我说了下自己的被虐经历,在假期前他去某互联网公司面试,结果直接被人家面试官Spring AOP三连问给问的一脸懵逼!其 ...