当前位置:网站首页>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);
}
边栏推荐
- GeoServer offline map service construction and layer Publishing
- Huawei interview question: no palindrome string
- 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
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- 1. Editing weapon VIM
- 解决el-radio-group 回显后不能编辑问题
- LeetCode - 搜索二维矩阵
- Base64 编码原来还可以这么理解
- [Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
- STM32 library function for GPIO initialization
猜你喜欢
Fatal: unsafe repository is owned by someone else
【NOI模拟赛】伊莉斯elis(贪心,模拟)
【C语言】详解指针的初阶和进阶以及注意点(1)
LeetCode 209. Minimum length subarray
【apipost】使用教程
Introduction to mathjax (web display of mathematical formulas, vector)
Why can't programmers who can only program become excellent developers?
kityformula-editor 配置字号和间距
Btrace- (bytecode) dynamic tracking tool
Base64 编码原来还可以这么理解
随机推荐
C # delay, start the timer in the thread, and obtain the system time
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
fatal: unsafe repository is owned by someone else 的解决方法
1、编辑利器vim
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Fundamentals of software testing
2、const 型指针
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
.NET Core 日志系统
Yolov6 training: various problems encountered in training your dataset
蜻蜓低代码安全工具平台开发之路
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
表格响应式布局小技巧
LeetCode 2320. 统计放置房子的方式数
C# richTextBox控制显示最大行数
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
Why can't browsers read JSX?
实现一个多进程并发的服务器
Fatal: unsafe repository is owned by someone else