关于10-28日题目的题解与总结

来源:互联网 发布:消防工程预算软件 编辑:程序博客网 时间:2024/05/28 15:38

NOIP模拟题

总结:T1:10分,T2:0分,T3:100分。

总分只有可怜的110分。。。。。。


T1:离散化就可以过了

#include<cstdio>#include<algorithm>using namespace std;#define ll long long#define MAXN 500005int n,k;ll ans;struct node{    ll w;    ll id;}a[MAXN];int cmp(node aa,node bb){    return aa.id<bb.id;}ll w[MAXN];int main(){    int i,j;    ll x,y;//  freopen("expedition.in","r",stdin);//  freopen("expedition.out","w",stdout);    scanf("%d",&n);    for(i=1;i<=n;++i){        scanf("%lld%lld",&x,&y);        a[i].id=y,a[i].w=x;    }    sort(a+1,a+n+1,cmp);    k=0;a[0].id=0;    for(i=1;i<=n;++i){        if(a[i].id!=a[i-1].id){            w[++k]=a[i].w;        }        else w[k]+=a[i].w;    }    for(i=1;i<=k;++i){        ans+=w[i]*w[i];    }    printf("%lld\n",ans);    return 0;}

T3:狂打一波就可以过了

#include<cstdio>#include<cstring>using namespace std;#define MAXN 3010#define MAXM 100050struct edge{    int to,last,w;}edges[MAXM],new_edges[MAXM];int que[MAXN],dis1[MAXN],dis2[MAXN],head[MAXN],head1[MAXN],cnt;bool vis[MAXN];void add(int u,int v,int w){edges[++cnt].to=v,edges[cnt].w=w,edges[cnt].last=head[u],head[u]=cnt;}void addedges(int u,int v,int w){new_edges[cnt].to=v,new_edges[cnt].w=w,new_edges[cnt].last=head1[u],head1[u]=cnt;}void zSPFA(){    int H=0,T=0;    memset(dis1,0x3f,sizeof dis1);    memset(vis,false,sizeof vis);    vis[1]=true;dis1[1]=0;que[++T]=1;    while(H!=T){        if(++H>MAXN-5)H=1;        int x=que[H];vis[x]=false;        for(int i=head[x];i;i=edges[i].last){            int to=edges[i].to;            if(dis1[to]>dis1[x]+edges[i].w){                dis1[to]=dis1[x]+edges[i].w;                if(!vis[to]){                    vis[to]=true;                    if(++T>MAXN-5)T=1;                    que[T]=to;                }            }        }    }}void fSPFA(){    int H=0,T=0;    memset(dis2,0x3f,sizeof dis1);    memset(vis,false,sizeof vis);    vis[1]=true;dis2[1]=0;que[++T]=1;    while(H!=T){        if(++H>MAXN-5)H=1;        int x=que[H];vis[x]=false;        for(int i=head1[x];i;i=new_edges[i].last){            int to=new_edges[i].to;            if(dis2[to]>dis2[x]+new_edges[i].w)            {                dis2[to]=dis2[x]+new_edges[i].w;                if(!vis[to]){                    vis[to]=true;                    if(++T>MAXN-5)T=1;                    que[T]=to;                }            }        }    }}int main(){    int i,j;    int n,m,q;//  freopen("production.in","r",stdin);//  freopen("production.out","w",stdout);    scanf("%d%d",&n,&m);    for(i=1;i<=m;++i){        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        add(a,b,c);//正向建图         addedges(b,a,c);//反向建图     }    zSPFA();//正向     fSPFA();//反向     scanf("%d",&q);    for(i=1;i<=q;++i){        int a,b;        scanf("%d%d",&a,&b);        if(dis2[a]==0x3f3f3f3f||dis1[b]==0x3f3f3f3f)printf("-1\n");        else printf("%d\n",dis2[a]+dis1[b]);    }    return 0;}

T2我不想说什么了。。

