当前位置:网站首页>Niuke Practice 101
Niuke Practice 101
2022-07-02 15:02:00 【Not Zhang Pangpang】
List of articles
C Reasoning clown
The question :
The length is n ( 1 ≤ n ≤ 1 0 5 ) n(1\leq n\leq 10^5) n(1≤n≤105) Sequence a ( 0 ≤ a i ≤ 2 31 ) a(0\le a_i\le 2^{31}) a(0≤ai≤231), Satisfy a i < a i + 1 a_i<a_{i+1} ai<ai+1
Find the smallest x x x, Satisfy a i & x < a i + 1 & x a_i\&x<a_{i+1}\&x ai&x<ai+1&x
Ideas :
For the answer x x x, Greedy from high to low to judge whether everyone can be 0 0 0
Typical wrong thinking : If you will x x x Someone becomes 0 0 0 It was found that the monotonicity requirements could not be met , Directly think x x x This bit cannot be 0 0 0, Time complexity O ( n log n ) O(n\log n) O(nlogn)
In fact, it is judged that i i i Whether a is 0 0 0 when , It should be used extra O ( n log n ) O(n\log n) O(nlogn) Time to judge the rest i − 1 i-1 i−1 Bits and already handled 31 − i 31-i 31−i Can bits satisfy monotony , If yes, it is 0 0 0.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
ll a[maxn];int n;
bool check(ll p,int num)
{
for(int i=num-1;i>=0;i--)
{
p^=(1ll<<i);
int ok=1;
for(int j=1;j<n;j++) {
if((a[j]&p)>(a[j+1]&p))
{
ok=0;break;
}
}
if(ok) continue;
p^=(1ll<<i);
}
for(int i=1;i<n;i++)
{
if((a[i]&p)>=(a[i+1]&p))
{
return false;
}
}
return true;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
ll ans=0;
for(int i=31;i>=0;i--)
{
ll p=ans;
if(check(p,i)) continue;
ans|=(1ll<<i);
}
printf("%lld\n",ans);
}
D Capital of crime
The question :
n ( 1 ≤ n ≤ 1 0 5 ) n(1\le n \le 10^5) n(1≤n≤105) A little bit m m m Undirected connected graph of strip edge , Give directions to the edges on the graph , Make each point from i i i Start at least move f i f_i fi Time
Ideas :
If m > n − 1 m>n-1 m>n−1 Then there must be a ring , Just connect on the ring , The rest points to the ring , There are unlimited ways to move
Otherwise it will be a tree , Consider the tree shape d p dp dp, d p i ≥ 0 dp_i\ge 0 dpi≥0 Said to i i i Under the subtree that is the root, it meets the requirements , And from i i i Start and walk down d p i dp_i dpi Step , if d p i < 0 dp_i<0 dpi<0, Represents that the requirements cannot be met in the subtree , At least move up ∣ d p i ∣ |dp_i| ∣dpi∣ Step can meet the requirements
If the root of the whole tree d p 1 < 0 dp_1<0 dp1<0 Represents no solution
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#define fi first
#define gcd __gcd
#define se second
#define pb push_back
#define lowbit(x) x&-x
#define inf 0x3f3f3f3f
#define mem(x,y) memset(x,y,sizeof(x))
#define lrb666 ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<int,int> PII;
const int maxn=1e5+7;
int num,head[maxn],n,m,f[maxn],g[maxn],vis[maxn],c[maxn];
struct road{
int b,c,nex;}r[4*maxn];
void add(int a,int b,int c){
r[num].b=b;r[num].c=c;r[num].nex=head[a];head[a]=num++;}
void dfs1(int u,int fa)
{
g[u]=-f[u];
for(int i=head[u];~i;i=r[i].nex)
{
int v=r[i].b;
if(v==fa) continue;
dfs1(v,u);
if(g[v]<0)
{
r[i].c=1;
if(-g[v]-1>g[u]) g[u]=min(g[u],g[v]+1);
}
else
{
r[i^1].c=1;
if(g[u]+1+g[v]>=0) g[u]=max(g[u],g[v]+1);
}
}
}
int dfs2(int u,int fa)
{
if(vis[u]) return u;vis[u]=1;
for(int i=head[u];~i;i=r[i].nex)
{
int v=r[i].b;
if(v==fa) continue;
int p=dfs2(v,u);
if(p==0) continue;
c[u]=1;r[i^1].c=1;
if(p==u) return -1;
return p;
}
return 0;
}
void dfs3(int u,int fa)
{
if(vis[u]) return; vis[u]=1;
for(int i=head[u];~i;i=r[i].nex)
{
int v=r[i].b;
if(v==fa || c[v]) continue;
r[i].c=1;
dfs3(v,u);
}
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&f[i]);
for(int i=1;i<=m;i++)
{
int a,b;scanf("%d%d",&a,&b);add(a,b,0);add(b,a,0);
}
if(m==n-1)
{
dfs1(1,-1);
if(g[1]<0)
{
puts("-1");return 0;
}
}
else
{
dfs2(1,-1);
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
if(c[i]) dfs3(i,-1);
}
}
for(int i=0;i<2*m;i+=2)
{
printf("%d",r[i].c);
}
}
E There is no water in the city
The question :
Flood from 1 1 1 set out , At time t t t The height reached is n n n, stay n ( 1 ≤ n ≤ 3000 ) n(1\le n\le3000) n(1≤n≤3000) A little bit m ( 1 ≤ m ≤ 2 ∗ 1 0 4 ) m(1\le m\le2*10^4) m(1≤m≤2∗104) In a graph with edges , Each two-way side has a height , When the flood reaches any city on both sides , And the height is greater than the edge , Will inundate another city , The flood height will stop before reaching the height that just submerges all cities , Now you can operate any edge once to increase its height by one , Ask how many times you can operate at least without flooding n n n spot
Ideas :
Build a tree through the minimum spanning tree , We can know the minimum height of flooding the whole map , Then just put all 1 1 1 To m m m The minimum edge on the path can be raised , The minimum cut can be achieved
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e4+7;
int fa[maxn],num,head[maxn],depth[maxn],now[maxn],s,t;
struct road{
int b,c,nex;}r[200005];
void add(int a,int b,int c){
r[num].b=b;r[num].c=c;r[num].nex=head[a];head[a]=num++;}
struct node
{
int a,b,c;
}e[maxn];
bool cmp(node a,node b)
{
return a.c<b.c;
}
int find(int x)
{
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
}
void lianjie(int x,int y)
{
int px=find(x),py=find(y);
fa[px]=py;
}
int make_level()
{
memset(depth,-1,sizeof(depth));
queue<int>q;
q.push(s);
depth[s]=1;
now[s]=head[s];
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];~i;i=r[i].nex)
{
int v=r[i].b;
if(depth[v]!=-1 || r[i].c<=0) continue;
now[v]=head[v];
depth[v]=depth[u]+1;
q.push(v);
}
}
return depth[t]!=-1;
}
int dinic(int u,int flow)
{
if(u==t) return flow;
int sum=0;
for(int i=now[u];~i;i=r[i].nex)
{
now[u]=i;
int v=r[i].b;
if(depth[v]!=depth[u]+1 || r[i].c<=0) continue;
int use=dinic(v,min(flow-sum,r[i].c));
if(use)
{
r[i].c-=use;
r[i^1].c+=use;
sum+=use;
}
if(sum==flow) return flow;
}
if(sum==0) depth[u]=-1;
return sum;
}
signed main()
{
memset(head,-1,sizeof(head));
int n,m;scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++) scanf("%lld%lld%lld",&e[i].a,&e[i].b,&e[i].c);
sort(e+1,e+1+m,cmp);
int mx=0;
for(int i=1;i<=m;i++)
{
if(find(e[i].a)==find(e[i].b)) continue;
mx=e[i].c;lianjie(e[i].a,e[i].b);
}
for(int i=1;i<=m;i++)
{
if(e[i].c>=mx) continue;
add(e[i].a,e[i].b,mx-e[i].c);add(e[i].b,e[i].a,mx-e[i].c);
}
s=1,t=n;
int ans=0;
while(make_level()) ans+=dinic(s,1e9);
printf("%lld\n",ans);
}
边栏推荐
- 微信小程序使用towxml显示公式
- LeetCode 209. Minimum length subarray
- LeetCode 2320. 统计放置房子的方式数
- LeetCode_字符串_简单_412.Fizz Buzz
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- 天猫商品详情接口(APP,H5端)
- OpenCV调用USB摄像头的点滴
- C#延时、在线程中开启定时器、获取系统时间
- 可视化搭建页面工具的前世今生
- Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
猜你喜欢

Implement a server with multi process concurrency

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

天猫商品详情接口(APP,H5端)

STM32 library function for GPIO initialization

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境

taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface

实现一个多进程并发的服务器

C语言习题---(数组)

关于网页中的文本选择以及统计选中文本长度

Makefile separates file names and suffixes
随机推荐
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
Makefile 分隔文件名与后缀
Database connection pool and data source
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Large top heap, small top heap and heap sequencing
Check password
C语言中的算术运算及相关练习题
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
LeetCode 2320. 统计放置房子的方式数
Socket and socket address
蜻蜓低代码安全工具平台开发之路
OpenCV调用USB摄像头的点滴
Stm32-dac Experiment & high frequency DAC output test
求轮廓最大内接圆
1. Editing weapon VIM
obsidian安装第三方插件——无法加载插件
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器