2019-2020Nowcoder Girl preliminary explanations

Topic is not very difficult, the last question is a little cancer

The first question: cattle sister love divisible

The hexadecimal number you put a split, split into several bits, then modulo, it will be found that if the hexadecimal number x, x + 1 then the binary conversion of this condition is met.

For example: a decimal number x abc a * x * x + b * x + c

Then (a * x * x + b * x + c)% k to satisfy (a + b + c) is equal to

Then x = k + 1, then x% k == 1 so that a * (x% k) * (x% k)% k + b * (x% k)% k + c% k == (a + b + c)% k

The code is simple, there is a demand to find a customer can go directly to cattle

 

The second question: eating peach

This is a tree of subject, direct dfs can be resolved, be it dp.

First dfs find depth and then, while looking for the depth of the path recorded the final output,

In order to ensure that small before the election can take a first order.

 

Third question: knapsack problem

This is a very bare backpack.

DP [j] represents the value of the j items, may constitute the largest volume of the backpack

 

Fourth Question: instant noodles

Do not know how to pass by, cf has a similar topic (I think almost before) before, later found to be two different topics.

But when the game is not found, then I write according to my impression, actually A, because I write a bug, but this bug seems to be an appropriate subject, the A mystery. . .

After the game re-write again.

This is a simulation, with a priority queue simulation on it.

Note open longlong

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
struct node{
    ll id,val;
    node(ll id=0,ll val=0):id(id),val(val){}
    bool operator<(const node &a)const{
        return a.id<id;
    }
}a[maxn];
bool cmp(node a,node b){
    return a.val<b.val;
}
priority_queue<node>que;
ll ans[maxn];
int main () {
    ll n,p,x;
    scanf("%lld%lld",&n,&p);
    for(int i=1;i<=n;i++) {
        scanf("%lld",&x);
        a[i]=node(i,x);
    }
    sort(a+1,a+1+n,cmp);
    ll nowtime=a[1].val,now=2;
    while(!que.empty()) que.pop();
    que.push(a[1]);
    while(!que.empty()){
        nowtime+=p;
        node u = que.top (); que.pop ();
        ans[u.id]=nowtime;
        while(now<=n&&a[now].val<=nowtime){
            que.push(a[now]);
            now++;
        }
        if(que.empty()&&now<=n){
            nowtime=a[now].val;
            que.push(a[now]);
            now++;        
        }
    }
    for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
    printf("\n");
    return 0;
}
Instant noodles

cf and like this topic

https://codeforces.com/contest/1248/problem/E

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <iostream>
#include <cstdlib>
#include <stack>
#define inf 0x3f3f3f3f
#define LL long long 
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
struct node{
    int id;
    ll tim;
    node(int id=0,ll tim=0):id(id),tim(tim){}
    bool operator<(const node&a)const{
        return a.id<id;
    }
}a[maxn];
queue<node>que;
priority_queue<node>prique;
int mins[maxn*4];
void build(int id,int l,int r){
    mins[id]=inf;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}
void push_up(int id){
    mins[id]=min(mins[id<<1],mins[id<<1|1]);
}
 
void update(int id,int l,int r,int pos,int val){
    if(l==r){
        mins[id]=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(id<<1,l,mid,pos,val);
    else update(id<<1|1,mid+1,r,pos,val);
    push_up(id);
}
bool cmp(node a,node b){
    if(a.tim==b.tim) return a.id<b.id;
    return a.tim<b.tim;
}
ll ans[maxn];
int main(){
    int n,p;
    scanf("%d%d",&n,&p);
    build(1,1,n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i].tim),a[i].id=i;
    sort(a+1,a+1+n,cmp);
    
    while(!que.empty()) que.pop();
    while(!prique.empty()) prique.pop();
    
    int now=1;
    ll nowtime=a[now].tim;
    que.push(a[now]);
    update(1,1,n,a[now].id,a[now].id);now++;
    
    while(now<=n&&a[now].tim<=nowtime+p){
        if(a[now].id<=mins[1]) {
            que.push(a[now]);
            update(1,1,n,a[now].id,a[now].id);now++;
        }
        else prique.push(a[now]),now++;
    }
    
    while(now<=n||!que.empty()||!prique.empty()){
        while(!prique.empty()){
            node u=prique.top();
            if(u.id<=mins[1]){
                prique.pop();
                que.push(u);
                update(1,1,n,u.id,u.id);
            }
            else  break ;
        }
        while(now<=n&&a[now].tim<=nowtime+p){
            if(a[now].id<=mins[1]) {
                que.push(a[now]);
                update(1,1,n,a[now].id,a[now].id);now++;
            }
            else prique.push(a[now]),now++;
        }
        if(!que.empty()){
            int u=que.front().id;que.pop();
            ans[u]=nowtime+p;
            update(1,1,n,u,inf);
        }
        nowtime+=p;
        if(que.empty()&&prique.empty()&&now<=n) nowtime=a[now].tim;
    }
    for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
    printf("\n");
    return 0;
}
E - Queue in the Train

 

