Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径
A
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }};
const int mod = 1e9 + , gakki = + + + + 1e9;
const int MAXN = 2e5 + , MAXM = 2e5 + , N = 2e5 + ;
const int MAXQ = ;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}*/
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
int num[];
int anser[];
map<string, int> mp;
int main()
{
mp["S"] = , mp["M"] = , mp["L"] = ;
mp["XL"] = , mp["XXL"] = , mp["XXXL"] = ;
mp["XS"] = , mp["XXS"] = , mp["XXXS"] = ;
int n;
int ans = ;
read(n);
string now;
for (int i = ; i <= n; i++)
{
cin >> now;
num[mp[now]]++;
}
for (int i = ; i <= n; i++)
{
cin >> now;
anser[mp[now]]++;
}
for (int i = ; i <= ; i++)
{
ans += abs(num[i] - anser[i]);
}
cout << ans / << endl;
return ;
}
B
分奇偶即可
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }};
const int mod = 1e9 + , gakki = + + + + 1e9;
const int MAXN = 2e5 + , MAXM = 2e5 + , N = 2e5 + ;
const int MAXQ = ;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}*/
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
int num[];
int anser = ;
int number[];
int main()
{
ios_base::sync_with_stdio();
cin.tie(); int n, m;
cin >> n >> m;
for (int i = ; i <= n; i++)
{
cin >> num[i];
}
n++;
num[n] = m;
for (int i = ; i <= n; i++)
{
number[i] = number[i - ];
if (i & )
{
int add = num[i] - num[i - ];
number[i] += add;
}
}
anser = number[n];
for (int i = ; i <= n - ; i++)
{
if (num[i] - > num[i - ] || num[i] + < num[i + ])
{
if (i & )
{
int now = number[i] + m - num[i] + number[i] - number[n] - ;
anser = max(anser, now);
}
}
}
cout << anser << endl;
return ;
}
C
离散化前缀和
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }};
const int mod = 1e9 + , gakki = + + + + 1e9;
const int MAXN = 2e5 + , MAXM = 2e5 + , N = 2e5 + ;
const int MAXQ = ;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}*/
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
ll number[];
ll ans[];
ll l[], r[];
ll anser[];
map<ll, ll> mp;
map<ll, ll> mpb;
int main()
{
ios_base::sync_with_stdio();
cin.tie(); int n;
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> l[i] >> r[i];
number[ * i - ] = l[i];
number[ * i - ] = r[i];
number[ * i] = r[i] + ;
}
sort(number + , number + * n + );
int pop = unique(number + , number + * n + ) - number - ;
// for (int i = 1; i <= pop; i++)
// {
// cout << number[i] << " ";
// }
// cout << endl;
for (int i = ; i <= pop; i++)
{
mp[number[i]] = i;
mpb[i] = number[i];
}
for (int i = ; i <= n; i++)
{
ans[mp[l[i]]]++;
ans[mp[r[i]] + ]--;
}
for (int i = ; i <= pop; i++)
{
ans[i] += ans[i - ];
}
ll cur = ans[];
for (int i = ; i <= pop - ; i++)
{
cur = ans[i];
anser[cur] += mpb[i + ] - mpb[i];
}
cur = ans[pop];
anser[cur]++;
for (int i = ; i <= n; i++)
{
cout << anser[i] << " ";
}
cout << endl;
return ;
}
D
倒着DP
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }};
const int mod = , gakki = + + + + 1e9;
const int MAXN = 2e5 + , MAXM = 2e5 + , N = 2e5 + ;
const int MAXQ = ;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}*/
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
ll zuhe[][];
int n;
ll num[];
ll ans[];
ll anser = ;
void init()
{
zuhe[][] = ;
for (int i = ; i <= n; i++)
{
zuhe[i][] = ;
for (int j = ; j <= i; j++)
{
zuhe[i][j] = (zuhe[i - ][j] + zuhe[i - ][j - ]) % mod;
}
}
}
int main()
{
ios_base::sync_with_stdio();
cin.tie(); cin >> n;
ans[n + ] = ;
init();
for (int i = ; i <= n; i++)
{
cin >> num[i];
}
for (int i = n; i >= ; i--)
{
if (num[i] <= )
{
continue;
}
for (int j = i + num[i] + ; j <= n + ; j++)
{
ans[i] += zuhe[j - i - ][num[i]] * ans[j];
ans[i] %= mod;
}
}
for (int i = ; i <= n; i++)
{
anser += 1LL * ans[i];
anser %= mod;
}
cout << anser << endl;
return ;
}
E
边双联通分量缩点后是一颗树 再求树的直径即可
/*Huyyt*/
#include <bits/stdc++.h>
#include <vector>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = + ;
const int M = + ;
struct EDGE
{
int v, next;
} edge[M * ];
int first[N], low[N], dfn[N], belong[N], degree[N], sta[M], instack[M];
vector<int> gra[];
int g, cnt, top, scc;
int min(int a, int b)
{
return a < b ? a : b;
}
void AddEdge(int u, int v)
{
edge[g].v = v;
edge[g].next = first[u];
first[u] = g++;
}
void Tarjan(int u, int fa)
{
int i, v;
low[u] = dfn[u] = ++cnt;
sta[++top] = u;
instack[u] = ;
for (i = first[u]; i != -; i = edge[i].next)
{
v = edge[i].v;
if (i == (fa ^ ))
{
continue;
}
if (!dfn[v])
{
Tarjan(v, i);
low[u] = min(low[u], low[v]);
}
else if (instack[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u])
{
scc++;
while ()
{
v = sta[top--];
instack[v] = ;
belong[v] = scc;
if (v == u)
{
break;
}
}
}
}
map<pair<int, int>, int> mp;
int ssssstart, eeeeeeend;
int distence[];
int visit[];
void getdis(int x)
{
visit[x] = ;
int len = gra[x].size();
for (int i = ; i < len; i++)
{
int to = gra[x][i];
if (visit[to])
{
continue;
}
distence[to] = distence[x] + ;
getdis(to);
}
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
memset(first, -, sizeof(first));
int n, m;
int u, v;
cin >> n >> m;
for (int i = ; i <= m; i++)
{
cin >> u >> v;
AddEdge(u, v);
AddEdge(v, u);
}
for (int i = ; i <= n; i++)
{
if (!dfn[i])
{
Tarjan(i, -);
}
}
for (int i = ; i <= n; i++)
{
for (int j = first[i]; j != -; j = edge[j].next)
{
v = edge[j].v;
if (belong[i] != belong[v])
{
if (!mp[make_pair(belong[i], belong[v])])
{
gra[belong[i]].push_back(belong[v]);
gra[belong[v]].push_back(belong[i]);
mp[make_pair(belong[i], belong[v])] = mp[make_pair(belong[v], belong[i])] = ;
}
}
}
}
getdis();
int maxnn = -;
for (int i = ; i <= scc; i++)
{
if (distence[i] > maxnn)
{
ssssstart = i;
maxnn = distence[i];
}
}
mem(visit, ), mem(distence, );
getdis(ssssstart);
maxnn = -;
for (int i = ; i <= scc; i++)
{
if (distence[i] > maxnn)
{
eeeeeeend = i;
maxnn = distence[i];
}
}
cout << distence[eeeeeeend] << endl;
return ;
}
Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径的更多相关文章
- hdu 4612 双联通缩点+树形dp
#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...
- hdu 2242双联通分量+树形dp
/*先求出双联通缩点,然后进行树形dp*/ #include<stdio.h> #include<string.h> #include<math.h> #defin ...
- Codeforces.264E.Roadside Trees(线段树 DP LIS)
题目链接 \(Description\) \(Solution\) 还是看代码好理解吧. 为了方便,我们将x坐标左右反转,再将所有高度取反,这样依然是维护从左到右的LIS,但是每次是在右边删除元素. ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- 【2019.8.6 慈溪模拟赛 T3】集合(set)(线段树上DP)
线段树上\(DP\) 首先发现,每个数肯定是向自己的前驱或后继连边的. 则我们开一棵权值线段树,其中每一个节点记录一个\(f_{0/1,0/1}\),表示在这个区间左.右端点是否连过边的情况下,使这个 ...
随机推荐
- 用户及用户组管理(week1_day4)
本节内容 useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最 ...
- 5分钟快速安装Redmine项目管理软件
公司还在使用Excel.project.word来管理项目吗?时间一长.项目参与的人多.就出现了断断续续无法连续跟踪的问题.终于忍受不了公司这种陈旧的项目管理手段了,于是花了一些时间研究了市面上常见的 ...
- 2019年icpc区域赛银川站总结
目录 一.前言 二.10月19日热身赛 三.10月20日正式赛 四.结果 一.前言 比赛前我们队有ccpc厦门和icpc银川的名额,然而这两个地区的时间正好撞了,考虑到银川更容易拿奖,加上我们ACM协 ...
- bert系列一:《Attention is all you need》论文解读
论文创新点: 多头注意力 transformer模型 Transformer模型 上图为模型结构,左边为encoder,右边为decoder,各有N=6个相同的堆叠. encoder 先对inputs ...
- firefox PAC代理
在linux用firefox,又不像windows下的chrome那样,可以直接设置个自动代理,但是发现firefox有个自动代理的功能哦. /home/allen/Documents/google. ...
- 【Python开发】使用pyplot模块绘图
快速绘图 使用pyplot模块绘图¶ matplotlib的pyplot模块提供了和MATLAB类似的绘图API,方便用户快速绘制二维图表.我们先看一个简单的例子: 05-matplotlib/mat ...
- 【图像-视频处理】YUV420、YV12与RGB24的转换公式
bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) { if (width ...
- Java十大bug之——包冲突
找bug就像破案,有的bug简单,有的bug复杂,还有的bug隐藏的令人难以发现. 一个逻辑上看起来一切都正常,结果确有问题,且怎么分析都感觉自己写的没问题的情况——包冲突 遇到这个bug最开始没有任 ...
- [转帖]深度: NVMe SSD存储性能有哪些影响因素?
深度: NVMe SSD存储性能有哪些影响因素? http://www.itpub.net/2019/07/17/2434/ 之前有一个误解 不明白NVME 到底如何在队列深度大的情况下来提高性能, ...
- 尝试自己搭一个简单的typescript运行环境
开发typescript项目有一些现成的脚手架,比如:typescript-library-starter,它的配置齐全,更适合用在实际项目开发上.其实在学习阶段可以自己搭建一个简单的typescri ...