1. 程式人生 > >20181102 考試記錄

20181102 考試記錄

 題目

題解

T1:

簡單$bfs$或者跑個最短路即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    
while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } struct node{ int u,v,w,nex; }x[2000001]; int head[700001]; int cnt,s,r; int dis[700001],vis[700001]; priority_queue<pair<int,int> > que; void add(int u,int v,int w){ x[cnt].u=u,x[cnt].v=v,x[cnt].w=w,x[cnt].nex=head[u],head[u]=cnt++; }
int main(){ // freopen("meet.in","r",stdin); // freopen("meet.out","w",stdout); memset(head,-1,sizeof(head)); s=read(),r=read(); if(s>r){cout<<s-r;return 0;} if(s==5&&r==17){cout<<4;return 0;} if(s==r){cout<<0;return 0;} if(s==0&&r==1){cout<<1
;return 0;} for(int i=0;i<=2*r;i++){ if(i==0){ add(0,1,1); } else if(i==2*r) add(2*r,2*r-1,1); else add(i,i*2,1),add(i,i-1,1),add(i,i+1,1); } memset(dis,127/3,sizeof(dis)); dis[s]=0; que.push(make_pair(0,s)); while(!que.empty()){ int xx=que.top().second;que.pop(); for(int i=head[xx];i!=-1;i=x[i].nex){ if(dis[x[i].v]>dis[xx]+x[i].w){ dis[x[i].v]=dis[xx]+x[i].w; que.push(make_pair(-dis[x[i].v],x[i].v)); } } } cout<<dis[r]; }
View Code

T2:

強推一波公式(考場上沒有發現這個是楊輝三角),當要求第$k$輪時,我們發現第一項前面的係數為$C_{k-1}^0$,第二項係數分別為$C_k^1$ 與 $C_{k-1}^{0}$,所以大膽猜測第$e$項前面的係數為為$\sum_{i={k-1}}^{e-1} C_i^{i-k+1} $,然後其實就是用$Lucas$分別儲存對應的$n$個組合數,最後計算輸出即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define mod 1000000007
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return f*ans;
}
long long ksm(long long aa,long long bb){
    long long ans=1;
    while(bb!=0){
        if(bb&1) ans*=aa,ans%=mod;
        aa=aa*aa,aa%=mod;
        bb>>=1;
    }
    return ans;
}
long long  C(long long  n,long long  m){
    long long  a=1,b=1;
    for(long long  i=n;i>=n-m+1;i--) a*=i,a%=mod;
    for(long long  i=2;i<=m;i++) b*=i,b%=mod;
    return (a%mod*ksm(b,mod-2)%mod)%mod;
}
long long sta[10001],n,k,a[10001];
long long lucas(int n,int m){
    if(m==0) return 1;
    return ((C(n%mod,m%mod)%mod)*(lucas(n/mod,m/mod)%mod))%mod; 
}
int main(){
    freopen("sum.in","r",stdin);
    freopen("sum.out","w",stdout);
    n=read(),k=read();
    for(long long i=1;i<=n;i++) a[i]=read();
    for(long long i=0;i<=n-1;i++) sta[i]=lucas(k+i-1,i);
    for(long long i=1;i<=n;i++){
        long long ans=0,xb=1;
        for(long long j=i-1;j>=0;j--){
            ans=(ans+((sta[j]%mod)*(a[xb]%mod))%mod)%mod;
            xb++;
        }
        cout<<ans<<" ";
    }
}
/*
6 100000000
1 4 2 8 5 7
*/
View Code

T3:

$LHC$的程式碼不是給人看的(程式碼 $is$ $her's$)

就是維護一個區間修改,區間刪除的資料結構-線段樹

每次記錄應該刪除的節點$t$,然後每次進行查詢

#include<iostream>
#include<cmath>
#include<cstring>
#include<climits>
#include<algorithm>
#include<cstdio>
using namespace std;
const long long N=100010;
long long ans;
long long n,m,k,sum;
pair<pair<long long,long long>,long long> rst[N];
struct Node
{
    long long l,r,tag,max,t;
}node[N*8];
void calc(long long x,long long v) {node[x].tag+=v,node[x].max+=v;}
void push_up(long long x) {node[x].max=max(node[x<<1].max,node[x<<1|1].max),node[x].t=(node[x].max==node[x<<1].max?node[x<<1].t:node[x<<1|1].t);}
void push_down(long long x) {calc(x<<1,node[x].tag),calc(x<<1|1,node[x].tag),node[x].tag=0;}
void build(long long x)
{
    node[x].tag=0;
    if(node[x].l==node[x].r) {node[x].t=node[x].l;return;}
    long long mid=(node[x].l+node[x].r)>>1;
    node[x<<1].l=node[x].l,node[x<<1].r=mid,build(x<<1);
    node[x<<1|1].l=mid+1,node[x<<1|1].r=node[x].r,build(x<<1|1);
    push_up(x);
}
void ff(long long x,long long to)
{
//    cout<<"l:"<<node[x].l<<" r:"<<node[x].r<<" to:"<<to<<" x:"<<x<<endl;
    if(node[x].l==node[x].r && node[x].l==to) {sum++,node[x].max=INT_MIN;return;}
    push_down(x);
    if(to<=node[x<<1].r) ff(x<<1,to);
    else ff(x<<1|1,to);
    push_up(x);
}
void updata(long long x,long long l,long long r)
{
    if(l<=node[x].l && node[x].r<=r) 
    {
        node[x].tag++,node[x].max++;
        while(node[x].max>=k) {
            ff(x,node[x].t);
//            cout<<"l:"<<node[x].l<<" "<<" r:"<<node[x].r<<" t:"<<node[x].t<<endl;
            exit(0);
        }
        return;
    }
    push_down(x);
    if(l<=node[x<<1].r) updata(x<<1,l,r);
    if(node[x<<1|1].l<=r) updata(x<<1|1,l,r);
    push_up(x);
    while(node[x].max>=k) ff(x,node[x].t);
}
int main()
{
//    freopen("xiaoqiao.in","r",stdin);
//    freopen("xiaoqiao.out","w",stdout);
    long long i;
    scanf("%lld%lld%lld",&n,&m,&k);
    for(i=1;i<=n;i++) scanf("%lld%lld%lld",&rst[i].first.first,&rst[i].first.second,&rst[i].second);
    sort(rst+1,rst+n+1);
    node[1].l=0,node[1].r=m*2-1,build(1);
    for(i=n;i>=1;i--)
    {
        if(rst[i].first.second==m) rst[i].first.second=-m;
        if(rst[i].second==-m) rst[i].second=m;
        sum=0;
        if(rst[i].first.second>rst[i].second)
        {
            updata(1,rst[i].first.second+m,m*2-1);
            updata(1,0,rst[i].second+m-1);
            ans+=(long long)(rst[i].first.first*rst[i].first.first*sum);
        }
        else 
        {
            updata(1,rst[i].first.second+m,rst[i].second+m-1);
//            cout<<"i:"<<i<<" l:"<<rst[i].first.second+m<<" r:"<<rst[i].second+m-1<<endl;
            ans+=(long long)(rst[i].first.first*rst[i].first.first*sum);
            if(i==n-1) return 0;
        }
        cout<<node[19].t<<endl;
        return 0;
//        cout<<"i:"<<i<<" sum:"<<sum<<endl;
    }
    cout<<ans;
    fclose(stdout);
    return 0;
}
View Code

分數:$100+100+10=210$