zoukankan      html  css  js  c++  java
  • Codeforces709

    A

    Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.

    The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?

    Input

    The first line of the input contains three integers nb and d (1 ≤ n ≤ 100 000, 1 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000) — sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.

    Output

    Print one integer — the number of times Kolya will have to empty the waste section.

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<math.h>
     9 #include<vector>
    10 #include<map>
    11 #include<set>
    12 #define il inline
    13 #define re register
    14 using namespace std;
    15 int n,b,d,a[1000001],c,ans;
    16 int main(){
    17     scanf("%d%d%d",&n,&b,&d);
    18     for(int i=1;i<=n;i++){
    19         scanf("%d",&a[i]);
    20         if(a[i]>b) continue;
    21         c+=a[i];
    22         if(c>d){
    23             c=0;ans++;
    24         }
    25     }
    26     cout<<ans;
    27     return 0;
    28 }

    B

    Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

    Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.

    Input

    The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000,  - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.

    The second line contains n integers x1, x2, ..., xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.

    Output

    Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<math.h>
     9 #include<vector>
    10 #include<map>
    11 #include<set>
    12 #define il inline
    13 #define re register
    14 using namespace std;
    15 int n,x,a[1000001];
    16 int main(){
    17     scanf("%d%d",&n,&x);
    18     for(int i=1;i<=n;i++)
    19         scanf("%d",&a[i]);
    20     sort(a+1,a+n+1);
    21     if(n==1){
    22         cout<<"0";return 0;
    23     }
    24     if(x<a[1]){
    25         cout<<abs(a[n-1]-x);return 0;
    26     }
    27     if(x>a[n]){
    28         cout<<abs(a[2]-x);return 0;
    29     }
    30     if(n==2){
    31         cout<<min(abs(a[1]-x),abs(a[n]-x));return 0;
    32     }
    33     cout<<min(min(abs(x-a[2]),abs(a[n]-x))+abs(a[2]-a[n]),min(abs(x-a[1]),abs(x-a[n-1]))+abs(a[1]-a[n-1]));
    34     return 0;
    35 }

    C

    You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z'  'y'  'x'  'b'  'a'  'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.

    What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?

    Input

    The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.

    Output

    Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<math.h>
     9 #include<vector>
    10 #include<map>
    11 #include<set>
    12 #define il inline
    13 #define re register
    14 using namespace std;
    15 char s[1000001];
    16 int n,a,b;
    17 il bool chk(){
    18     for(int i=1;i<=n;i++)
    19         if(s[i]!='a') return false;
    20     return true;
    21 }
    22 int main(){
    23     scanf("%s",s+1);n=strlen(s+1);
    24     if(chk()){
    25         for(int i=1;i<n;i++)
    26             printf("a");
    27         cout<<'z';return 0;
    28     }
    29     a=1;
    30     while(s[a]=='a') a++;
    31     for(int i=1;i<a;i++) printf("%c",s[i]);
    32     for(int i=a;i<=n;i++){
    33         if(s[i]!='a'){
    34             printf("%c",s[i]-1);
    35         }
    36         else{
    37             for(int j=i;j<=n;j++)
    38                 printf("%c",s[j]);
    39             return 0;
    40         }
    41     }
    42     return 0;
    43 }

    D

    For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number ofsubsequences of length 2 of the string s equal to the sequence {x, y}.

    In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than1 000 000.

    Input

    The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 10^9.

    Output

    If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.

    这个嘛,预处理要多少个1和0,然后01和10加起来的数目是固定的,因此满足一个就好了。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<math.h>
     9 #include<vector>
    10 #include<map>
    11 #include<set>
    12 #define il inline
    13 #define re register
    14 using namespace std;
    15 typedef long long ll;
    16 ll n=-1,a1,a2,a3,a4,s=-1,t=-1,a,p;
    17 int main(){
    18     cin>>a1>>a2>>a3>>a4;a=a1+a2+a3+a4;
    19     for(ll i=1;i<=1000000;i++){
    20         if(i*(i-1)/2==a){
    21             n=i;break;
    22         }
    23     }
    24     if(n==-1){
    25         cout<<"Impossible";return 0;
    26     }
    27     if(a1==a){
    28         for(int i=1;i<=n;i++)
    29             printf("0");
    30         return 0;
    31     }
    32     if(a4==a){
    33         for(int i=1;i<=n;i++)
    34             printf("1");
    35         return 0;
    36     }
    37     for(ll i=1;i<=1000000;i++){
    38         if(i*(i-1)/2==a1){
    39             s=i;break;
    40         }
    41     }
    42     for(ll i=1;i<=1000000;i++){
    43         if(i*(i-1)/2==a4){
    44             t=i;break;
    45         }
    46     }
    47     if(s==-1&&t==-1){
    48         cout<<"Impossible";return 0;
    49     }
    50     if(a2+a3!=s*t){
    51         cout<<"Impossible";return 0;
    52     }
    53     p=a2/s+(a2%s>0);
    54     if(p>t){
    55         cout<<"Impossible";return 0;
    56     }
    57     for(int i=1;i<=t-p;i++)
    58         printf("1");
    59     for(int i=1;i<=s;i++){
    60         printf("0");
    61         if(a2%s==i){
    62             printf("1");
    63         }
    64     }
    65     for(int i=1;i<=a2/s;i++)
    66         printf("1");
    67     return 0;
    68 }

     E

    Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed .

    You are given a tree of size n and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it's possible to make it centroid by performing no more than one edge replacement.

    Input

    The first line of the input contains an integer n (2 ≤ n ≤ 400 000) — the number of vertices in the tree. Each of the next n - 1 lines contains a pair of vertex indices ui and vi (1 ≤ ui, vi ≤ n) — endpoints of the corresponding edge.

    Output

    Print n integers. The i-th of them should be equal to 1 if the i-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise.

    如果有两个重心,则把重心边破开,把其中一边接到另外一边即可

    如果有一个重心,则找到重心最大的一个子树,把它断开和当前点接上。

    如果当前点在那个子树上,就和次大的接上。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<math.h>
     9 #include<vector>
    10 #include<map>
    11 #include<set>
    12 #define il inline
    13 #define re register
    14 using namespace std;
    15 const int N=1000001;
    16 struct edge{int next,to;
    17 } e[N];
    18 int g[N],n,M,cn,c[2],fir,sec,color[N],s[N];
    19 il void addedge(int x,int y){
    20     e[++M]=(edge){g[x],y};g[x]=M;
    21 }
    22 il void dfs1(int h,int fa){
    23     bool flag=true;
    24     for(int i=g[h];i;i=e[i].next){
    25         if(e[i].to==fa) continue;
    26         dfs1(e[i].to,h);
    27         if(s[e[i].to]>n/2) flag=false;
    28     }
    29     if(n-s[h]<=n/2&&flag==true){
    30         c[cn++]=h;
    31     }
    32 }
    33 il void dfs2(int h,int fa){
    34     s[h]=1;
    35     for(int i=g[h];i;i=e[i].next){
    36         if(fa==e[i].to||e[i].to==c[0]) continue;
    37         dfs2(e[i].to,h);s[h]+=s[e[i].to];
    38     }
    39 }
    40 il void dfs3(int h,int fa){
    41     for(int i=g[h];i;i=e[i].next){
    42         if(fa==e[i].to||e[i].to==c[0]) continue;
    43         color[e[i].to]=1;dfs3(e[i].to,h);
    44     }
    45 }
    46 il void predo(){
    47     dfs2(1,0);
    48     dfs1(1,0);
    49 }
    50 int main(){
    51     scanf("%d",&n);
    52     for(int i=1,x,y;i<n;i++){
    53         scanf("%d%d",&x,&y);
    54         addedge(x,y);
    55         addedge(y,x);
    56     }
    57     predo();
    58     if(cn==2){
    59         for(int i=1;i<=n;i++)
    60             printf("1 ");
    61         return 0;
    62     }
    63     dfs2(c[0],0);
    64     for(int j=g[c[0]];j;j=e[j].next){
    65         if(e[j].to==c[0]||e[j].to==c[1]) continue;
    66         if(s[e[j].to]>s[fir]){
    67             sec=fir;
    68             fir=e[j].to;
    69         }
    70         else if(s[e[j].to]>s[sec]){
    71             sec=e[j].to;
    72         }
    73     }
    74     color[fir]=1;dfs3(fir,0);
    75     for(int i=1;i<=n;i++){
    76         if(i==c[0]){
    77             printf("1 ");continue;
    78         }
    79         if(color[i]){
    80             if(n-s[sec]-s[i]<=n/2){
    81                 printf("1 ");
    82             }
    83             else printf("0 ");
    84         }
    85         else{
    86             if(n-s[fir]-s[i]<=n/2){
    87                 printf("1 ");
    88             }
    89             else{
    90                 printf("0 ");
    91             }
    92         }
    93     }
    94     return 0;
    95 }

     

  • 相关阅读:
    什么是首次适应算法,该算法的特点是什么?
    用上、下界防护方法是如何实现界地址保护?在硬件上需要什么支持?
    什么是存储保护?
    什么是静态地址重定位,它需要什么支持?什么是动态地址重定位,他需要什么支持?静态地址重定位与动态地址重定位有什么区别?
    什么是逻辑地址,什么是物理地址,为什么要进行二者的转换工作?
    三个进程共享四个同类资源,这些资源的分配与释放只能一次一个。已知每一个进程最多需要两个资源,试问,该系统会发生死锁吗?为什么?
    试举出一种一种避免死锁的发生的方法,并说明为什么能避免死锁的发生?
    产生死锁的原因是什么?产生死锁的必要条件是什么?
    什么是死锁,试举例说明?
    说出两种一笔调度的算法,并说出这两种移臂调度算法的定义。
  • 原文地址:https://www.cnblogs.com/ExiledPoet/p/5806974.html
Copyright © 2011-2022 走看看