• Codeforces 474(#271 Div 2) 解题报告


    A:直接贴过来装换

    解题代码

     1 // File Name: a.cpp
     2 // Author: darkdream
     3 // Created Time: 2014年10月06日 星期一 23时28分57秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #define LL long long
    25 
    26 using namespace std;
    27 char  str[102];
    28 char  tp[4][100] = {"qwertyuiop",
    29 "asdfghjkl;",
    30 "zxcvbnm,./",
    31 };
    32 int main(){
    33     scanf("%s",str);
    34     if(str[0] == 'L')
    35     {
    36        scanf("%s",str);
    37        for(int i = 0;i < strlen(str);i ++)
    38        {
    39             for(int j = 0;j <= 2;j ++)
    40             {
    41                if(strchr(tp[j],str[i]))
    42                {
    43                  printf("%c",tp[j][strchr(tp[j],str[i]) - tp[j] +1]);
    44                }
    45             }
    46        }
    47     }
    48   else{
    49        scanf("%s",str);
    50        for(int i = 0;i < strlen(str);i ++)
    51        {
    52             for(int j = 0;j <= 2;j ++)
    53             {
    54                if(strchr(tp[j],str[i]))
    55                {
    56                  printf("%c",tp[j][strchr(tp[j],str[i]) - tp[j] - 1]);
    57                }
    58             }
    59        
    60        }
    61   }
    62 return 0;
    63 }
    View Code

    B:一个标记或者二分就行,最大1e6的复杂度

     1 // File Name: b.cpp
     2 // Author: darkdream
     3 // Created Time: 2014年10月06日 星期一 23时39分31秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #define LL long long
    25 
    26 using namespace std;
    27 int a[100005];
    28 int b[100005];
    29 int n , m ; 
    30 int find(int x)
    31 {
    32     int l = 1; 
    33     int r = n;
    34     while(l <= r)
    35     {
    36       int mid = (l + r)/2;
    37       if(a[mid] < x)
    38       {
    39         l = mid + 1;
    40       }else {
    41         r = mid - 1;
    42       }
    43     }
    44     return l;
    45 }
    46 int main(){
    47   scanf("%d",&n);
    48   for(int i =1 ;i <= n;i ++)
    49   {      scanf("%d",&a[i]);
    50       a[i] = a[i-1] + a[i];
    51   }
    52   scanf("%d",&m);
    53   for(int i =1 ;i <= m;i ++)
    54   {
    55       scanf("%d",&b[i]);
    56       printf("%d
    ",find(b[i]));
    57   }
    58 return 0;
    59 }
    View Code

    C:知道转换公式以后就是一个手速题

      1 // File Name: c.cpp
      2 // Author: darkdream
      3 // Created Time: 2014年10月07日 星期二 00时41分28秒
      4 
      5 #include<vector>
      6 #include<list>
      7 #include<map>
      8 #include<set>
      9 #include<deque>
     10 #include<stack>
     11 #include<bitset>
     12 #include<algorithm>
     13 #include<functional>
     14 #include<numeric>
     15 #include<utility>
     16 #include<sstream>
     17 #include<iostream>
     18 #include<iomanip>
     19 #include<cstdio>
     20 #include<cmath>
     21 #include<cstdlib>
     22 #include<cstring>
     23 #include<ctime>
     24 #define LL long long
     25 #define eps 1e-8
     26 using namespace std;
     27 struct node{
     28     double x,y,a,b;
     29 };
     30 node a[5][5];
     31 node  change(node tp)
     32 {
     33    node ans;
     34    ans.a = tp.a;
     35    ans.b = tp.b;
     36    ans.x = tp.a - (tp.y - tp.b);
     37    ans.y = tp.b + (tp.x - tp.a);
     38    return ans;
     39 }
     40 int ans[0];
     41 double dis[5][5];
     42 double thedis(double x1, double y1, double x2,double y2)
     43 {
     44   return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) ;
     45 }
     46 int mi; 
     47 bool ok()
     48 {
     49    node tt[5];
     50    for(int i = 1;i <= 4;i ++)
     51    {
     52       tt[i] = a[i][ans[i]];
     53    }
     54    for(int i = 1;i <= 4;i ++)
     55    {
     56        for(int j= 1;j <= 4 ; j ++)
     57        {
     58             dis[i][j] = thedis(tt[i].x,tt[i].y,tt[j].x,tt[j].y);
     59        }
     60      sort(dis[i] + 1,dis[i] + 1 + 4);
     61    }
     62    for(int i = 2;i <= 4;i ++)
     63        for(int j = 1;j <= 4; j ++)
     64        {
     65           if(fabs(dis[i][j] - dis[i-1][j]) > eps)
     66               return 0 ; 
     67        }
     68    if(fabs(dis[1][4]-0 ) <eps)
     69        return 0 ; 
     70    if(fabs(dis[1][3] - dis[1][2]) > eps)
     71        return 0 ;
     72    if(fabs(dis[1][3] * sqrt(2) - dis[1][4]) > eps)
     73        return 0 ;
     74    if(fabs(dis[1][3] - 0 ) < eps)
     75        return 0 ;
     76    if(fabs(dis[1][2] - 0 ) < eps)
     77        return 0 ;
     78    /*for(int i = 1;i <= 4;i ++)
     79    {
     80        for(int j = 1;j <= 4; j ++)
     81            printf("%lf ",dis[i][j]);
     82        printf("
    ");
     83    }
     84    for(int i = 1;i<= 4;i ++)
     85        printf("%lf %lf
    ",tt[i].x,tt[i].y);
     86    
     87        */
     88    return 1;
     89 }
     90 void dfs(int k)
     91 {
     92    if(k == 5)
     93    {
     94       if(ok())
     95       {
     96         if(ans[1] + ans[2] + ans[3] + ans[4]  - 4< mi)
     97            mi = ans[1] + ans[2] + ans[3] + ans[4] -4;
     98         //printf("%d %d %d %d
    ",ans[1],ans[2],ans[3],ans[4]);
     99       }
    100       return;
    101    }
    102    for(int i = 1;i <= 4;i ++)
    103    {
    104       ans[k] = i ;
    105       dfs(k+1);
    106    }
    107 }
    108 int main(){
    109    int n; 
    110    scanf("%d",&n);
    111    while(n--)
    112    {
    113        for(int i= 1;i <= 4;i ++)
    114           scanf("%lf %lf %lf %lf",&a[i][1].x,&a[i][1].y,&a[i][1].a,&a[i][1].b);
    115        for(int i = 1;i<= 4;i ++ ) 
    116        {
    117           for(int j = 2;j <= 4;j ++)
    118               a[i][j] = change(a[i][j-1]);
    119        }
    120       /* for(int i = 1;i<= 4;i ++ ) 
    121        {
    122           for(int j = 1;j <= 4;j ++)
    123               printf("%lf %lf ** ",a[i][j].x,a[i][j].y);
    124           printf("
    ");
    125        }*/
    126       mi = 1000; 
    127       dfs(1);
    128       if(mi == 1000)
    129           printf("-1
    ");
    130       else printf("%d
    ",mi);
    131    }
    132 return 0;
    133 }
    View Code

    D: dp,考虑最后这一位是R还是W即可

     1 // File Name: d.cpp
     2 // Author: darkdream
     3 // Created Time: 2014年10月07日 星期二 00时21分01秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #define LL long long
    25 #define maxn 100004
    26 #define M 1000000007
    27 using namespace std;
    28 int t, k ;
    29 
    30 LL ans[maxn][2];
    31 LL sum[maxn];
    32 int main(){
    33   while(scanf("%d %d",&t,&k) != EOF)
    34   {
    35      ans[1][1] = 1;
    36      ans[0][2] = 1; 
    37      
    38      if(k == 1 )
    39        ans[1][2] = 1;
    40      else ans[1][2] = 0 ; 
    41      sum[0] = 0 ;
    42      sum[1] = ans[1][1] + ans[1][2];
    43      for(int i = 2; i <= 100000;i ++)
    44      {
    45          ans[i][1] = (ans[i-1][1] + ans[i-1][2])%M ;
    46          if(i >= k )
    47            ans[i][2] = (ans[i-k][1] + ans[i-k][2])%M;
    48          else{
    49            ans[i][2] = 0 ; 
    50          }
    51          sum[i] = (sum[i-1] + ans[i][1] + ans[i][2])%M;
    52     //    printf("%I64d
    ",sum[i]);
    53      }
    54      int a, b; 
    55      for(int i =1 ;i <= t;i ++)
    56      {
    57        scanf("%d %d",&a,&b);
    58        printf("%I64d
    ",((sum[b] - sum[a-1])+ M)%M );
    59      }
    60      
    61   }
    62 return 0;
    63 }
    View Code

    E:离散化,二分找端点值,线段树查找最大值

      1 // File Name: e.cpp
      2 // Author: darkdream
      3 // Created Time: 2014年10月07日 星期二 01时29分07秒
      4 
      5 #include<vector>
      6 #include<list>
      7 #include<map>
      8 #include<set>
      9 #include<deque>
     10 #include<stack>
     11 #include<bitset>
     12 #include<algorithm>
     13 #include<functional>
     14 #include<numeric>
     15 #include<utility>
     16 #include<sstream>
     17 #include<iostream>
     18 #include<iomanip>
     19 #include<cstdio>
     20 #include<cmath>
     21 #include<cstdlib>
     22 #include<cstring>
     23 #include<ctime>
     24 #define LL long long
     25 #define maxn 100005
     26 using namespace std;
     27 LL a[maxn];
     28 LL b[maxn];
     29 int la[maxn];
     30 int dp[maxn];
     31 map <LL,int> mp;
     32 struct node{
     33    int l ,r; 
     34    int mx;
     35    int site;; 
     36 }tree[maxn << 2];
     37 int L(int x)
     38 {
     39   return 2 * x;
     40 }
     41 int R(int x)
     42 {
     43   return  2 * x + 1 ;
     44 }
     45 void build(int c, int l , int r)
     46 {
     47     tree[c].l =  l ; 
     48     tree[c].r = r; 
     49     tree[c].mx = 0 ; 
     50     tree[c].site = 0 ; 
     51     if(l == r)
     52     {
     53        return ; 
     54     }
     55     build(L(c),l,(l+r)/2);
     56     build(R(c),(l+r)/2+1,r);
     57 }
     58 void pushup(int c)
     59 {
     60     if(tree[L(c)].mx > tree[R(c)].mx)
     61     {
     62         tree[c].mx = tree[L(c)].mx; 
     63         tree[c].site = tree[L(c)].site;
     64     }else {
     65         tree[c].mx = tree[R(c)].mx; 
     66         tree[c].site = tree[R(c)].site;
     67     }
     68 }
     69 void update(int c, int k,int st,int v )
     70 {
     71     if(tree[c].l == tree[c].r  && tree[c].l == k )
     72     {
     73         tree[c].mx = v ; 
     74         tree[c].site = st ; 
     75         return ;
     76     }
     77     if(k <= (tree[c].l + tree[c].r)/2)
     78     {
     79         update(L(c),k,st,v);
     80     }else {
     81         update(R(c),k,st,v);
     82     }
     83     pushup(c);
     84 }
     85 int mx ;
     86 int site; 
     87 void query(int c, int l ,int r)
     88 {
     89       if(tree[c].l>= l && tree[c].r <= r)
     90       {
     91          if(tree[c].mx > mx )
     92          {
     93             mx = tree[c].mx;
     94             site = tree[c].site ; 
     95          }
     96          return;
     97       }
     98       int m = (tree[c].l + tree[c].r) /2;
     99       if(l <= m)
    100          query(L(c),l,r);
    101       if(r > m)
    102          query(R(c),l,r);
    103 }
    104 int n , d; 
    105 int find(LL x)
    106 {
    107     int l = 1; 
    108     int r = n; 
    109     while(l <= r )
    110     {
    111        int m = (l + r)/2;
    112        if(b[m] <= x)
    113        {
    114           l = m + 1; 
    115        }else {
    116           r = m - 1;
    117        }
    118     }
    119     return l;
    120 }
    121 void solve(int k )
    122 {
    123    if(k == 0 )
    124        return;
    125    solve(la[k]);
    126    printf("%d ",k);
    127 }
    128 int main(){
    129     scanf("%d %d",&n,&d);
    130     for(int i =1 ;i <= n;i ++)
    131     {
    132         scanf("%I64d",&a[i]);
    133         b[i] = a[i];
    134     }
    135     sort(b+1,b+1+n);
    136     mp.clear();
    137     b[0] = 0 ; 
    138     int num = 0 ; ;
    139     for(int i = 1;i <= n;i ++)
    140     {
    141         if(b[i] != b[i-1])
    142         {
    143             num ++ ;
    144              mp[b[i]] = num;
    145         }
    146     }
    147     build(1,1,num);
    148     //printf("****%d
    ",num);
    149     int p = 0 ;
    150     int pmx =0; 
    151     for(int i =1;i <= n;i ++)
    152     {
    153        int t = mp[a[i]];
    154        int l = find(a[i] - d);
    155        int r = find(a[i] + d - 1);
    156        
    157        mx = 0; 
    158        site = 0 ;
    159        if(l != 1)
    160        {
    161          //printf("mp[b[l-1]] = %d
    ",mp[b[l-1]]);
    162          query(1,1,mp[b[l-1]]);
    163        }
    164        if(r != n + 1)
    165        {
    166           //printf("mp[b[r]] = %d %d
    ",mp[b[r]],r);
    167           query(1,mp[b[r]],num);
    168        }
    169        la[i] = site;
    170        //if(mx != 0 || i == 1)
    171           mx ++ ; 
    172        
    173        dp[i] = mx;
    174        if(mx > pmx)
    175        {
    176          pmx = mx ; 
    177          p = i ;    
    178        }
    179        //printf("%d %d %d
    ",t,i,mx);
    180        update(1,t,i,mx); 
    181       // printf("%d %d %d %d
    ",t,mx,site,tree[1].mx);
    182     }
    183     
    184     printf("%d
    ",pmx);
    185     solve(p);
    186     /*printf("
    ");
    187     for(int i = 1;i <= n;i ++)
    188         printf("%d ",la[i]);
    189     
    190         */
    191     return 0;
    192 }
    View Code

    F:用线段树统计 区间 gcd  ,新建一个结构体 储存  值和下标, 找到gcd 以后 用两次二分 找到 区间内 gcd 值的个数即可

      1 // File Name: f.cpp
      2 // Author: darkdream
      3 // Created Time: 2014年10月07日 星期二 21时25分35秒
      4 
      5 #include<vector>
      6 #include<list>
      7 #include<map>
      8 #include<set>
      9 #include<deque>
     10 #include<stack>
     11 #include<bitset>
     12 #include<algorithm>
     13 #include<functional>
     14 #include<numeric>
     15 #include<utility>
     16 #include<sstream>
     17 #include<iostream>
     18 #include<iomanip>
     19 #include<cstdio>
     20 #include<cmath>
     21 #include<cstdlib>
     22 #include<cstring>
     23 #include<ctime>
     24 #define LL long long
     25 #define maxn 100005
     26 using namespace std;
     27 struct node{
     28    int l , r ; 
     29    int gcd;
     30 }tree[maxn << 2];
     31 int a[maxn];
     32 struct node1{
     33     int v,p; 
     34 }b[maxn];
     35 int gcd(int x, int y )
     36 {
     37    return y == 0 ? x:gcd(y,x%y);
     38 }
     39 int L(int x)
     40 {
     41    return 2 * x ; 
     42 }
     43 int R(int x)
     44 {
     45    return 2 * x + 1 ; 
     46 }
     47 void pushup(int c)
     48 {
     49    tree[c].gcd = gcd(tree[L(c)].gcd,tree[R(c)].gcd);
     50 }
     51 void build(int c, int l , int r)
     52 {
     53    tree[c].l = l ;
     54    tree[c].r = r;
     55    //printf("%d %d %d
    ",c,l,r);
     56    if(l == r)
     57    {
     58       tree[c].gcd = a[l];
     59       return ;
     60    }
     61    int m = (l + r)/2;
     62    build(L(c),l,m);
     63    build(R(c),m+1,r);
     64    pushup(c);
     65 }
     66 int cmp(node1 a, node1 b)
     67 {
     68   if(a.v == b.v)
     69   {
     70     return a.p < b.p ;
     71   }
     72   return a.v < b.v ; 
     73 }
     74 int ans = 0 ;
     75 
     76 void query(int c, int l, int r)
     77 {
     78     //printf("%d %d %d %d %d 
    ",c,l,r,tree[c].l ,tree[c].r);
     79     if(tree[c].l >= l && tree[c].r <= r)
     80     {
     81          ans = gcd(ans,tree[c].gcd);
     82          return ;
     83     }
     84     int m = (tree[c].l + tree[c].r)/2;
     85     if(l <= m)
     86          query(L(c),l,r);
     87     if(r > m )
     88          query(R(c),l,r);
     89 }
     90 int n ;
     91 int  find(int x)
     92 {
     93     int l = 1; 
     94     int r = n ; 
     95     while(l <= r)
     96     {
     97       int m = (l +r )/2;
     98       if(b[m].v <= x)
     99       {
    100          l = m + 1;  ; 
    101       }else {
    102          r = m - 1; 
    103       }
    104     }
    105     return l ; 
    106 }
    107 int ll, rr ;
    108 int find1(int x)
    109 {
    110     int l = ll ;
    111     int r = rr ; 
    112     while(l <= r)
    113     {
    114       int m = (l +r )/2;
    115       if(b[m].p <= x)
    116       {
    117          l = m + 1;  ; 
    118       }else {
    119          r = m - 1; 
    120       }
    121      }
    122    return l ;
    123 }
    124 int main(){
    125    scanf("%d",&n);
    126    for(int i =1;i <= n;i ++)
    127    {
    128       scanf("%d",&a[i]);
    129       b[i].v = a[i];
    130       b[i].p = i;
    131    }
    132    build(1,1,n);
    133    sort(b+1,b+n+1,cmp);
    134    //for(int i =1 ;i <= n;i ++)
    135      //  printf("%d %d
    ",b[i].v,b[i].p);
    136    int m ; 
    137    scanf("%d",&m);
    138    for(int i = 1;i <= m;i ++)
    139    {
    140      int l , r ; 
    141      scanf("%d %d",&l,&r);
    142      ans = a[l];
    143      query(1,l,r);
    144      //printf("%d
    ",ans);
    145      ll = find(ans-1);
    146      if(b[ll].v != ans)
    147      {
    148          printf("%d
    ",r-l+1);
    149          continue;
    150      }else{
    151          rr = find(ans);
    152          rr --;
    153          int x, y ; 
    154          x = find1(l-1);
    155          if(b[x].p > r)
    156          {
    157             printf("%d
    ",r-l+1);
    158             continue;
    159          }else{
    160             y = find1(r);
    161             y -- ;
    162             printf("%d
    ",r-l+1 - (y-x+1));
    163          } 
    164      }
    165 
    166    }
    167 return 0;
    168 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    DP问题之最长非降子序列
    CentOS 6.8 编译安装MySQL5.5.32
    [Linux] killall 、kill 、pkill 命令详解
    编写登陆接口
    python学习day01
    python购物车程序
    ERROR:Attempting to call cordova.exec() before 'deviceready'
    BSF脚本引擎‘改变’Bean
    Solr安装配置
    amchart配置备忘
  • 原文地址:https://www.cnblogs.com/zyue/p/4009555.html
Copyright © 2020-2023  润新知