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. Best Time to Buy and Sell Stock I II III IV

    一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...

  2. Python生成csv中文乱码解决办法

    前言 在Linux下面用python进行数据处理,然后输出为csv格式,如果没有中文一切正常,但是如果有中文,就会出现乱码的问题,本篇将讲述怎么处理这个问题 处理过程 原始代码 #!/usr/bin/ ...

  3. Matlab项目经验分享-去除震荡点

    Matlab是做科研是比较常用的建模工具,我在研一做项目期间遇到了一个还算比较基础的问题,所以我打算记录下来并分享出来! 处理问题步骤: 1. 抛出问题 2. 思考解决方法 3. 代码验证看结果 抛出 ...

  4. Hadoop大数据平台之Zookeeper搭建

    环境:CentOS 7.4 (1708  DVD) 工具:MobaXterm 1. 使用xftp将hadoop上传到/usr/local目录下,将其解压并重命名. 2. 修改环境变量并source. ...

  5. 下载器Folx扩展程序支持哪些浏览器

    Folx使用多线程的下载方式大大提升了下载的速度,可以完全替代浏览器自带的下载工具,使下载文件的管理更加简单高效.但是,必须给浏览器安装Folx扩展程序,才能使用Folx下载页面链接. Folx在偏好 ...

  6. Jmeter生成HTML测试报告

    jmeter轻便小巧,运行速度快,但是缺少直观的可视化测试报告,并且生成测试报告操作稍微有点麻烦. GUI界面没有生成测试报告的功能,只能使用命令行生成测试报告.这里需要提到一个jtl的文件,它是生成 ...

  7. ubuntu解决安装速度问题

    速度慢得原因:linux系统很多的软件源链接都是在国外服务器上,由于国内防火墙影响导致下载速度极慢,甚至超时. 解决办法一:购买梯子 这样你就可以快速的下载国外服务器的软件包了,但是你得有个可靠得梯子 ...

  8. Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)

    题目链接 题目大意 给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q ...

  9. IDEA连接码云

    IDEA连接码云: 1.安装Gitee插件 2.下载git.exe https://git-scm.com/download/win 3.安装git 如果是Win10专业版,可能会出错,GitBash ...

  10. java学生简单管理系统

    1 //设一个班有n名学生,期末考试5门,编写程序评定学生奖学金 2 514 //要求打印输出一二等奖学金学生的学号,姓名和各科成绩 3 515 //总成绩超过全班总平均成绩20%一等奖学金,超过总平 ...