#include<bits/stdc++.h>using namespace std;#define MAXN 505#define mod 10007struct node{    int size,a[MAXN];    node operator + (const node &x){        size=size>x.size?size:x.size;        for(int i=0;i<=size;++i)a[i]+=x.a[i];        return *this;    }    node operator - (const node &x){        size=size>x.size?size:x.size;        for(int i=0;i<=size;++i)a[i]-=x.a[i];        if(a[size]==0)--size;        return *this;    }    node operator * (const node &x){        node y;        memset(y.a,0,sizeof (y.a));        y.size=size+x.size;        for(int i=0;i<=size;++i)            for(int j=0;j<=x.size;++j)                y.a[i+j]+=a[i]*x.a[j];        return y;    }};stack<node>st;stack<char>ch;string s;void check(node &q){for(int i=0;i<=q.size;++i)q.a[i]=(q.a[i]%mod+mod)%mod;}void work(){    if(ch.top()=='+'){        node x=st.top();st.pop();        node y=st.top();st.pop();        st.push(x+y);    }    else if(ch.top()=='-'){        node x=st.top();st.pop();        node y=st.top();st.pop();        st.push(y-x);    }    else{        node x=st.top();st.pop();        node y=st.top();st.pop();        st.push(x*y);    }    check(st.top());    ch.pop();}bool judge(char c){    if((c=='+'||c=='-')&&ch.top()!='(')return true;    if(c=='*'&&ch.top()=='*')return true;    return false;}int main(){    cin>>s;    s="(0+"+s+"+0)";    node x;    memset(x.a,0,sizeof(x.a));    x.size=1;    x.a[1]=1;    int lens=s.size(),p=0;    while(p<lens){        if(s[p]=='('){            ch.push('(');            ++p;        }        if(s[p]>=48&&s[p]<=57){            node y;            y.size=0;            memset(y.a,0,sizeof(y.a));            while(s[p]>=48&&s[p]<=57){y.a[0]=y.a[0]*10+s[p++]-48;}            st.push(y);        }        do{            if(s[p]==')'){                while(ch.top()!='(')work();                ch.pop();            }            else if(s[p]=='x')st.push(x);            else{                while(judge(s[p]))work();                ch.push(s[p]);            }            ++p;        }while(p<lens&&s[p-1]==')');    }    node ans=st.top();    printf("%d\n",ans.size);    for(int i=0;i<=ans.size;++i){printf("%d\n",ans.a[i]);}    return 0;}

另:有标程:

#include<cstdio>#include<cstring>#include<iostream>#include<string>#include<stack>using namespace std;struct node{    int size;    int a[510];//a[i]表示x^i的系数    node operator + (const node &x){        size=size>x.size?size:x.size;//取长度较大的那个        for (int i=0;i<=size;i++) a[i]+=x.a[i];        return *this;    }    node operator - (const node &x){        size=size>x.size?size:x.size;        for (int i=0;i<=size;i++) a[i]-=x.a[i];        if (a[size]==0) size--;        return *this;    }    node operator * (const node &x){        node y;        memset(y.a,0,sizeof(y.a));        y.size=size+x.size;        for (int i=0;i<=size;i++)            for (int j=0;j<=x.size;j++)                y.a[i+j]+=a[i]*x.a[j];        return y;    }};stack <node> st;//多项式栈 stack <char> ch;//符号栈 string s;void check(node &q){    for (int i=0;i<=q.size;i++){        if (q.a[i]<0) q.a[i]+=10007;        else if (q.a[i]>=10007) q.a[i]%=10007;    }}void work(){    if (ch.top()=='+'){        node x=st.top(); st.pop();        node y=st.top(); st.pop();        x=x+y;        st.push(x);    }    else if (ch.top()=='-'){        node x=st.top(); st.pop();        node y=st.top(); st.pop();        x=y-x;        st.push(x);    }    else{        node x=st.top(); st.pop();        node y=st.top(); st.pop();        x=x*y;        st.push(x);    }    check(st.top());    ch.pop();}bool judge(char c){    if ((c=='+'||c=='-')&&ch.top()!='(') return true;    if (c=='*'&&ch.top()=='*') return true;    return false;}int main(){    cin>>s;    s="(0+"+s+"+0)";//s = "a+b" ->s = "(0+a+b+0)"     node x;    memset(x.a,0,sizeof(x.a));    x.size=1;    x.a[1]=1;    int p=0,lens=s.size();    while (p<lens){        while (s[p]=='('){//对(处理             ch.push('(');            p++;        }        if (s[p]<='9'&&s[p]>='0'){//对数字处理             node y;            y.size=0;            memset(y.a,0,sizeof(y.a));            while (s[p]<='9'&&s[p]>='0') y.a[0]=y.a[0]*10+s[p++]-'0';            st.push(y);        }        do{            if (s[p]==')'){//对)处理                 while (ch.top()!='(') work();                ch.pop();            }            else if (s[p]=='x') st.push(x);//对x处理             else{//对+,-,*处理                 while (judge(s[p])) work();                ch.push(s[p]);            }            p++;        }        while (p<lens&&s[p-1]==')');    }    node ans=st.top();    printf("%d\n",ans.size);    for (int i=0;i<=ans.size;i++) printf("%d\n",ans.a[i]);    return 0;}
原创粉丝点击