Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力
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 = 1e5 + , MAXM = 1e5 + , N = 1e5 + ;
const int MAXQ = ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
ll n, m, a, b;
cin >> n >> m >> a >> b;
if (n % m == )
{
cout << << endl;
return ;
}
ll now = n / m;
ll ans1 = (n - now * m) * b;
ll ans2 = ((now + ) * m - n) * a;
cout << min(ans1, ans2) << endl;
return ;
}
B
题目说了 在某个数之前K范围内的都能被消除掉 所以直接作一个前缀和即可
/*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 = 1e5 + , MAXM = 1e5 + , N = 2e5 + ;
const int MAXQ = ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}
int num[N];
int visit[];
priority_queue<int, vector<int>, greater<int> >que;
int main()
{
ios_base::sync_with_stdio();
cin.tie();
int n, k;
cin >> n >> k;
int anser = ;
for (int i = ; i <= n; i++)
{
cin >> num[i];
int l = max(num[i] - k, );
visit[l]++;
visit[num[i]]--;
}
for (int i = ; i <= ; i++)
{
visit[i] += visit[i - ];
}
for (int i = ; i <= n; i++)
{
if (!visit[num[i]])
{
anser++;
}
}
cout << anser << endl;
return ;
}
C
卡题意题..最后弱智答案没遍历完全FST了QAQ
/*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 = 1e5 + , MAXM = 1e5 + , N = 2e5 + ;
const int MAXQ = ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}
ll n, flag, cnt, pop;
ll anser = ;
string ch[];
ll number[];
ll ansl[];
ll ansr[];
void init()
{
pop = , cnt = , flag = ;
}
int main()
{
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
init();
cin >> ch[i];
for (int j = ; j < ch[i].size(); j++)
{
if (ch[i][j] == '(')
{
number[++pop] = ;
}
else
{
number[++pop] = -;
}
}
for (int i = ; i <= pop; i++)
{
cnt += number[i];
if (cnt < )
{
flag = ;
break;
}
}
if (!flag)
{
ansl[cnt]++;
}
flag = cnt = ;
for (int i = pop; i >= ; i--)
{
cnt -= number[i];
if (cnt < )
{
flag = ;
break;
}
}
if (!flag)
{
ansr[cnt]++;
}
}
for (int i = ; i <= ; i++)
{
anser += ansl[i] * ansr[i];
}
cout << anser << endl;
}
D
构造题
/*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 = 1e5 + , MAXM = 1e5 + , N = 2e5 + ;
const int MAXQ = ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}
int ans[][];
int main()
{
int n;
int a, b;
cin >> n >> a >> b;
if (min(a, b) != )
{
cout << "NO" << endl;
return ;
}
if ((n == && a == && b == ) || (n == && a == && b == ))
{
cout << "NO" << endl;
return ;
}
if (a > && b == )
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (i != j)
{
ans[i][j] = ;
}
}
}
for (int i = ; i <= a - ; i++)
{
for (int j = ; j <= n; j++)
{
ans[i][j] = ;
ans[j][i] = ;
}
}
cout << "YES" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
cout << ans[i][j];
}
cout << endl;
}
return ;
}
if (a == && b == )
{
for (int i = ; i <= n; i++)
{
ans[i][i + ] = ans[i + ][i] = ;
}
cout << "YES" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
cout << ans[i][j];
}
cout << endl;
}
return ;
}
if (a == && b > )
{
for (int i = ; i <= b - ; i++)
{
for (int j = ; j <= n; j++)
{
if (i != j)
{
ans[i][j] = ans[j][i] = ;
}
}
}
cout << "YES" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
cout << ans[i][j];
}
cout << endl;
}
return ;
}
}
E
直接把灯尽量往右放 贪心暴力即可 复杂度和调和级数有关 并不会炸
有些人暴力的姿势不对会FST
/*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 = 1e5 + , MAXM = 1e5 + , N = 2e6 + ;
const int MAXQ = ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
inline void addedge(int u, int v)
{
to[++tot] = v;
nxt[tot] = Head[u];
Head[u] = tot;
}
int n, m, k;
int visit[], has_block[], lighttt[];
ll anser;
int pop, summm, a1, a2;
void init()
{
summm = pop = ;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
anser = LLONG_MAX;
int n, m, k, now;
cin >> n >> m >> k;
n++;
for (int i = ; i <= m; i++)
{
cin >> now;
visit[now + ] = ;
}
if (visit[])
{
cout << - << endl;
return ;
}
for (int i = ; i <= k; i++)
{
cin >> lighttt[i];
}
for (int i = ; i <= n; i++)
{
if (visit[i])
{
has_block[i] = has_block[i - ];
}
else
{
has_block[i] = i;
}
}
for (int i = ; i <= k; i++)
{
init();
while ()
{
if (pop + i >= n)
{
ll cnt = 1LL*summm * lighttt[i];
anser = min(anser, cnt);
break;
}
if (pop == has_block[pop + i])
{
break;
}
summm++;
pop = has_block[pop + i];
}
}
if (anser == LLONG_MAX)
{
cout << - << endl;
}
else
{
cout << anser << endl;
}
}
F
首先很显然 因为传递不改变总值而要求是每个点的值都为0 所以当点的总值不为0时 是Impossible
然后我们要做的就是 DFS出一个生成树 然后从叶子到根传递出以一个顶点为根的子树其值的总值
因为每条边连接有两个顶点 这条边可以把这颗树分为两个子树 但题目要求是全部节点的值为0
所以最终要求是这两个子树的值也为0 则这条边传递的值 一定是 |其中一个子树的总值-0| 再判一下方向即可
其他没用到的边 答案是0 直接输出就行了
/*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 << ], nxt[MAXM << ], Head[MAXN], tot = ;
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 value[MAXN], visit[MAXN], ans[MAXM << ];
int dfs(int x)
{
int now = value[x];
visit[x] = ;
for (int i = Head[x]; i; i = nxt[i])
{
int v = to[i];
if (!visit[v])
{
int nownxt = dfs(v);
now += nownxt;
if (i & )
{
ans[i ^ ] = -nownxt;
}
else
{
ans[i] = nownxt;
}
}
}
return now;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie(); int sum = ;
int n, m, u, v;
read(n);
for (int i = ; i <= n; i++)
{
read(value[i]);
sum += value[i];
}
read(m);
for (int i = ; i <= m; i++)
{
read(u), read(v);
addedge(u, v), addedge(v, u);
}
if (sum != )
{
printf("Impossible\n");
return ;
}
printf("Possible\n");
dfs();
for (int i = ; i <= m; i++)
{
printf("%d\n", ans[i << ]);
}
return ;
}
G
也是个暴力题..把GCD搞出来 然后看连通块就行了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 222222
inline int read()
{
RG int x=,t=;RG char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')t=-,ch=getchar();
while(ch<=''&&ch>='')x=x*+ch-,ch=getchar();
return x*t;
}
struct Line{int v,next;}e[MAX<<];
int h[MAX],cnt=,mx;
inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}
vector<int> ys[MAX];
int n,a[MAX],tot;
bool book[MAX],vis[MAX];
ll ans[MAX];
void Div(int x,int id)
{
for(int i=,m=sqrt(x);i<=m;++i)
if(x%i==)
{
ys[i].push_back(id);
if(i*i!=x)ys[x/i].push_back(id);
}
}
void dfs(int u,int ff)
{
if(!book[u])return;
vis[u]=true;++tot;
for(int i=h[u];i;i=e[i].next)
if(e[i].v!=ff)dfs(e[i].v,u);
}
int main()
{
n=read();
for(int i=;i<=n;++i)mx=max(mx,a[i]=read());
for(int i=;i<=n;++i)Div(a[i],i);
for(int i=;i<n;++i)
{
int u=read(),v=read();
Add(u,v);Add(v,u);
}
for(int i=;i<=mx;++i)
{
for(int j=,l=ys[i].size();j<l;++j)
{
int v=ys[i][j];
vis[v]=false;book[v]=true;
}
for(int j=,l=ys[i].size();j<l;++j)
{
tot=;int v=ys[i][j];
if(!vis[v])dfs(v,);book[v]=false;
ans[i]+=1ll*tot*(tot-)/+tot;
}
}
for(int i=mx;i;--i)
if(ans[i])for(int j=i+i;j<=mx;j+=i)ans[i]-=ans[j];
for(int i=;i<=mx;++i)
if(ans[i])printf("%d %I64d\n",i,ans[i]);
return ;
}
Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力的更多相关文章
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- A. The Fault in Our Cubes 暴力dfs
http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...
- Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
随机推荐
- 左值引用&右值引用实践【TODO】
这篇文章写的很好,下半部分还未完全理解,后续还需要回头来看看20190706(): https://www.cnblogs.com/likaiming/p/9045642.html 简单实践如下: # ...
- rocketMQ 订阅关系
场景:2 个消费者进程中,创建了 2 个消费者,同属于 1 个消费组,但是订阅了不同的 topic,会因为订阅信息相互覆盖,导致拉不到消息. 原因是 rocketMQ 的订阅关系,是根据 group ...
- Android 调用相机、相册功能
清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述) private static final int REQUEST_CODE_ALBUM = 100 ...
- Kafka sender消息生产者
1.pom文件引入Kafka依赖(我用的版本是2.2.2.RELEASE) <dependency> <groupId>org.springframework.kafka< ...
- 【ABAP系列】SAP ABAP7.40新语法简介第二篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP7.40新语法简 ...
- cocos2dx基础篇(27) 屏幕适配
[3.x]https://blog.csdn.net/qq_40338728/article/details/82964046 [屏幕适配] 1.两个分辨率 1.1.窗口分辨率 在main.cpp中有 ...
- 【LeetCode】309、最佳买卖股票时机含冷冻期
Best Time to Buy and Sell Stock with Cooldown 题目等级:Medium 题目描述: Say you have an array for which the ...
- 【miscellaneous】北斗短报文
北斗系统最大的特色在于有源定位和短报文特色服务,不止解决了中国有无卫星导航系统的问题,还能将短信和导航结合,是中国北斗卫星导航系统的独特发明,也是一大优势. 北斗的短报文功能,在国防.民生和应急救援等 ...
- redis漏洞攻击
参考: https://www.cnblogs.com/kobexffx/p/11000337.html 利用redis漏洞获得root权限,挖矿. 解决方法: 用普通帐号启redis,用云的redi ...
- 洛谷 P1073 最优贸易 题解
题面 大家都是两遍SPFA吗?我这里就一遍dp啊: 首先判断对于一个点u,是否可以从一号点走到这里,并且可以从u走到n号点: 对于这样的点我们打上标记: 那么抛出水晶球的点一定是从打上标记的点中选出一 ...