zoukankan      html  css  js  c++  java
  • Topcoder SRM652div2

    开始接触Topcode..div2..

    250题目:

    Problem Statement
        
    You are given a String s consisting of lower case letters. We assign the letters 'a' to 'z' values of 1 to 26, respectively. We will denote the value assigned to the letter X by val[X]. For example, val['a'] = 1 and val['e'] = 5.
    We define the value of the string s as follows. For each letter s[i], let k[i] be the number of letters in s that are less than or equal to s[i], including s[i] itself. Then, the value of s is defined to be the sum of k[i] * val[s[i]] for all valid i.
    Given the string, compute and return the value of the string.
    Definition
        
    Class:
    ValueOfString
    Method:
    findValue
    Parameters:
    String
    Returns:
    int
    Method signature:
    int findValue(String s)
    (be sure your method is public)
    Limits
        
    Time limit (s):
    2.000
    Memory limit (MB):
    256
    Stack limit (MB):
    256
    Constraints
    -
    s will contain between 1 and 50 characters, inclusive.
    -
    s will consist of lowercase letters ('a'-'z').
    Examples
    0)
    
        
    "babca"
    Returns: 35
    The value of this string is 2*4 + 1*2 + 2*4 + 3*5 + 1*2 = 35.
    We can get the value as follows. The first character is a 'b' which has value 2, and has 4 characters that are less than or equal to it in the string (i.e. the first, second, third and fifth character of the string). Thus, this first character contributes 2*4 to the sum. We can derive a similar expression for each of the other characters.
    1)
    
        
    "zz"
    Returns: 104
    
    2)
    
        
    "y"
    Returns: 25
    
    3)
    
        
    "aaabbc"
    Returns: 47
    
    4)
    
        
    "topcoder"
    Returns: 558
    
    5)
    
        
    "thequickbrownfoxjumpsoverthelazydog"
    Returns: 11187
    
    6)
    
        
    "zyxwvutsrqponmlkjihgfedcba"
    Returns: 6201
    
    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
    View Code

    根据题意直接搞~

    代码:

    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    class ValueOfString
    {
      public:
        int findValue( string s )
          {
            int res = 0 ;
            int cnt[27] , val[27] ;
            for( int i = 0 ; i < 26 ; ++i ) {
                cnt[i] = 0 ; val[i] = i + 1 ;
            }
            for( int i = 0 ; i < s.length() ; ++i ) {
                cnt[ s[i]-'a' ]++;
            }
            for( int i = 1 ; i < 26 ; ++i ) cnt[i] += cnt[i-1] ;
            for( int i = 0 ; i < s.length() ; ++i ) {
                 res += cnt[ s[i] - 'a' ] *  val[ s[i]-'a' ] ;
            }
            return res ;
        }
    };
    View Code

    500题目:

    Problem Statement
        
    Alice and Bob are playing a game called "The Permutation Game". The game is parameterized with the int N. At the start of the game, Alice chooses a positive integer x, and Bob chooses a permutation of the first N positive integers. Let p be Bob's permutation. Alice will start at 1, and apply the permutation to this value x times. More formally, let f(1) = p[1], and f(m) = p[f(m-1)] for all m >= 2. Alice's final value will be f(x). Alice wants to choose the smallest x such that f(x) = 1 for any permutation Bob can provide. Compute and return the value of such x.
    Definition
        
    Class:
    ThePermutationGameDiv2
    Method:
    findMin
    Parameters:
    int
    Returns:
    long long
    Method signature:
    long long findMin(int N)
    (be sure your method is public)
    Limits
        
    Time limit (s):
    2.000
    Memory limit (MB):
    256
    Stack limit (MB):
    256
    Notes
    -
    The return value will fit into a signed 64-bit integer.
    -
    A permutation of the first N positive integers is a sequence of length N that contains each of the integers 1 through N exactly once. The i-th (1-indexed) element of a permutation p is denoted by p[i].
    Constraints
    -
    N will be between 1 and 35 inclusive.
    Examples
    0)
    
        
    2
    Returns: 2
    Bob can choose the permutations {1,2} or {2,1}. If Alice chooses 1, then, Bob can choose the permutation {2,1}, which would would make f(1) = 2. However, if Alice chooses 2, no matter which permutation Bob chooses, Alice will get f(2) = 1. Thus the answer in this case is 2.
    1)
    
        
    3
    Returns: 6
    
    2)
    
        
    6
    Returns: 60
    
    3)
    
        
    11
    Returns: 27720
    
    4)
    
        
    25
    Returns: 26771144400
    
    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
    View Code

    模拟完yy一下应该是 1 ~ n 的 LCM , 结果对了。。

    代码:

    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    class ThePermutationGameDiv2
    {
      public:
        long long gcd( long long a , long long b ) { return b == 0 ? a : gcd( b , a % b ) ; }
        long long LCM( long long a , long long b ) { return a/gcd(a,b)*b; }
        long long findMin( int n )
          {
            long long res = 1 ;
            for( int i = 1 ; i <= n ; ++i ) {
                res = LCM( res , i ) ;
            }
            return res ;
        }
    };
    View Code

    1000题目:

    Problem Statement
        
    Roger the Robot has been sent to explore a planet. The surface of the planet can be thought of as a two-dimensional plane. You are given two int[]s x and y. The planet has N interesting points described by these int[]s. The i-th interesting point has coordinates (x[i], y[i]). No three interesting points will be collinear.
    Roger will choose a permutation of {0,1,...,N-1}, and will visit the points in that order. Roger will travel in a straight line in between points. There are two conditions he must follow:
    He must never cross his own path (that is, if we look at the line segments formed by the path, no two segments strictly intersect).
    Due to rather unfortunate oversight, Roger is incapable of making any right turns. This means that for any three consecutive points that he visits, these three points constitute a counter-clockwise orientation.
    Your job is to find a path that Roger can take. If there is no valid path, return an empty int[]. Otherwise, return an int[] containing a permutation of 0,...,N-1, representing a valid path that Roger can take.
    Definition
        
    Class:
    NoRightTurnDiv2
    Method:
    findPath
    Parameters:
    int[], int[]
    Returns:
    int[]
    Method signature:
    int[] findPath(int[] x, int[] y)
    (be sure your method is public)
    Limits
        
    Time limit (s):
    2.000
    Memory limit (MB):
    256
    Stack limit (MB):
    256
    Constraints
    -
    x will contain between 2 and 50 elements, inclusive.
    -
    y will contain exactly the same number of elements as x.
    -
    Each element of x,y will be between -1,000 and 1,000, inclusive.
    -
    All pairs (x[i], y[i]) will be distinct.
    -
    No three points will be collinear.
    Examples
    0)
    
        
    {-10, 0, 10}
    {10, -10, 10}
    Returns: {0, 1, 2 }
    The points form a triangle. Any of the following return values will be accepted: {0,1,2},{1,2,0},{2,0,1}
    1)
    
        
    {0,0,-3,-3,3,3}
    {-1,1,-3,3,-3,3}
    Returns: {0, 4, 5, 3, 2, 1 }
    Here is a picture of the points:
     
    Here is an example of a different valid solution. This would correspond to a return value of {1,5,3,2,4,0}
     
    2)
    
        
    {10,9,8,7,6,5,4,3,2,1}
    {1,4,9,16,25,36,49,64,81,100}
    Returns: {9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
    
    3)
    
        
    {0, 2,-2, 4,-4, 2,-2, 0}
    {1, 2, 2, 4, 4, 6, 6, 5}
    Returns: {4, 2, 0, 1, 3, 5, 6, 7 }
    
    4)
    
        
    {-76,98,83,58,-15,94,21,55,80,84,-39,-90,-46,100,-80,-49,-2,-70,36,48,88,10,
    55,-56,22,67,31,81,100,-39,64,-62,-7,45,-82,-24,51,-33,53,11,20,-74,-83,47,
    9,39,42,63,-97,94}
    {-90,68,91,-92,-6,88,99,10,39,-69,-61,-4,71,-5,90,-51,21,-53,-21,-86,41,-9,
    42,-23,-4,12,94,-59,55,18,70,-88,-86,-17,-97,-33,87,80,91,-80,-79,-79,-78,
    -99,57,67,-52,-46,61,-10}
    Returns: 
    {39, 32, 40, 31, 19, 27, 47, 46, 0, 34, 43, 3, 9, 13, 28, 1, 5, 2, 6,
     14, 48, 42, 41, 49, 20, 38, 26, 37, 12, 11, 17, 10, 33, 25, 8, 30, 36,
     44, 29, 23, 15, 18, 7, 22, 45, 16, 4, 35, 24, 21 }
    
    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
    View Code


    给出n个点求出一条路径,这路径必须满足只能向左拐,能够走过n个点,路径没有交叉的方案。

    先找出边角点作为起点,然后贪心,枚举点,找左拐角度最微的点作为下一个点

    代码:

    #include <string>
    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    #include <map>
    using namespace std;
    typedef pair<int,int> pii;
    const int N = 101;
    struct Point {
           int x , y , idx ;
        Point(){};
        Point( int x , int y ):x(x),y(y){}
        bool operator < ( const Point a )const {
            if( x != a.x ) return x < a.x;
            else return y < a.y ;
        }
    }p[N];
    
    Point operator - ( const Point a , const Point b ){
        return Point( a.x - b.x , a.y - b.y );
    }
    
    int Cross( Point A , Point B ) { return A.x*B.y-A.y*B.x; }
    
    bool vis[N];
    
    class NoRightTurnDiv2
    {
      public:
        vector<int> findPath( vector<int> x, vector<int> y )
          {
            vector<int>res ;
            int n = x.size();
            for( int i = 0 ; i < n ; ++i ) {
                p[i].x = x[i] , p[i].y = y[i];
                p[i].idx = i ;
                if( p[i].y < p[0].y || p[i].y == p[0].y && p[i].x > p[0].x ) swap( p[0] , p[i] );
            }
            memset( vis , false , sizeof vis );
            vis[0] = true ; res.push_back( p[0].idx );
            int pre = 0 ;
            for( int i = 1 ; i < n ; ++i ) {
                int now = -1 ;
                for( int j = 1 ; j < n ; ++j ) if( !vis[j] ) {
                    if( now == -1 ) now = j ;
                    else {
                        if( Cross( p[pre] - p[now] , p[pre] - p[j] ) <= 0 ) {
                            now = j ;
                        }
                    }
                }
                vis[now] = true ;
                res.push_back( p[now].idx ) ;
                pre = now ;
            }
            return res ;
        }
    };
    View Code
  • 相关阅读:
    asp.net gridview中增加单击单元格事件
    asp.net在应用母版的页面下采用了ModalPopupExtender弹出窗中应用autocomplete
    网站发布后无法访问,提示“/”应用程序中的服务器错误
    asp.net将数据导出到excel
    看完让你彻底搞懂Websocket原理
    PL/SQL简单使用——导入、导出数据表
    Java 定时任务的几种实现方式
    用element-ui 时,报value.getTime is not a function错误:
    Object.assign()解释整理
    IntelliJ IDEA2017 激活方法 最新的(亲测可用)
  • 原文地址:https://www.cnblogs.com/hlmark/p/4325793.html
Copyright © 2011-2022 走看看