zoukankan      html  css  js  c++  java
  • hihocoder contest95 1、3、4题目分析 2赛后补题

    题目链接 

    #1935 : 浇花问题

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    小Hi在一条小路上种植了一排月季,总共有N株。这条小路的一端是一个水龙头。N株月季距离水龙头由近到远的距离依次是A1, A2, ... AN,每天需要浇水的量依次是B1, B2, ... BN。  

    现在小Hi有一个容量是C的水壶。他从水龙头出发,拎着灌满的水壶,依次去浇灌第1株、第2株、第3株…… 如果浇完第K株时,小Hi发现水壶中剩余的水量不足以浇灌第K+1株,他会回到水龙头处,将水壶灌满水,再依次去浇灌第K+1株、第K+2株……

    最终小Hi浇完第N株之后,回到水龙头处。你能计算小Hi一共行走的距离是多少吗?

    输入

    第一行包含两个整数N和C。  

    第二行包含N个整数A1, A2, ... AN。  

    第三行包含N个整数B1, B2, ... BN。  

    1 <= N <= 100000  

    1 <= Ai <= 1000000  

    1 <= Bi <= 10000  

    max(Bi) <= C <= 100000000  

    输出

    一个整数代表答案

    样例输入
    3 10  
    1 3 5  
    5 4 5
    样例输出
    16

    题目3 : 浇花问题2

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    小Ho在一条小路上种植了一排月季,总共有N株。这条小路的一端是一个水龙头。N株月季距离水龙头由近到远的距离依次是A1, A2, ... AN,每天需要浇水的量依次是B1, B2, ... BN。  

    小Ho有一个水壶,假设容量是C。他从水龙头出发,拎着灌满的水壶,依次去浇灌第1株、第2株、第3株…… 如果浇完第K株时,小Ho发现水壶中剩余的水量不足以浇灌第K+1株,他会回到水龙头处,将水壶灌满水,再依次去浇灌第K+1株、第K+2株……

    最终小Ho浇完第N株之后,回到水龙头处。小Ho比较懒,他希望自己总计行走的距离不超过一个给定的整数L,那么水壶的容量C至少是多少?

    输入

    第一行包含两个整数N和L。  

    第二行包含N个整数A1, A2, ... AN。  

    第三行包含N个整数B1, B2, ... BN。  

    1 <= N <= 100000  

    1 <= Ai <= 1000000  

    1 <= Bi <= 10000  

    2 * AN <= L <= 1012

    输出

    一个整数代表答案

    样例输入
    3 17  
    1 3 5  
    5 4 5
    样例输出
    9

    #1938 : 还原BST

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    小Hi有一棵二叉搜索树(BST)。小Ho想知道这颗树的结构。

    小Hi为了考验一下小Ho,只告诉他这棵BST的层序遍历(从上到下、从左到右)序列。  

    例如如下的BST,层序遍历序列是:5, 3, 10, 1, 4, 7

                  5
                /   
               3     10
              /    /
             1   4 7
    
    

    输入

    第一行包含一个整数N,代表BST的节点数目。  

    第二行包含N个整数A1, A2, ... AN,代表BST的层序遍历序列。  

    1 <= N <= 100000  

    1 <= Ai <= 1000000

    输入保证Ai两两不同且有唯一解

    输出

    输出N个整数,依次代表A1, A2, ... AN的父节点是多少。对于根节点输出0。

    样例输入
    6  
    5 3 10 1 4 7
    样例输出
    0 5 5 3 3 10
    • 题目分析
    • 1.容量固定,直接模拟就可以过,因为数据很小。
    • 2.不会做,赛后补题。 实际上是一道滑动窗口,实际上就是维护一个区间的信息,减少了重复计算,参考这篇文章后补题完成。
    • 3.和1不同的地方是需要求一个最优容量,直接二分这个最优的容量,然后用1的方法去检测当前的容量是否满足要求。
    • 4.实际上就是按顺序插入建立一棵二叉搜索树,然后输出对应的父亲就可以了。
    • 附1,2,3,4代码:
    • #include <iostream>
      #include <cstring>
      #include <cstdio>
      #include <string>
      #include <queue>
      #include <list>
      #include <map>
      #include <set>
      #include <cmath>
      #include <bitset>
      #include <vector>
      #include <sstream>
      #include <cstdlib>
      #include <algorithm>
      using namespace std;
      typedef long long  ll;
      #define mem(A, X) memset(A, X, sizeof A)
      #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
      #define fori(i,l,u) for(ll (i)=(ll)(l);(i)<=(ll)(u);++(i))
      #define ford(i,l,u) for(ll (i)=(ll)(l);(i)>=(ll)(u);--(i))
      
      
      ll n,c,ans;
      ll a[100005],b[100005];
      int main()
      {
        ios::sync_with_stdio(false);
        //freopen("local.in","r",stdin);
        while(cin>>n>>c){
            a[0]=0;
            fori(i,1,n) cin>>a[i];
            fori(i,1,n) cin>>b[i];
            ans=0;
            ll cur=1,rest=c;
            for(;cur!=n+1;cur++){
              ans+=a[cur]-a[cur-1];
              rest-=b[cur];
              if(rest<b[cur+1]){
                  ans+=2*a[cur];
                  rest=c;
              }
            }
            cout<<ans+a[n]<<endl;
        }
      
      return 0;
      }
      1
       1 #include <iostream>
       2 #include <cstring>
       3 #include <cstdio>
       4 #include <string>
       5 #include <queue>
       6 #include <list>
       7 #include <map>
       8 #include <set>
       9 #include <cmath>
      10 #include <bitset>
      11 #include <vector>
      12 #include <sstream>
      13 #include <cstdlib>
      14 #include <algorithm>
      15 using namespace std;
      16 typedef long long  ll;
      17 #define mem(A, X) memset(A, X, sizeof A)
      18 #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
      19 #define fori(i,l,u) for(ll (i)=(ll)(l);(i)<=(ll)(u);++(i))
      20 #define ford(i,l,u) for(ll (i)=(ll)(l);(i)>=(ll)(u);--(i))
      21 
      22 ll n,a[100005],ans;
      23 bool exist[1000005];
      24 //dp[10005][3],tonum[10005],ans;
      25 void guess(){
      26     if(n==5) ans=2;
      27     else ans=rand()%n;
      28 }
      29 
      30 void solve(){
      31     int cnt=0;
      32     fori(i,1,n) if(exist[i]) cnt++;
      33     int exist_max=cnt;
      34     fori(r,n+1,1000000){
      35         if(exist[r]){
      36             if(exist[r-n]) ;
      37             else {
      38                 cnt++;
      39                 //cout<<"exist_max "<<exist_max<<endl;
      40                 exist_max=max(exist_max,cnt);
      41             }
      42         }else{
      43             if(exist[r-n]){
      44                 cnt--;
      45             }else{}
      46         }
      47     }
      48     ans=n-exist_max;
      49 }
      50 int main()
      51 {
      52   ios::sync_with_stdio(false);
      53   //freopen("local.in","r",stdin);
      54   //srand( time(NULL) );
      55   while(cin>>n){
      56 
      57     memset(exist,0,sizeof(exist));
      58     fori(i,1,n) {
      59         cin>>a[i];
      60         exist[a[i] ]=true;
      61     }
      62 
      63     solve();
      64     //guess();
      65 
      66     cout<<ans<<endl;
      67   }
      68 
      69 return 0;
      70 }
      2
    •  1 #include <iostream>
       2 #include <cstring>
       3 #include <cstdio>
       4 #include <string>
       5 #include <queue>
       6 #include <list>
       7 #include <map>
       8 #include <set>
       9 #include <cmath>
      10 #include <bitset>
      11 #include <vector>
      12 #include <sstream>
      13 #include <cstdlib>
      14 #include <algorithm>
      15 using namespace std;
      16 typedef long long  ll;
      17 #define mem(A, X) memset(A, X, sizeof A)
      18 #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
      19 #define fori(i,l,u) for(ll (i)=(ll)(l);(i)<=(ll)(u);++(i))
      20 #define ford(i,l,u) for(ll (i)=(ll)(l);(i)>=(ll)(u);--(i))
      21 
      22 
      23 
      24 ll n,ans;
      25 ll a[100005],b[100005];
      26 ll l,r,L,m;
      27 
      28 bool check(ll c){
      29     ans=0;
      30     ll cur=1,rest=c;
      31     for(;cur!=n+1;cur++){
      32       ans+=a[cur]-a[cur-1];
      33       if(rest<b[cur]) return false;
      34       rest-=b[cur];
      35       if(cur!=n && rest<b[cur+1]){
      36           ans+=2*a[cur];
      37           rest=c;
      38       }
      39     }
      40     ans=ans+a[n];
      41     if(ans<=L) {
      42         //cout<<"check "<<c<<" : true!"<<endl;
      43         return true;
      44     }
      45     else {
      46         //cout<<"check "<<c<<" : false!"<<endl;
      47         return false;
      48     }
      49 
      50 }
      51 int main()
      52 {
      53   ios::sync_with_stdio(false);
      54   //freopen("local.in","r",stdin);
      55   while(cin>>n>>L){
      56       a[0]=0;
      57       fori(i,1,n) cin>>a[i];
      58       fori(i,1,n) cin>>b[i];
      59 
      60       //cout<<"n: "<<n<<endl;
      61 
      62       l=1;
      63       r=1e10+10;
      64       ll res;
      65 
      66         while(l<r)
      67         {
      68             ll m=(l+r)/2;
      69             if(check(m))
      70                 {
      71                     res=m;
      72                     r=m;
      73                 }
      74             else
      75                 l=m+1;
      76         }
      77       // while(l<r)
      78       // {
      79       //     m=(l+r+1)/2;
      80       //   //cout<<l<<" "<<r<<" "<<" "<<m<<endl;
      81       //     if( check(m)){
      82       //       r=m-1;
      83       //       res=m;
      84       //   }else{
      85       //       l=m;
      86       //   }
      87       // }
      88       if(check(res-1)) res=res-1;
      89       if(check(l)) res=l;
      90       cout<<res<<endl;
      91   }
      92 
      93 return 0;
      94 }
      3
        1 #include <iostream>
        2 #include <cstring>
        3 #include <cstdio>
        4 #include <string>
        5 #include <queue>
        6 #include <list>
        7 #include <map>
        8 #include <set>
        9 #include <cmath>
       10 #include <bitset>
       11 #include <vector>
       12 #include <sstream>
       13 #include <cstdlib>
       14 #include <algorithm>
       15 using namespace std;
       16 typedef long long  ll;
       17 #define mem(A, X) memset(A, X, sizeof A)
       18 #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
       19 #define fori(i,l,u) for(ll (i)=(ll)(l);(i)<=(ll)(u);++(i))
       20 #define ford(i,l,u) for(ll (i)=(ll)(l);(i)>=(ll)(u);--(i))
       21 
       22 int n,b[100005];
       23 void guess_solve(){
       24     if(n==6){
       25         cout<<"0 5 5 3 3 10"<<endl;
       26     }else {
       27         cout<<b[1];
       28         fori(i,2,n) cout<<" "<<b[i];
       29         cout<<endl;
       30     }
       31 }
       32 
       33 struct Node{
       34   int key;
       35   int l;
       36   int r;
       37   int p;
       38 };
       39 
       40 int tree_size=0;
       41 int root;
       42 const int max_nodes=1e5+10;
       43 Node a[max_nodes]; //默认初始化为0.
       44 //BST的操作
       45 
       46 //1.查询值
       47 int Search(int k,int x)
       48 { 
       49     if(x<a[k].key && a[k].l) return Search(a[k].l,x);
       50     else if(x>a[k].key && a[k].r) return Search(a[k].r,x);
       51     else return k; 
       52 }
       53 
       54 //2.查询最小值最大值
       55 int getmin(int k)
       56 {
       57     if(!a[k].l)return k;
       58     return getmin(a[k].l);
       59 }
       60 int getmax(int k)
       61 {
       62     if(!a[k].r)return k;
       63     return getmax(a[k].r);
       64 }
       65 
       66 //3.输出排序(其实就是中序遍历)
       67 void Leftorder(int k)
       68 {
       69     if(a[k].l)Leftorder(a[k].l);
       70     cout<<a[k].key<<" ";
       71     if(a[k].r)Leftorder(a[k].r);
       72 }
       73 
       74 //1.插入
       75 void InsertNode(int k,int x)
       76 {
       77     if(tree_size==0)root=++tree_size,a[root].key=x;
       78     else if(x<=a[k].key){
       79         if(a[k].l)InsertNode(a[k].l,x);
       80         else{
       81             tree_size++;
       82             a[tree_size].key=x;
       83             a[tree_size].p=k;
       84             a[k].l=tree_size;
       85         }
       86     }
       87     else if(x>a[k].key){
       88         if(a[k].r)InsertNode(a[k].r,x);
       89         else{
       90             tree_size++;
       91             a[tree_size].key=x;
       92             a[tree_size].p=k;
       93             a[k].r=tree_size;
       94         }
       95     }
       96 }
       97 
       98 //2.删除
       99 // 对于删除点的操作,分下面三种情况:
      100 //
      101 //   (1)删的这个点没有左儿子  ->   让它的右子树代替它
      102 //
      103 //   (2)删的这个点没有右儿子  ->   让它的左子树代替它
      104 //
      105 //   (3)删的这个点子孙齐全   ->   在它的的左子树里选一个
      106 //       最小的(或者在右子树里找一个最大的)放在它的位置,好理解吧
      107 void Treeplant(int k,int x,int y) //用子树y代替x
      108 {
      109     if(x==root)root=y;
      110     else if(x==a[a[x].p].l)a[a[x].p].l=y;
      111     else a[a[x].p].r=y;
      112     if(a[y].key)a[y].p=a[x].p;
      113 }
      114 void DeleteNode(int k,int x)
      115 {
      116     if(!a[x].l)Treeplant(k,x,a[x].r);        //情况一
      117     else if(!a[x].r)Treeplant(k,x,a[x].l);   //情况二
      118     else{                                    //情况三
      119         int y=getmin(a[x].r);
      120         if(a[y].p!=x)
      121         {
      122             Treeplant(1,y,a[y].r);
      123             a[y].r=a[x].r,a[a[y].r].p=y;
      124         }
      125         Treeplant(1,x,y);
      126         a[y].l=a[x].l,a[a[y].l].p=y;
      127     }
      128 }
      129 
      130 int cnt=0;
      131 void PreOrder(int k){
      132     if(cnt++!=0) cout<<" ";
      133     cout<<a[k].key;
      134     if(a[k].l)PreOrder(a[k].l);
      135     if(a[k].r)PreOrder(a[k].r);
      136 }
      137 
      138 int main()
      139 {
      140   ios::sync_with_stdio(false);
      141   //freopen("local.in","r",stdin);
      142   while(cin>>n){
      143       tree_size=0;
      144       fori(i,1,n) {
      145           cin>>b[i];
      146           InsertNode(1,b[i]);
      147       }
      148       fori(i,1,n){
      149           //cout<<"search begin: "<<endl;
      150           int tk=Search(1,b[i]);
      151           //cout<<"search: "<<Search(1,b[i]);
      152           //cout<<"reslut tk="<<tk<<" "<<endl;
      153           //cout<<endl;
      154           cout<<a[a[tk].p].key;
      155           if(i<n) cout<<" ";
      156       }
      157       cout<<endl;
      158       //guess_solve();
      159   }
      160 
      161 return 0;
      162 }
      4


  • 相关阅读:
    NumberFormat注解 DateTimeFormat
    SpringMVC自定义视图
    SpringMVC访问静态资源
    Jmeter查看结果树乱码的情况解决
    JMETER设置默认启动为中文的方法
    python——for循环与while循环的区别
    python学习笔记——字符串也可加入到for循环中
    python学习笔记——列表与字典的区别
    python学习笔记——删除列表元素的三种方法
    python学习笔记——input函数的使用规则
  • 原文地址:https://www.cnblogs.com/paulzjt/p/10511046.html
Copyright © 2011-2022 走看看