Codeforces Round #142 Div.2 Problem Solution

Keywords: less

A

Strike n dragons, dragon attack xi, kill each dragon attack plus yi, your initial attack is s.
If your attack power is less than or equal to that of a dragon, you die.
Ask if you can go back alive.
Every time greedy strikes the least aggressive dragon, the dragon can be sorted.

#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

Judge whether a number has exactly three approximations.
This number must obviously be the square of the prime number. Screen out the prime number within 106106 by sieving method, then calculate n_n and judge it.

#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

The 01 matrix of an n-row m-column can move the last number of a row to the front or the first number of a row to the end at a time.
Find the minimum number of moves that make a column all 1, and cannot output -1.
Each row uses set to store 11 locations, enumerates a column each time, finds the nearest two 11 locations in the row by dichotomy, and calculates the minimum number of movement additions.
In the middle of the WA 9 rounds, the suan function was changed in a mess, but finally A was dropped.

#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))//Move line a to line b with the minimum number of moves.
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

It's basically a naked shortest path. But when you reach a point in the second, it may be occupied by a tourist.
So you have to start at this point in the next second.
Run a spfaspfa. (Here spfaspfa is not dead.)
When the head of the team is taken out, see if the current point in dist[i]dist[i] is occupied by a tourist, or dist [i] + 1 dist [i] + 1, until it is not occupied.
It's all question D. I really take it.
But I also have T66 points. That's really a black history. Whenever I expand to one point, is this point occupied to judge T-flight once?

#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

A complete graph is constructed by taking m edges and finding the sum of the number of ternary rings of the graph with the remaining m edges removed.
Question E had this kind of question, which really helped her to convince him.
All you have to do is mess around and you can get rid of A for no reason.
The degree of point I I is cnt[i]cnt[i], and the number of triple rings destroyed is (n_1_cnt[i])*cnt[i](n_1_cnt[i])*cnt[i](n_1_cnt[i])*cnt[i].
Since the destruction of the ternary ring is bidirectional, divide it by two.

#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);
}

Thank you.

Posted by Iconoclast on Fri, 10 May 2019 17:23:05 -0700