The fifth question: pseudo-diameter

If you come across this problem required diameter of the topic before it can quickly react, but without it, you may need to think about for a while now.

And this problem has been to a lot of name suggests, it is still well written.

The answer is a reduced diameter

How do find the diameter? This can be seen in the purple book

It is twice dfs

That is, just find a spot and then find the deepest leaf node, and then find the deepest leaf node to another node from the leaf on it.

 

Question 6: maximum minimum difference

The subject good cancer

When the game did not write it

Then after the game, with tree line + one-half to write, tle 3 points

Then he asked a oi dalao, learn about the wording of the double pointer + st table, still tle, but 18 points

Finally, add a variety of optimization, 19 points tle

Finally, we learn a powerful super-fast read, and finally the A, oi fast read really strong.

Also it seems to be in a monotonous two-pointer queue + write, but so hard to write, I will not. . .

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
# define getchar() (S==T&&(T=(S=BB)+fread(BB,1,1<<20,stdin),S==T)?EOF:*S++)
char BB[1 << 20], *S = BB, *T = BB;
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
int maxsum[maxn][22],minsum[maxn][22],a[12][maxn],v[12],f[maxn],w[30];
void init(int n){
    for(int i=1;i<maxn;i++) f[i]=(log(i*1.0)/log(2.0));
    for(int i=0;i<30;i++) w[i]=(1<<i);
}
void RMQ(int id,int n) {
    for(int i=1;i<=n;i++){
        maxsum[i][0]=a[id][i];
        minsum[i][0]=a[id][i];
    }
    for (int i = 1; i < 20; i++) {
        for (int j = 1; j <= n; j++) {
            if (j + w[i] - 1 <= n) {
                [j] [i] = max ([j] [i - 1 ], [j + w [I- ]] [i - 1, 1 ]);
                minsum[j][i] = min(minsum[j][i - 1], minsum[j + w[i-1]][i - 1]);
            }
        }
    }
}
  
int st ( int x, int y) {
     int k = f [y-x + 1 ];
    int maxnum = max (maxsum [x] [k], maxsum [y - w [k] + 1 ] [k]);
    int minnum = min (minsum [x] [k], minsum [y - w [k] + 1 ] [k]);
    int ans = maxnum - minnum;
    return ans;
}
int L[maxn],R[maxn];
int read()
{
    int x=0;
    char c=getchar();
    while (!isdigit(c)) c=getchar();
    while (isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x;
}
 
inline void write(ll X) {
    if (X < 0) { putchar('-'); X = ~(X - 1); }
    int s[20], top = 0;
    while (X) { s[++top] = X % 10; X /= 10; }
    if (!top) s[++top] = 0;
    while (top) putchar(s[top--] + '0');
    putchar('\n');
}
  
int main(){
    int n,t;
    n=read();
    t=read();
    init(n);
    for(int i=1;i<=t;i++) v[i]=read();
    for(int i=1;i<=t;i++){
        for(int j=1;j<=n;j++){
            a[i][j]=read();
        }
    }
    ll ans=0;
    memset(R,inf,sizeof(R));
    for(int i=1;i<=t;i++){
        RMQ(i,n);
        int p1=1,p2=1;
        for(int j=1;j<=n;j++){
            while(p1<=j&&st(p1,j)>v[i]) p1++;
            while(p2+1<=j&&st(p2+1,j)>=v[i]) p2++;
              
            if(st(p1,j)!=v[i]) L[j]=inf;
            else L[j]=max(L[j],p1);
            if(st(p2,j)!=v[i]) R[j]=0;
            else R[j]=min(R[j],p2);
        }
    }
    for(int i=1;i<=n;i++){
        if(L[i]<=R[i]) ans+=R[i]-L[i]+1;
    }
//    printf("%lld\n",ans);
    write(ans);
    return 0;
}
Amazing fast read

 

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/12030689.html