zoukankan      html  css  js  c++  java
  • noip2013day1模拟赛

    转圈游戏
    题目描述 Description

    n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏。按照顺时针方向给 n 个位置编号,从0 到 n-1。最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推。
    游戏规则如下:每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,……,依此类推,第n - m号位置上的小伙伴走到第 0 号位置,第n-m+1 号位置上的小伙伴走到第 1 号位置,……,第 n-1 号位置上的小伙伴顺时针走到第m-1 号位置。
    现在,一共进行了 10^k 轮,请问 x 号小伙伴最后走到了第几号位置。

    输入描述 Input Description

    输入共 1 行,包含 4 个整数 n、m、k、x,每两个整数之间用一个空格隔开。

    输出描述 Output Description

    输出共 1 行,包含 1 个整数,表示 10^k 轮后 x 号小伙伴所在的位置编号。

    样例输入 Sample Input

    10 3 4 5

    样例输出 Sample Output

    5

    数据范围及提示 Data Size & Hint

    对于 30%的数据,0 < k < 7; 
    对于 80%的数据,0 < k < 10^7; 
    对于 100%的数据,1 < n < 1,000,000,0 < m < n,1 <= x <=n,0 < k < 10^9。

    题解

    快速幂**题,自己傻逼的把ksm里面ret置成了0,直接gg

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 #define ll long long
     6 ll ksm(ll a,ll b,ll p)
     7 {
     8     ll ret(0);
     9     ll x=a%p;
    10     while(b)
    11     {
    12         if(b&1)ret=ret*x%p;
    13         b>>=1;
    14         x=x*x%p;
    15     }
    16     return ret;
    17 }
    18 int main()
    19 {
    20     freopen("circle.in","r",stdin);
    21     freopen("circle.out","w",stdout); 
    22     ll n,m,k,x;
    23     scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&x);
    24     printf("%I64d",(x%n+m*ksm(10,k,n)%n)%n);
    25     return 0;
    26 }

    火柴排队

    题目描述 Description

    涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度。现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:
    ,其中 ai表示第一列火柴中第 i 个火柴的高度,bi表示第二列火柴中第 i 个火柴的高度。
    每列火柴中相邻两根火柴的位置都可以交换,请你通过交换使得两列火柴之间的距离最小。请问得到这个最小的距离,最少需要交换多少次?如果这个数字太大,请输出这个最小交换次数对 99,999,997 取模的结果。

    输入描述 Input Description

    共三行,第一行包含一个整数 n,表示每盒中火柴的数目。
    第二行有 n 个整数,每两个整数之间用一个空格隔开,表示第一列火柴的高度。
    第三行有 n 个整数,每两个整数之间用一个空格隔开,表示第二列火柴的高度。

    输出描述 Output Description

    输出共一行,包含一个整数,表示最少交换次数对 99,999,997 取模的结果。

    样例输入 Sample Input

    [Sample 1]

    2 3 1 4 
    3 2 1 4
    [Sample 2]

    1 3 4 2 
    1 7 2 4

    样例输出 Sample Output

    [Sample 1]
    1
    [Sample 2]
    2

    数据范围及提示 Data Size & Hint

    【样例1说明】
    最小距离是 0,最少需要交换 1 次,比如:交换第 1 列的前 2 根火柴或者交换第 2 列的前 2 根火柴。
    【样例2说明】
    最小距离是 10,最少需要交换 2 次,比如:交换第 1 列的中间 2 根火柴的位置,再交换第 2 列中后 2 根火柴的位置。
    【数据范围】
    对于 10%的数据, 1 ≤ n ≤ 10; 
    对于 30%的数据,1 ≤ n ≤ 100; 
    对于 60%的数据,1 ≤ n ≤ 1,000; 
    对于 100%的数据,1 ≤ n ≤ 100,000,0 ≤火柴高度≤ 2^31 - 1。

    题解

    逆序对,又写gg,还是没理解透题

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 #define mod 99999997
     6 #define maxn 100005
     7 int ans,tree[maxn],a[maxn],c[maxn],n;
     8 inline int read()
     9 {
    10     char ch=getchar();
    11     int ret(0);
    12     while(ch>'9'||ch<'0')ch=getchar();
    13     while(ch>='0'&&ch<='9')
    14     {
    15         ret=(ret<<1)+(ret<<3)+ch-'0';
    16         ch=getchar();
    17     }
    18     return ret;
    19 }
    20 struct node{
    21     int x,id;
    22 }b[maxn];
    23 int lowbit(int x){return x&(-x);}
    24 void insert(int x)
    25 {
    26     for( ; x<=n ; x+=lowbit(x))tree[x]++;
    27 }
    28 int query(int x)
    29 {
    30     int ret(0);
    31     for( ; x ; x-=lowbit(x))ret+=tree[x];
    32     return ret; 
    33 }
    34 bool cmp(node p,node q){return p.x<q.x;}
    35 int main()
    36 {
    37     n=read();
    38     for(int i=1 ; i<=n ; ++i)a[i]=read();
    39     for(int i=1 ; i<=n ; ++i)b[i].x=read(),b[i].id=i;
    40     sort(b+1,b+1+n,cmp);
    41     for(int i=1 ; i<=n ; ++i)c[i]=a[b[i].id];
    42     for(int i=1 ; i<=n ; ++i)a[i]=c[i];
    43     sort(a+1,a+1+n);
    44     for(int i=1 ; i<=n ; ++i)c[i]=lower_bound(a+1,a+1+n,c[i])-a;
    45     for(int i=1 ; i<=n ; ++i)
    46     {
    47         insert(c[i]);
    48         ans=(ans+i-query(c[i]))%mod;
    49     }
    50     printf("%d",ans);
    51     return 0;
    52 }

    货车运输

    题目描述 Description

    A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。

    输入描述 Input Description

    第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。
    接下来 m 行每行 3 个整数 x、y、z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意:x 不等于 y,两座城市之间可能有多条道路。
    接下来一行有一个整数 q,表示有 q 辆货车需要运货。
    接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意:x 不等于 y。

    输出描述 Output Description

    输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。

    样例输入 Sample Input

    4 3 
    1 2 4 
    2 3 3 
    3 1 1 
    3
    1 3 
    1 4 
    1 3

    样例输出 Sample Output

    3
    -1
    3

    数据范围及提示 Data Size & Hint

    对于 30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q < 1,000; 
    对于 60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q < 1,000; 
    对于 100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q < 30,000,0 ≤ z ≤ 100,000。

    题解

    网上的题解都是树上倍增??蒟蒻不会写树上倍增,所以写了树链剖分用线段树维护了下过掉了。

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<cstring>
      4 #define maxn 10005
      5 #define maxm 50005
      6 #define inf 1<<29
      7 using namespace std;
      8 int n,m,ecnt,cnt,dcnt,ord,head[maxn],hed[maxn],siz[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],tree[maxn],a[maxn],jud[maxn];
      9 int minn[maxn<<2];
     10 struct edge{
     11     int u,v,nxt,w;
     12 }E[maxm],Ed[maxn<<1];
     13 void add(int u,int v,int w)
     14 {
     15     E[++ecnt].u=u;
     16     E[ecnt].v=v;
     17     E[ecnt].w=w;
     18     E[ecnt].nxt=head[u];
     19     head[u]=ecnt;
     20 }
     21 void added(int u,int v,int w)
     22 {
     23     Ed[++dcnt].v=v;
     24     Ed[dcnt].w=w;
     25     Ed[dcnt].nxt=hed[u];
     26     hed[u]=dcnt;
     27 }
     28 inline int read()
     29 {
     30     int ret(0);
     31     char ch=getchar();
     32     while(ch>'9'||ch<'0')ch=getchar();
     33     while(ch>='0'&&ch<='9')
     34     {
     35         ret=(ret<<1)+(ret<<3)+ch-'0';
     36         ch=getchar();
     37     }
     38     return ret;
     39 }
     40 void dfs(int x)
     41 {
     42     siz[x]=1;
     43     for(int i=hed[x] ; i ; i=Ed[i].nxt)
     44     {
     45         int v=Ed[i].v;
     46         if(fa[x]==v)continue;
     47         fa[v]=x;deep[v]=deep[x]+1;jud[v]=Ed[i].w;
     48         dfs(v);
     49         siz[x]+=siz[v];
     50         if(siz[son[x]]<siz[v])son[x]=v;
     51     }
     52 }
     53 void dfs2(int x,int tp)
     54 {
     55     top[x]=tp;tree[x]=++ord;a[tree[x]]=jud[x];
     56     if(son[x])dfs2(son[x],tp);
     57     for(int i=hed[x] ; i ; i=Ed[i].nxt )
     58     {
     59         int v=Ed[i].v;
     60         if(son[x]==v||fa[x]==v)continue;
     61         dfs2(v,v);
     62     }
     63 }
     64 #define lson o<<1,l,mid
     65 #define rson o<<1|1,mid+1,r
     66 void pushup(int o){minn[o]=min(minn[o<<1],minn[o<<1|1]);}
     67 void build(int o,int l,int r)
     68 {
     69     minn[o]=inf; 
     70     if(l==r){minn[o]=a[l];return ;}
     71     int mid=(l+r)>>1;
     72     build(lson);build(rson);
     73     pushup(o);
     74 }
     75 void update(int o,int l,int r,int x,int v)
     76 {
     77     if(l==r)
     78     {
     79         minn[o]=v;
     80         return ;
     81     }
     82     int mid=(l+r)>>1;
     83     if(x<=mid)update(lson,x,v);
     84     else update(rson,x,v);
     85     pushup(o);
     86 }
     87 int query(int o,int l,int r,int ql,int qr)
     88 {
     89     int ret=inf;
     90     if(ql<=l&&r<=qr)return minn[o];
     91     int mid=(l+r)>>1;
     92     if(ql<=mid)ret=min(ret,query(lson,ql,qr));
     93     if(qr>mid)ret=min(ret,query(rson,ql,qr));
     94     return ret;
     95 }
     96 int que(int x,int y)
     97 {
     98     int ret=inf;
     99     while(top[x]!=top[y])
    100     {
    101         if(deep[top[x]]<deep[top[y]])swap(x,y);
    102         ret=min(ret,query(1,1,n,tree[top[x]],tree[x]));
    103         x=fa[top[x]];
    104     }
    105     if(deep[x]>deep[y])swap(x,y);
    106     ret=min(ret,query(1,1,n,tree[x],tree[y]));
    107     return ret;
    108 }
    109 int lca(int x,int y)
    110 {
    111     while(top[x]!=top[y])
    112     {
    113         if(deep[top[x]]<deep[top[y]])swap(x,y);
    114         x=fa[top[x]];
    115     }
    116     return deep[x]<deep[y]?x:y;
    117 }
    118 int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    119 bool cmp(edge p,edge q){return p.w>q.w;}
    120 int main()
    121 {
    122     memset(minn,0x7f,sizeof(minn));
    123     int q,x,y,z;
    124     n=read();m=read();
    125     for(int i=1 ; i<=n ; ++i )
    126     {
    127         fa[i]=i;a[i]=inf;
    128     }
    129     for(int i=1 ; i<=m ; ++i )
    130     {
    131         x=read();y=read();z=read();
    132         add(x,y,z);
    133     }
    134     sort(E+1,E+1+ecnt,cmp);
    135     for(int i=1 ;i<=ecnt ; ++i)
    136     {
    137         int u=E[i].u;int v=E[i].v;int fx=find(u);int fy=find(v);
    138         if(fx!=fy){fa[fy]=fx;++cnt;added(u,v,E[i].w);added(v,u,E[i].w);}
    139         if(cnt==n-1)break;
    140     }
    141     memset(fa,0,sizeof(fa));
    142     dfs(1);
    143     dfs2(1,1);
    144     a[1]=inf;
    145     build(1,1,n);
    146     q=read();
    147     while(q--)
    148     {
    149         x=read();y=read();
    150         if(top[x]==0||top[y]==0){printf("-1
    ");continue;}
    151         int Lca=lca(x,y);
    152         int tmp=a[tree[Lca]];
    153         update(1,1,n,tree[Lca],inf);
    154         printf("%d
    ",que(x,y));
    155         update(1,1,n,tree[Lca],tmp);
    156     }
    157     return 0;
    158 }

    反思:基础板子不过关啊。。。导致今天前两题gg,要不是靠第三题估计就爆0了,还是好好练模板qwqqq

  • 相关阅读:
    CF516E Drazil and His Happy Friends
    洛谷P4228 [清华集训2017] 榕树之心
    洛谷P5404 [CTS2019] 重复
    洛谷P4229 [清华集训2017] 某位歌姬的故事
    CF1286E Fedya the Potter Strikes Back
    CF1239
    洛谷P5892 [IOI2014] holiday 假期
    AT5202 [AGC038E] Gachapon
    库默尔定理
    UOJ37 [清华集训2014] 主旋律
  • 原文地址:https://www.cnblogs.com/fujudge/p/7736214.html
Copyright © 2011-2022 走看看