[Race] to improve the simulation test Jizhong Group A 19.8.23

Task.1 matrix multiplication

Topic effect: to give you a \ (N \ times N \) matrix, \ (Q \) times ask Always ask a sub-rectangular section \ (K \) decimal places.

Data range: \ (. 1 \ Leq N \ Leq 500, Q \ Leq 60000 \)

Tree Tree sleeve (Chairman tree), whole-half block. Probably it will be able to in the past ... but I do not ds master.

\ (Source: \) smart people can see the 2738 BZOJ Luogu P1527

Code:

Task.2 Tree

Title effect: given a \ (n-\) points \ (m \) Right of band edge (weight \ (C \) ) FIG communication, to ensure there is a minimum spanning tree. Standard deviation for the Minimum Spanning Tree.

Data range: \ (. 1 \ Leq N \ Leq 100,. 1-N \ Leq M \ Leq 2000, C_i \ Leq 100 \)

Standard deviation for the Minimum Spanning Tree to the spanning tree is ... \ (n-1 \) edges to minimize this thing: \ (\ sqrt {\ FRAC {\ SUM (c_i- \ overline {C}) ^ 2 } {n-1}} \ )

Essentially minimized \ (\ SUM (c_i- \ overline C) ^ 2 \) . The next operation is more a routine.

Order \ (F (X) = \ SUM (C_i-X) ^ 2 \) , can be seen that this formula is the \ (X \) When the average number of certain \ (C_i \) seeking a variance something similar when \ (x = \ overline c \ ) when \ (f (x) \) is equal to the above formula.

A more preliminary idea is to enumerate a \ (x \) to seek potential \ (c \) What, then finalized these \ (c \) find the standard deviation. We follow the edge \ ((c_i-x) ^ 2 \) sorted seeking "minimal" spanning tree can be determined \ (\ overline c \) closest \ (X \) when selected from the group which \ (C \) can minimize \ (f (the X-) \) , constantly increases \ (x \) at the same time to update the answer.

\ (Source: \) Smart people can see BZOJ 3754

Code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

template<class T>void read(T &x){
    x=0; char c=getchar();
    while(c<'0'||'9'<c)c=getchar();
    while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^48); c=getchar();}
}
typedef double db;
const int N=105,M=2005;

int n,m;
int fa[N];
struct edge{int x,y,c;}e[M];
int q[N]; db ave,ans=1e9;

db sqr(db x){return x*x;}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
bool cmp(edge e1,edge e2){return sqr(e1.c-ave)<sqr(e2.c-ave);}
void kruskal(){
    int x,y,fx,fy,cnt=0;
    db tmp=0,sum=0;
    sort(e+1,e+m+1,cmp);
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m&&cnt<n;i++){
        x=e[i].x; y=e[i].y;
        if((fx=find(x))==(fy=find(y)))continue;
        fa[fx]=fy; tmp+=e[i].c; q[++cnt]=e[i].c;
    }
    if(cnt!=n-1)return ;
    tmp/=(n-1);
    for(int i=1;i<n;i++)sum+=sqr(q[i]-tmp);
    ans=min(ans,sum);
    return ;
}

int main(){
//  freopen("in","r",stdin);
    read(n); read(m);
    for(int i=1;i<=m;i++){
        read(e[i].x); read(e[i].y); read(e[i].c);
    }
    for(ave=0.25;ave<=100;ave+=0.25)kruskal();
    printf("%.4lf\n",sqrt(ans/(n-1)));
    return 0;
}

Task.3 Points and Segments

Title effect: the shaft has a number \ (n-\) line segments \ ([L, R & lt] \) , each segment should now dyed red or blue. Seeking a number satisfying all points frequency axes is covered by two colors line \ (| c_ {red} -c_ {blue} | \ leq 1 \) is a legitimate staining protocol.

Data range: \ (. 1 \ Leq N \ ^ Leq 10 5,0 \ Leq L_i, r_i \ Leq. 9 ^ 10 \)

Scared me I thought the number of program requirements

It is contemplated that the two colors are viewed as transfection \ (1 + \ -1 \) , then the interval is required after the subtraction result does not exceed the absolute value of each position \ (1 \) .

And then there is the idea to build a discrete map, connected \ (L_i, r_i \) , and then let each position is as equal as possible in two colors side cover.

If you combine the two ideas about, watching the construction to have the right values. Method of assigning weights is directed to the side. The point is to balance a number of sides left to right. From one point walked in one direction, and then come back ... If we can balance this number, we will be able to find a directional program.

Loop? What loop? We found this thing a bit like Euler.

If there is no built Figure singularity, then there must be an Euler tour, then we can construct a legitimate program.

If there is a singularity. So the number of singularities must be an even number (Think about why). From left to right we enumerate every singularity, and the nearest one singular point edge even after he put the two of them have become even point, and then again to jump, so that we can become the kind of the above situation, and then to solve Europe back to the road on it. For those edges even between singularity deleted nor will the answer becomes illegal (Think about why).

Code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;

template<class T>void read(T &x){
    x=0; char c=getchar();
    while(c<'0'||'9'<c)c=getchar();
    while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^48); c=getchar();}
}

const int N=200050;

int n;
struct seg{int l,r;}a[N];
int t[N],cnt;
int d[N],ans[N];
int head[N],tot=1,pos[N];
struct edge{int to,next;}e[N<<1];
int vis[N],ve[N];

void add(int x,int y){e[++tot]=(edge){y,head[x]}; head[x]=tot;}
void dfs(int x,int dep=1){
    vis[x]=1;
    for(int i=head[x],y;i;i=e[i].next){
        y=e[i].to; if(ve[i>>1])continue;
        ve[i>>1]=1; ans[pos[i>>1]]=x<y;
        dfs(y);
    }
}

int main(){
//  freopen("in","r",stdin);
    read(n);
    int x,y;
    for(int i=1;i<=n;i++){
        read(x); read(y); ++y;
        a[i]=(seg){x,y};
        t[++cnt]=x; t[++cnt]=y;
    }
    sort(t+1,t+cnt+1);
    cnt=unique(t+1,t+cnt+1)-t-1;
    for(int i=1;i<=n;i++){
        x=lower_bound(t+1,t+cnt+1,a[i].l)-t;
        y=lower_bound(t+1,t+cnt+1,a[i].r)-t;
        ++d[x]; ++d[y];
        add(x,y); add(y,x);
        pos[tot>>1]=i;
    }
    for(x=1,y=-1;x<=cnt;x++)if(d[x]&1){
        if(y==-1)y=x;
        else {add(x,y); add(y,x); y=-1;}
    }
    for(x=1;x<=cnt;x++)
        if(!vis[x])dfs(x);
    for(int i=1;i<=n;i++)printf("%d ",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/opethrax/p/11402427.html