Codeforces Round #142 Div.2 题解

55 篇文章 0 订阅
36 篇文章 0 订阅

A

打n条龙,龙的攻击力xi,打死每条龙攻击力加yi,你的初始攻击力是s.
如果你的攻击力小于或者等于某条龙的攻击力你就死了.
问你能不能活着回去.

每次贪心打攻击力最小的龙,将龙排序既可.

#include<bits/stdc++.h> //Ithea Myse Valgulious
using namespace std;
const int yuzu=1e5;
struct drn{
int x,y;
bool operator <(const drn &b) const{
  return x^b.x?x<b.x:y>b.y;
  }
}d[yuzu|10];
int main(){
int s=read(),n=read(),i;
for (i=1;i<=n;++i) d[i].x=read(),d[i].y=read();
sort(d+1,d+n+1);
for (i=1;i<=n;++i){
  if (s<=d[i].x) return puts("NO"),0;
  s+=d[i].y;
  }puts("YES");
}

B

判断一个数是不是刚好有3个约数.
这个数显然必须是质数的平方.用筛法筛出 106 10 6 以内的质数,然后算出 n n 并判断.

#include<bits/stdc++.h> //Ithea Myse Valgulious
using namespace std;
const int yuzu=1e6;
typedef int fuko[yuzu|10];
fuko pr,p;

void preprime(){
int i,j;
fill(p+2,p+yuzu+10,1);
for (i=2;i*i<=yuzu;++i){
  if (p[i]){
    for (j=i*i;j<=yuzu;j+=i) p[j]=0;
    }
  }
}

bool judge(ll x){
ll t=sqrt(x);
if (t*t^x) return 0;
return p[t];
}

int main(){
preprime();
for (int t=read();t--;){
  ll x;read(x);
  puts(judge(x)?"YES":"NO");
  }
}

C

一个n行m列的01矩阵,每次可以将一行的最后一个数移到最前,或者将一行的最前面一个数移到最后.
求使得某列全为1的最小移动次数,不能输出-1.

每一行用set存储 1 1 的位置,每一次枚举一列,在行里二分找到离该列最近的两个1的位置,并求出移动次数相加取最小值.
中间WA了9发,那个suan函数改动得乱七八糟,但是最后终于A掉了.

#include<bits/stdc++.h> //Ithea Myse Valgulious
using namespace std;
const int yuzu=1e4,_=105,inf=yuzu*_;
typedef int fuko[yuzu|10];
char c[yuzu|10];
set<int> s[_];
int n,m;
#define cal(a,b) min(min(abs(a-b),abs(a+m-b)),abs(b+m-a))//把第a行移动到第b行最小的移动次数.
int suan(int r,int c){
int ans;
auto p=s[r].lower_bound(c);
if (p==s[r].end()){
  ans=min(cal(*s[r].rbegin(),c),cal(*s[r].begin(),c));
  }else if (p==s[r].begin()){
  ans=min(cal(*p,c),cal(*s[r].rbegin(),c));
  }else{
  ans=min(cal(*p,c),cal(c,*--s[r].lower_bound(c)));
  }
return ans;
}

int main(){
int i,j,ans=inf;
scanf("%d%d",&n,&m);
for (i=1;i<=n;++i){
  scanf("%s",c+1);
  for (j=1;j<=m;++j) if (c[j]&1) s[i].insert(j);
  }
for (j=1;j<=m;++j){
  int tmp=0;
  for (i=1;i<=n;++i){
    if (s[i].empty()) return puts("-1"),0;
    tmp+=suan(i,j);
    }
  ans=min(ans,tmp);
  }write(ans);
}

D

基本是个裸的最短路.不过当你在第i秒到达某个点的时候这个点有可能被某个tourist占了,
这样你必须要在下一秒才能从这个点出发.

跑个 spfa s p f a 即可.(这里 spfa s p f a 没死).
在把队首取出来的时候,看看当前点在 dist[i] d i s t [ i ] 的时间是不是被某个tourist占了,否则将 dist[i]+1 d i s t [ i ] + 1 ,直到没有被占.
这样都是D题我是真的服了.
不过我也有T66个点,那真的是黑历史了,我每次扩展到一个点的时候都把这个点是不是被占判断一遍T飞了.

#pragma GCC optimize("inline",3)
#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rec register char
#define rel register ll
#define gc getchar
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) x=-x,pc('-');
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=2e5,inf=0x3f3f3f3f;
typedef int fuko[yuzu|10];
struct edge{int to,w;};
vector<edge> lj[yuzu|10];
set<int> tors[yuzu|10];
fuko vis,dist;
int n,m;
#define key(v) for (;v^n&&tors[v].count(dist[v]);++dist[v])
int spfa(int s){
queue<int> q;
memset(vis,0,sizeof vis);
memset(dist,0x3f,sizeof dist);
q.push(s),vis[s]=1,dist[s]=0;
for (;!q.empty();){
  int u=q.front();q.pop();
  vis[u]=0;
  key(u);
  for (auto i:lj[u]){
    int v=i.to,w=i.w;
    if (dist[v]>dist[u]+w){
      dist[v]=dist[u]+w;    
      if (!vis[v]) vis[v]=1,q.push(v);
      }
    }
  }
}

int main(){
int i,j;n=read(),m=read();
for (i=1;i<=m;++i){
  int u=read(),v=read(),w=read();
  lj[u].push_back(edge{v,w});
  lj[v].push_back(edge{u,w});
  }
for (i=1;i<=n;++i){
  for (j=read();j--;){
    tors[i].insert(read());
    }
  }
spfa(1);
write(dist[n]^inf?dist[n]:-1);
}

E

一张完全图,取m条边出来构造一张图,求这张图和去掉m条边剩下的图的三元环数的总和.
E题有这种题真是扶她服他了.
你只要乱搞一下就可以莫名其妙地A掉了.
令点 i i 的度数是cnt[i],被破坏掉的三元环的数目是 (n1cnt[i])×cnt[i] ( n − 1 − c n t [ i ] ) × c n t [ i ] .
由于破坏三元环是双向的,除以二即可.

#pragma GCC optimize("inline",3)
#include<bits/stdc++.h> //Ithea Myse Valgulious
using namespace std;
const int yuzu=1e6;
typedef ll fuko[yuzu|10];
fuko cnt;
int main(){
int n=read(),m=read(),i;
ll ans=0;
for (i=1;i<=m;++i) ++cnt[read()],++cnt[read()];
for (i=1;i<=n;++i) ans+=(n-cnt[i]-1)*cnt[i];
write(1ll*n*(n-1)*(n-2)/6-ans/2);
}

谢谢大家.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值