2019 Google Kickstart Round H

Very good subject, is less likely to ...

H-Index

The meaning of problems: For an array, each for \ (I (. 1 \ Leq I \ n-Leq) \) , to find a digital \ (H \) , so \ (a_ {1} ... a_ {i} \) greater than equal to \ (H \) number of figures that can appear also greater than or equal \ (H \) .
solution:
may be found to be from \ (I \) to (I + 1 \) \ , increasing the answer up \ (1 \) , may be implemented in query operation with the priority queue, or data structures, time complexity \ (O (nlog ( the n-)) \) .
code (Chairman of the tree):

#include<bits/stdc++.h>

using namespace std;
const int N = 2e5+100;
int a[N];
vector<int> v[N<<2];
vector<int>::iterator it;
 
void build(int id,int l,int r){
    v[id].clear();
    for(int i = l; i <= r; i++)
        v[id].push_back(a[i]);
    sort(v[id].begin(),v[id].end());
    if(l == r)
        return;
    int mid = (l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}
 
int query(int id,int L,int R,int l,int r,int h){
    if(l <= L && R <= r){
        it = upper_bound(v[id].begin(),v[id].end(),h);
        return it - v[id].begin();
    }
    int mid = (L+R)>>1;
    int ans = 0;
    if(l <= mid)
        ans += query(id<<1,L,mid,l,r,h);
    if(mid < r)
        ans += query(id<<1|1,mid+1,R,l,r,h);
    return ans;
}

int main(){
    int T,n;
    scanf("%d",&T);
    int cas=1;
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) v[i].clear();
        vector<int> ans(n+1);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        ans[1]=1;
        for(int i=2;i<=n;i++){
            int counts=i-query(1,1,n,1,i,ans[i-1]); 
            if(counts>=ans[i-1]+1){
                ans[i]=ans[i-1]+1;
            }else{
                ans[i]=ans[i-1];
            }
        }
        cout<<"Case #"<<cas++<<":";
        for(int i=1;i<=n;i++){
            cout<<' '<<ans[i];
        }
        cout<<endl;
    }
    return 0;
}

Diagonal Puzzle

Meaning of the questions:
a matrix, each cell is black or white, you can now choose a color of the diagonal and reverse diagonal, diagonal to ask how many full-matrix least reverse the black.
sol:
each point abstracted edge, each diagonal abstracted point, if \ (maze_ {i, j} \) as a white, it must go through to reverse diagonal in this coordinate wherein a, FIG consider building even numbered dots represent not reversed, to reverse the odd-numbered, according to color build FIG.

Assuming that the coordinates of white after the diagonal numbered \ (U \) and \ (V \) , then
\ (the Add (2U, 2V +. 1), the Add (2V, + 2U. 1) \) , represents \ (u, v \) must be reversed one of them. If black is \ (the Add (2U, 2v), the Add (+ 1,2v 2U + 1) \) .

For each white coordinates, inversion two kinds of enumeration (parity staining) performed for each point \ (the DFS \) to time complexity \ (O (n-2 ^) \) .
code:

#include<bits/stdc++.h>

using namespace std;
const int N = 100000;
string maze[N];
vector<int> G[N];
bool vis[N];
int counts=0;
void dfs(int u){//奇数是反转的
    if(vis[u]) return ;
    vis[u]=1;
    if(u&1){
        counts++;
    }
    for(int v:G[u]){
        dfs(v);
    }
}
void add(int u,int v){
    G[u].push_back(v);
    G[v].push_back(u);
}
void solve(){
    counts=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=N-1;i++) G[i].clear();
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>maze[i];
    }
    int all=4*n-2;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int u=i-j+n;
            int v=i+j+2*n;
            // cout<<i<<' '<<j<<' '<<u<<' '<<v<<endl;
            if(maze[i][j]=='.'){
                add(2*u,2*v+1);//u反转
                add(2*v,2*u+1);//v反转
            }else{
                    add(2*u,2*v);//都反转
                    add(2*u+1,2*v+1);//都不反转
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int x=i-j+n;
            int y=x*2+1;//不反转
            x*=2;//反转
            if(maze[i][j]=='.'&&!vis[x]){
                counts=0;
                dfs(x);
                int res=counts;
                counts=0;
                dfs(y);
                ans+=min(res,counts);
            }
        }
    }
    cout<<ans<<endl;
}
int main(){
    int T;
    cin>>T;
    int cas=1;
    while(T--){
        cout<<"Case #"<<cas++<<": ";
        solve();
    }
    return 0;
}

Elevngrm

To be completed (dp).

Guess you like

Origin www.cnblogs.com/codancer/p/12232467.html