https://www.cnblogs.com/31415926535x/p/11601964.html

这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补。。。

A. Creating a Character

贪心加一堆判断就行了,,,

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 15e4 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; int a[maxn], n; int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); int t; cin >> t;
while(t--)
{
ll s, i, e;
cin >> s >> i >> e;
if(s + e <= i)
{
cout << 0 << endl;
continue;
}
ll x = i - s + e;
x = x / 2;
if(s + x <= i + e - x)++x;
if(e == 0 && s > i)x = 0;
else if(e == 0 && s <= i)x = 1;
if(x <= 0)x = 0;
cout << e - x + 1 << endl;
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

B. Zmei Gorynich

贪心++

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 15e4 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; int a[maxn], n; int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); int t; cin >> t;
while(t--)
{
ll n, x; cin >> n >> x;
ll mx = -inf, mxd = 0;
ll d, h;
for(int i = 1; i <= n; ++i)
{
cin >> d >> h;
mx = max(mx, d - h);
mxd = max(mxd, d);
}
if(mx <= 0 && mxd < x)cout << -1 << endl;
else
{
ll ans = (x - mxd + mx - 1) / mx;
++ans;
if(mxd >= x)ans = 1;
cout << ans << endl;
}
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

C. The Number Of Good Substrings

貌似满足条件的串不多???

直接枚举每一个1的位置,,然后对于以他为最高位的串表示的十进制如果小于串的长度以及他前面的前导零长度的和就是一个满足条件的,,这样跑一遍就行了,,,

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; char s[maxn]; int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); int t; cin >> t;
while(t--)
{
cin >> s;
ll ans = 0;
int lst = -1;
int len = strlen(s);
for(int i = 0; i <= len - 1; ++i)
{
if(s[i] == '0')continue;
else
{
ll base = 1;
++ans;
for(int j = i + 1; j <= len - 1; ++j)
{
base <<= 1;
if(s[j] == '1')base |= 1;
if(j - lst >= base)++ans;
else break;
}
lst = i;
}
}
cout << ans << endl;
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}
// 010010001000

D. Coloring Edges

感觉这题很不错,,有向图判环之前只知道用拓扑排序,,现在才知道有好几种方法,,,

题意是给一张图,然后对边染色,用最少的颜色染出的图中相同颜色的边没有成环就行

显然没有环的时候答案就是1,,,有环的时候答案就是2,,

所以可以先判环,,然后染色

这样做的话染色的一个技巧就是对于 u->v 边, \(u \ge v\) 直接染2,,其他的染1

