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. Ceph中的Copyset概念和使用方法

    前言 copyset运用好能带来什么好处 降低故障情况下的数据丢失概率(增加可用性) 降低资源占用,从而降低负载 copyset的概念 首先我们要理解copyset的概念,用通俗的话说就是,包含一个数 ...

  2. Git 分支相关

    创建分支 git branch dev 切换分支 git checkout dev (dev换成-可切换为上一个使用的分支) 以上两个可合并为 git checkout -b dev 将新分支推送到远 ...

  3. PDF技术 -Java实现Html转PDF文件

    转载:https://blog.csdn.net/qq_34190023/article/details/82999702 html转换为pdf的关键技术是如何处理网页中复杂的css样式.以及中文乱码 ...

  4. 2、Spring Boot配置

    1.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...

  5. hive显示列名

    查询时显示列名:hive> set hive.cli.print.header;hive.cli.print.header=falsehive> set hive.cli.print.he ...

  6. pandas 生成html文档,支持可添加多个表

    如何通过pandas生成html格式?如何通过pandas生成html文件文件中包含多个表单Balance_64_data = pd.read_sql(Balance_64_sql,engine)df ...

  7. 20-SAP PI开发手册-ERP发布服务供外部系统调用(sproxy代理类)

    一.      接口内容 接口详细信息 1.  字段对应关系 发送字段对应关系 返回字段对应关系 2.  报文信息 传入报文(报文结构,外围系统提供) 1 <?xml version=" ...

  8. Mac电脑数据被误删了怎么办,还能恢复吗

    随着苹果产品的使用率越来越高,苹果电脑视频丢失的风险也是居高不下,大部分情况下都是由于误操作或者是中病毒导致视频丢失,苹果电脑视频恢复可以实现吗?涉及到文件恢复的问题,找EasyRecovery文件恢 ...

  9. iOS gif图显示问题

    问题 有时候需要显示gif动态图,让界面更加的绚丽,但是iOS默认只支持png,gpg图片.那么如何才能显示gif图呢? 解决方式 添加框架 CoreGraphics.framework ImageI ...

  10. CentOS下Python尝试

    打印一个爱心 #猴赛雷 print'\n'.join([''.join([('AndyLove'[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2 ...