Manthan, Codefest 18 (Div 1 + Div 2) (A~E)
Codeforces 1037
F之后的先不做了...
A.Packets
logn+1,没细想,反正对。
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
int n=read(),x=0;
for(; n; n>>=1,++x);
printf("%d\n",x);
return 0;
}
B.Reach Median
//写法麻烦了
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;
int n,s,A[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
n=read(),s=read();
for(int i=1; i<=n; ++i) A[i]=read();
std::sort(A+1,A+1+n);
int p=(n+1)/2; long long ans=0;
if(A[p]>s)
{
int p2=p;
for(int i=1; i<p; ++i) if(A[i]>s) {p2=i; break;}
for(int i=p2; i<=p; ++i) ans+=A[i]-s;
}
else if(A[p]<s)
{
int p2=p;
for(int i=n; i>p; --i) if(A[i]<s) {p2=i; break;}
for(int i=p; i<=p2; ++i) ans+=s-A[i];
}
printf("%I64d\n",ans);
return 0;
}
C.Equalize
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=1e6+5;
int n;
char A[N],B[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
n=read();
scanf("%s%s",A+1,B+1);
int ans=0;
for(int i=1; i<n; ++i)
if(A[i]!=B[i])
if(A[i+1]!=B[i+1]&&A[i]==B[i+1]) std::swap(A[i],A[i+1]), ++ans;
else ++ans;
if(A[n]!=B[n]) ++ans;
printf("%d\n",ans);
return 0;
}
D.Valid BFS
用个set队列,模拟一波BFS。
#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;
int n,sz[N],A[N],Enum,H[N],nxt[N<<1],to[N<<1];
std::set<int> st[N];
bool vis[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline void AddEdge(int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum;
}
bool Solve()
{
if(A[1]!=1) return 0;
int h=1,t=1,now=2; vis[1]=1;
for(int i=H[1]; i; i=nxt[i]) st[1].insert(to[i]);
while(now<=n && h<=t)
{
int x=A[now++]; if(vis[x]) return 0;
if(!st[h].count(x)) return 0;
++t, ++sz[h], vis[x]=1;
for(int i=H[x]; i; i=nxt[i]) if(!vis[to[i]]) st[t].insert(to[i]);
if(st[t].empty()) --t;
if(sz[h]==st[h].size()) ++h;
}
return now>n;
}
int main()
{
n=read();
for(int i=1; i<n; ++i) AddEdge(read(),read());
for(int i=1; i<=n; ++i) A[i]=read();
puts(Solve()?"Yes":"No");
return 0;
}
比赛结束后
E.Trips(正难则反)
要保证答案集合中的每个点到集合内点的连边至少有k条。正序不好更新,考虑倒序。
添加完所有边后,删掉度数不足k的点,每成功删一个点,如果其邻接点也在答案集合中,将其度数减一再尝试删它然后删它的邻接点...
邻接点当然要用set存。。复杂度\(O(n\log n)\)。
当时还想了下倒着做,还立马觉得不行。。
//343ms 40100KB
#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;
int n,m,K,Enum,H[N],to[N<<1],nxt[N<<1],dgr[N],Ans[N];
bool del[N];
std::set<int> ans,E[N];
std::pair<int,int> e[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline void AddEdge(int u,int v,int i)
{
E[u].insert(v), E[v].insert(u);
e[i]=std::make_pair(u,v), ++dgr[u], ++dgr[v];
}
void Delete(int x)
{
del[x]=1, ans.erase(x);
int v;
for(std::set<int>::iterator it=E[x].begin(); it!=E[x].end(); ++it)
if(!del[v=*it] && --dgr[v]<K) Delete(v);
}
int main()
{
n=read(), m=read(), K=read();
for(int i=1; i<=m; ++i) AddEdge(read(),read(),i);
for(int i=1; i<=n; ++i) ans.insert(i);
for(int i=1; i<=n; ++i) if(!del[i]/*!!*/&&dgr[i]<K) Delete(i);
for(int i=m,u,v; i; --i)
{
Ans[i]=ans.size(), u=e[i].first, v=e[i].second;
if(!del[u]&&!del[v])//都没被删这条边才存在
{
if(--dgr[u]<K) Delete(u);
else if(--dgr[v]<K) ++dgr[u]/*不判pre会重复删*/, Delete(v);
// if(--dgr[u]<K) Delete(u,v);
// if(--dgr[v]<K && !del[v]/*!!*/) Delete(v,u);
}
E[u].erase(v), E[v].erase(u);
}
for(int i=1; i<=m; ++i) printf("%d\n",Ans[i]);
return 0;
}
------------------------------------------------------------------------------------------------------------------------
很久以前的奇怪但现在依旧成立的签名
attack is our red sun $$\color{red}{\boxed{\color{red}{attack\ is\ our\ red\ sun}}}$$ ------------------------------------------------------------------------------------------------------------------------
很久以前的奇怪但现在依旧成立的签名
attack is our red sun $$\color{red}{\boxed{\color{red}{attack\ is\ our\ red\ sun}}}$$ ------------------------------------------------------------------------------------------------------------------------