dfs判环

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; int n, m;
struct edge
{
int to, nxt, col;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
tot = 0;
memset(head, -1, sizeof head);
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].nxt = head[u];
edge[tot].col = 0;
head[u] = tot++;
}
bool vis[maxn];
bool dfs(int u, int s)
{
for(int i = head[u]; ~i; i = edge[i].nxt)
{
int v = edge[i].to;
if(v == s)return true;
if(vis[v])continue;
vis[v] = true;
if(dfs(v, s))return true;
}
return false;
} int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); cin >> n >> m;
int u, v;
init();
for(int i = 1; i <= m; ++i)
{
cin >> u >> v;
addedge(u, v);
}
bool flag = false;
for(int i = 1; i <= n; ++i)
{
memset(vis, false, sizeof vis);
vis[i] = true;
flag = dfs(i, i);
if(flag)break;
}
if(!flag)
{
cout << 1 << endl;
for(int i = 1; i <= m; ++i)cout << 1 << " ";
cout << endl;
}
else
{
cout << 2 << endl;
for(int i = 1; i <= n; ++i)
for(int j = head[i]; ~j; j = edge[j].nxt)
if(i > edge[j].to)edge[j].col = 2;
else edge[j].col = 1;
for(int i = 0; i <= tot - 1; ++i)
cout << edge[i].col << " ";
cout << endl;
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

topo排序判环

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; int n, m;
struct edge
{
int to, nxt, col;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
tot = 0;
memset(head, -1, sizeof head);
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].nxt = head[u];
edge[tot].col = 0;
head[u] = tot++;
}
int du[maxn];
bool topo()
{
int cnt = 0;
queue<int> q;
while(!q.empty())q.pop();
for(int i = 1; i <= n; ++i)
if(!du[i])
q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop();
++cnt;
for(int i = head[u]; ~i; i = edge[i].nxt)
if(--du[edge[i].to] == 0)
q.push(edge[i].to);
}
return cnt == n;
} int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); cin >> n >> m;
int u, v;
init();
memset(du, 0, sizeof du);
for(int i = 1; i <= m; ++i)
{
cin >> u >> v;
++du[v];
addedge(u, v);
}
if(topo())
{
cout << 1 << endl;
for(int i = 1; i <= m; ++i)cout << 1 << " ";
cout << endl;
}
else
{
cout << 2 << endl;
for(int i = 1; i <= n; ++i)
for(int j = head[i]; ~j; j = edge[j].nxt)
if(i > edge[j].to)edge[j].col = 2;
else edge[j].col = 1;
for(int i = 0; i <= tot - 1; ++i)
cout << edge[i].col << " ";
cout << endl;
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

dfs染回边

另一种做法需要知道dfs的一些性质:

dfs跑图会产生四种边,,(算法导论上有(看过都忘了,,,)这些是参考这个的

  • 树边(Tree Edge) : 就是 u->v v是第一次访问的边
  • 前向边(Forward Edge) : 就是 u->v v是访问过的,并且不是v的直接的孩子
  • 回边(Back Edge) : 就是 u->v v是指向他的一个祖先的边,,(显然这样的边可能是环的一部分
  • 跨越边(Cross Edge) : 就是 u->v v是指向一个访问过的点,但 u , v 之间没关系,,(可能是两棵子树中的点等等

所以对于这题,,我们只要跑一边dfs,,然后将所有的回边染2,,其他的边染1即可,,,这样子就不用判环什么的,,,

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 4e5 + 233;
const int mod = 1e9 + 7; int n, m;
struct edge
{
int to, nxt, col;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
tot = 0;
memset(head, -1, sizeof head);
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].nxt = head[u];
edge[tot].col = 0;
head[u] = tot++;
}
bool flag;
int vis[maxn];
void dfs(int u)
{
// 先将子树标记为1
// 如果子树中有到子树中的某个点时,表示有环
// 最后将子树标记为2 // 对于染色,树边染1(vis[v] == 0)、回边(vis[v] == 1)染2,前边(就是连到其他树的边)和跨越边(连着已经走过的点的边)染1
vis[u] = 1;
for(int i = head[u]; ~i; i = edge[i].nxt)
{
int v = edge[i].to;
if(vis[v] == 0)
{
dfs(v);
edge[i].col = 1;
}
else if(vis[v] == 1)
{
flag = true;
edge[i].col = 2;
}
else
edge[i].col = 1;
}
vis[u] = 2;
} int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); cin >> n >> m;
int u, v;
init();
for(int i = 1; i <= m; ++i)
{
cin >> u >> v;
addedge(u, v);
}
flag = false;
memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; ++i)
if(vis[i] == 0)
dfs(i);
cout << (flag ? 2 : 1) << endl;
for(int i = 0; i <= tot - 1; ++i)
cout << edge[i].col << " ";
cout << endl; // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

(end)

Educational Codeforces Round 72 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  2. 拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D

    https://codeforces.com/contest/1217 D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色 不成环的边全部用1染色 ps:最后输出需要注意, ...

  3. Educational Codeforces Round 72 (Rated for Div. 2) C题

    C. The Number Of Good Substrings Problem Description: You are given a binary string s (recall that a ...

  4. Educational Codeforces Round 72 (Rated for Div. 2) B题

    Problem Description: You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a ...

  5. Educational Codeforces Round 72 (Rated for Div. 2) A题

    Problem Description: You play your favourite game yet another time. You chose the character you didn ...

  6. Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)

    题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...

  7. Educational Codeforces Round 72 (Rated for Div. 2) Solution

    传送门 A. Creating a Character 设读入的数据分别为 $a,b,c$ 对于一种合法的分配,设分了 $x$ 给 $a$ 那么有 $a+x>b+(c-x)$,整理得到 $x&g ...

  8. Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...

  9. Educational Codeforces Round 72 (Rated for Div. 2)C(暴力)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[200007];int a[20 ...

随机推荐

  1. node实现后台权限管理系统

    本文面向的是node初学者,目标是搭建一个基础的后台权限系统.使用的node框架是上手最简单的express,模板是ejs,这些在node入门的书籍中都有介绍说明,所以应该是难度较低的. 对于node ...

  2. Nginx总结(二)基于ip的虚拟主机配置

    前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要说的 ...

  3. HBase的安装和使用

    文章作者:foochane  原文链接:https://foochane.cn/article/2019062801.html 1 Hbase基本介绍 Hbase是一个分布式数据库,可以提供数据的实时 ...

  4. CentOS -- RocketMQ HA & Monitoring

    RocketMQ Architecture NameServer Cluster Name Servers provide lightweight service discovery and rout ...

  5. 设计模式(C#)——02简单工厂模式

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来.通俗来说,你只关心怎么用,不用关心怎么做 ...

  6. Egret白鹭开发小游戏之自定义load加载界面

    刚接触不久就遇到困难------自定义loading.想和其他获取图片方式一样获取加载界面的图片,结果发现资源还没加载就需要图片,在网上百度了许多,都没有找到正确的方式,通过自己的摸索,终于,,,我成 ...

  7. 在SpringMVC中,jsp和后台互相传值

    如题,这个是以前做的笔记,现在搬到博客上...... package com.ruide.action; ​ import java.util.HashMap; import java.util.Ma ...

  8. odoo12从零开始:一、安装odoo运行环境(mac)

    写在前面: 接触odoo已经两年多了,在大学做课程设计的时候,无意间了解到odoo这个erp框架,当时的odoo在国内还默默无闻,我也不曾想过自己毕业后会从事到odoo框架的相关开发工作中来.两年多的 ...

  9. CodeForces 639C Bear and Polynomials

    Bear and Polynomials 题解: 如果改变一个其中的一个数,那么需要知道的是,前面的数都可以进到当前位来,如果过不来的话,那么就会因为前面有数导致无法变成0. 所以我们将前面的数不断向 ...

  10. 牛客小白月赛4 C 病菌感染 dfs

    链接:https://www.nowcoder.com/acm/contest/134/C来源:牛客网 题目描述 铁子和顺溜上生物课的时候不小心将几滴超级病菌滴到了培养皿上,这可急坏了他们. 培养皿可 ...