zoukankan      html  css  js  c++  java
  • 浙南联合训练赛20180214

    这场比赛题目还行,但是被一道数学题卡了

    A - Supermarket

     CodeForces - 919A 

    We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a / b yuan for a kilo.

    Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.

    You can assume that there are enough apples in all supermarkets.

    Input

    The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.

    The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.

    Output

    The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10 - 6.

    Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if .

    Example

    Input
    3 5
    1 2
    3 4
    1 3
    Output
    1.66666667
    Input
    2 1
    99 100
    98 99
    Output
    0.98989899

    Note

    In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5 / 3 yuan.

    In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98 / 99 yuan.

    找到比值最小的就行了

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+5;
    int main()
    {
        int n,m;
        cin>>n>>m;
        double mi=1e9;
        for(int i=0,x,y;i<n;i++)
        {
            cin>>x>>y;
            mi=min(mi,m*x*1.0/y);
        }
        printf("%12f",mi);
        return 0;
    }

    B - Eleven

     CodeForces - 918A 

    Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters.

    Her friend suggested that her name should only consist of uppercase and lowercase letters 'O'. More precisely, they suggested that the i-th letter of her name should be 'O' (uppercase) if i is a member of Fibonacci sequence, and 'o' (lowercase) otherwise. The letters in the name are numbered from 1 to n. Fibonacci sequence is the sequence f where

    • f1 = 1,
    • f2 = 1,
    • fn = fn - 2 + fn - 1 (n > 2).

    As her friends are too young to know what Fibonacci sequence is, they asked you to help Eleven determine her new name.

    Input

    The first and only line of input contains an integer n (1 ≤ n ≤ 1000).

    Output

    Print Eleven's new name on the first and only line of output.

    Example

    Input
    8
    Output
    OOOoOooO
    Input
    15
    Output
    OOOoOooOooooOoo

    输出长度为n的字符串,如果是第i位是斐波那契数输出O

    #include<bits/stdc++.h>
    using namespace std;
    int a[1005],f[100],n;
    int main()
    {
        f[1]=1,f[0]=1;
        for(int i=1;f[i]<=1005;i++)
            f[i+1]=f[i]+f[i-1],a[f[i]]=1;
        cin>>n;
        for(int i=1;i<=n;i++)
            if(a[i]==1)cout<<"O";
            else cout<<"o";
        return 0;
    }

    C - The Way to Home

     CodeForces - 910A 

    A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.

    For each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1and n.

    Determine the minimal number of jumps that the frog needs to reach home which is in the point n from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.

    Input

    The first line contains two integers n and d (2 ≤ n ≤ 100, 1 ≤ d ≤ n - 1) — the point, which the frog wants to reach, and the maximal length of the frog jump.

    The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.

    Output

    If the frog can not reach the home, print -1.

    In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1.

    Example

    Input
    8 4
    10010101
    Output
    2
    Input
    4 2
    1001
    Output
    -1
    Input
    8 4
    11100101
    Output
    3
    Input
    12 3
    101111100101
    Output
    4

    Note

    In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).

    In the second example the frog can not reach home, because to make it she need to jump on a distance three, but the maximum length of her jump equals to two.

    前几场CF的题目,直接dp就可以了

    #include<bits/stdc++.h>
    using namespace std;
    const int N=105,INF=0x3f3f3f3f;
    int dp[N],n,d;
    string s;
    int main()
    {
        cin>>n>>d>>s;
        memset(dp,INF,sizeof dp);
        dp[0]=0;
        for(int i=0; i<n; i++)
            if(s[i]=='1')
            {
                for(int j=max(0,i-d);j<i;j++)
                    dp[i]=min(dp[i],dp[j]+1);
            }
        printf("%d
    ",dp[n-1]!=INF?dp[n-1]:-1);
        return 0;
    }

    D - Cave Painting

     CodeForces - 922C

    Imp is watching a documentary about cave painting.

    Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.

    Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:

    • 1 ≤ i < j ≤ k,
    • , where  is the remainder of division x by y.

    Input

    The only line contains two integers nk (1 ≤ n, k ≤ 1018).

    Output

    Print "Yes", if all the remainders are distinct, and "No" otherwise.

    You can print each letter in arbitrary case (lower or upper).

    Example

    Input
    4 4
    Output
    No
    Input
    5 3
    Output
    Yes

    Note

    In the first sample remainders modulo 1 and 4 coincide.

    只是试了一下暴力就过了,可能大数据很难造吧,或者不存在

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int main()
    {
        ll n,k;
        cin>>n>>k;
        for(ll i=1;i<=k;i++)
            if(n%i!=i-1)
            {
                puts("No
    ");
                return 0;
            }
        puts("Yes
    ");
        return 0;
    }

    E - And Now, a Remainder from Our Sponsor

     HDU - 1930

    IBM has decided that all messages sent to and from teams competing in the ACM programming contest should be encoded. They have decided that instead of sending the letters of a message, they will transmit their remainders relative to some secret keys which are four, two-digit integers that are pairwise relatively prime. For example, consider the message "THE CAT IN THE HAT". The letters of this message are first converted into numeric equivalents, where A=01, B=02, ..., Z=26 and a blank=27. Each group of 3 letters is then combined to create a 6 digit number. (If the last group does not contain 3 letters it is padded on the right with blanks and then transformed into a 6 digit number.) For example 
    THE CAT IN THE HAT → 200805 270301 202709 142720 080527 080120 
    Each six-digit integer is then encoded by replacing it with the remainders modulo the secret keys as follows: Each remainder should be padded with leading 0’s, if necessary, to make it two digits long. After this, the remainders are concatenated together and then any leading 0’s are removed. For example, if the secret keys are 34, 81, 65, and 43, then the first integer 200805 would have remainders 1, 6, 20 and 38. Following the rules above, these combine to get the encoding 1062038. The entire sample message above would be encoded as 
    1062038 1043103 1473907 22794503 15135731 16114011 

    InputThe input consists of multiple test cases. The first line of input consists of a single positive integer n indicating the number of test cases. The next 2n lines of the input consist of the test cases. The first line of each test case contains a positive integer (< 50) giving the number of groups in the encoded message. The second line of each test case consists of the four keys followed by the encoded message. 
    Each message group is separated with a space. 
    OutputFor each test case write the decoded message. You should not print any trailing blanks.Sample Input

    2
    6
    34 81 65 43 1062038 1043103 1473907 22794503 15135731 16114011
    3
    20 31 53 39 5184133 14080210 7090922

    Sample Output

    THE CAT IN THE HAT
    THE END

    这个题目是数学题啦

    给n串密码,再给4个被取余的数,要求恢复成原来的一串数组,所以两位两位分,运用扩欧或者中国剩余定理即可解决

    还有玄学代码使用272727这个数字的,应该是数据不强,这个题目是这次最难得题目

    #include<bits/stdc++.h>
    using namespace std;
    const int N=55;
    int a[5],TT[N][5],out[N];
    char num[N],m[5];
    void exgcd(int a,int b,int &d,int &x,int &y)
    {
        if(!b)d=a,x=1,y=0;
        else exgcd(b,a%b,d,y,x),y-=x*(a/b);
    }
    int main()
    {
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--)
        {
            int n;
            cin>>n;
            for(int i=1; i<=4; i++)cin>>a[i];
            for(int i=1,x; i<=n; i++)
            {
                cin>>x;
                for(int j=4; j>0; j--)TT[i][j]=x%100,x/=100;
            }
            for(int i=1; i<=n; i++)
            {
                int c=a[1],t=TT[i][1],d,z,x,y;
                for(int j=2; j<5; j++)
                {
                    exgcd(c,a[j],d,x,y);
                    z=a[j]/d;
                    x=((TT[i][j]-t)/d*x%z+z)%z;
                    t+=c*x;
                    c=c/d*a[j];
                }
                out[i]=t;
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<4; j++)
                    m[j]=out[i]%100,out[i]/=100;
                for(int j=3; j>0; j--)
                {
                    if(m[j]==27)
                    {
                        if(i!=n)printf(" ");
                        else break;
                    }
                    else printf("%c",m[j]+'A'-1);
                }
            }
            putchar(10);
        }
        return 0;
    }

    F - Connected Components?

     CodeForces - 920E 

    You are given an undirected graph consisting of n vertices and  edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.

    You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.

    Input

    The first line contains two integers n and m (1 ≤ n ≤ 200000, ).

    Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ nx ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.

    Output

    Firstly print k — the number of connected components in this graph.

    Then print k integers — the sizes of components. You should output these integers in non-descending order.

    Example

    Input
    5 5
    1 2
    3 4
    3 2
    4 2
    2 5
    Output
    2
    1 4

    图论啦,bfs出序列就可以了

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+5;
    set<int>M[N+N];
    queue<int>Q;
    int q[N],yy[N],n,m,k;
    void bfs()
    {
        while(!Q.empty())
        {
            int tot=1,u=Q.front();
            Q.pop();
            q[0]=u;
            for(int i=0; i<tot; i++)
            {
                int now=n-tot;
                while(now>0&&!Q.empty())
                {
                    int v=Q.front();
                    Q.pop();
                    if(!M[q[i]].count(v)) q[tot++]=v;
                    else Q.push(v);
                    --now;
                }
            }
            yy[k++]=tot;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin>>n>>m;
        for(int i=0,u,v; i<m; i++)
            cin>>u>>v,M[u].insert(v),M[v].insert(u);
        for(int i=1; i<=n; i++) Q.push(i);
        bfs();
        sort(yy,yy+k);
        printf("%d
    ",k);
        for(int i=0; i<k; i++)printf("%d ",yy[i]);
        return 0;
    }

    G - MADMAX

     CodeForces - 917B 

    As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with nvertices and m edges. There's a character written on each edge, a lowercase English letter.

    Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.

    Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?

    You have to determine the winner of the game for all initial positions of the marbles.

    Input

    The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).

    The next m lines contain the edges. Each line contains two integers vu and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ nv ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.

    Output

    Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex iand Lucas's marble is initially at vertex j, and 'B' otherwise.

    Example

    Input
    4 4
    1 2 b
    1 3 a
    2 4 c
    3 4 b
    Output
    BAAA
    ABAA
    BBBA
    BBBB
    Input
    5 8
    5 3 h
    1 2 c
    3 1 c
    3 2 r
    5 1 r
    4 3 z
    5 4 r
    5 2 h
    Output
    BABBB
    BBBBB
    AABBB
    AAABA
    AAAAB

    Note

    Here's the graph in the first sample test case:

    Here's the graph in the second sample test case:

     

    因为dag(有向无环图),所以可以记忆化搜索

    #include<bits/stdc++.h>
    using namespace std;
    const int N=105;
    int SG[105][105][26];
    int head[5005],nxt[5005],to[5005],W[5005];
    int tot;
    int DFS(int i,int j,int k)
    {
        if(SG[i][j][k])return SG[i][j][k];
        for(int p=head[i]; p; p=nxt[p])
            if(W[p]>=k&&!DFS(j,to[p],W[p]))SG[i][j][k]=1;
        return SG[i][j][k];
    }
    void add(int u,int v,int w)
    {
        nxt[++tot]=head[u],head[u]=tot,to[tot]=v,W[tot]=w;
    }
    int main()
    {
        int n,m;
        string s;
        cin>>n>>m;
        for(int i=1,u,v; i<=m; i++)
            cin>>u>>v>>s,add(u,v,s[0]-'a');
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                if(DFS(i,j,0))cout<<'A';
                else cout <<'B';
            cout<<"
    ";
        }
        return 0;
    }

    H - Color a Tree

     HDU - 1055 

    Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root to each of the other nodes. 

    Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, ..., N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try. 

    Each node has a “coloring cost factor”, Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci * Fi. 

    For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33. 

     

    Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes. 

    InputThe input consists of several test cases. The first line of each case contains two integers N and R (1 <= N <= 1000, 1 <= R <= N), where N is the number of nodes in the tree and R is the node number of the root node. The second line contains N integers, the i-th of which is Ci (1 <= Ci <= 500), the coloring cost factor of node i. Each of the next N-1 lines contains two space-separated node numbers V1 and V2, which are the endpoints of an edge in the tree, denoting that V1 is the father node of V2. No edge will be listed twice, and all edges will be listed. 

    A test case of N = 0 and R = 0 indicates the end of input, and should not be processed. 
    OutputFor each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes. 
    Sample Input

    5 1
    1 2 1 2 4
    1 2
    1 3
    2 4
    3 5
    0 0

    Sample Output

    33

    树染色经典题目,可以并查集维护的

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1005;
    int a[N],f[N],fa[N],num[N],sum[N],n,r;
    int find(int u)
    {
        return u==fa[u]?u:(fa[u]=find(fa[u]));
    }
    int main()
    {
        ios::sync_with_stdio(false);
        while(cin>>n>>r)
        {
            if(!n&&!r)break;
            f[r]=0,memset(sum,0,sizeof sum);
            for(int i=0; i<=n; i++)
                fa[i]=i,num[i]=1;
            for(int i=1; i<=n; i++)cin>>a[i];
            for(int i=1,u,v; i<n; i++)
                cin>>u>>v,f[v]=u;
            for(int i=0; i<n; i++)
            {
                int u=-1;
                for(int j=1; j<=n; j++)
                if(fa[j]==j&&(u==-1||a[u]*num[j]<a[j]*num[u]))u=j;
                int v=find(f[u]);
                sum[v]+=num[v]*a[u]+sum[u],num[v]+=num[u],a[v]+=a[u],fa[u]=v;
            }
            cout<<sum[0]<<"
    ";
        }
        return 0;
    }
  • 相关阅读:
    linux命令之free篇
    linux操作之逻辑分区与交换分区篇
    linux之软连接,硬连接篇
    Linux之磁盘分区篇
    Linux命令之vi篇
    JVM总结-垃圾回收算法
    JVM总结-字节码
    JVM总结-java对象的内存布局
    JVM-synchronized怎么实现的?
    JVM总结-invokedynamic
  • 原文地址:https://www.cnblogs.com/BobHuang/p/8457755.html
Copyright © 2011-2022 走看看