TopCoder Practice SRM 144 Div 1

  1 #include <iostream>
  2 #include <vector>
  3 #include <string>
  4 #include <sstream>
  5 #include <algorithm>
  6
  7 class Lottery
  8 {
  9 public:
 10     std::vector<std::string> sortByOdds(std::vector<std::string> rules)
 11     {
 12         std::vector<std::string> name;
 13         std::vector<int> choice;
 14         std::vector<int> blank;
 15         std::vector<std::string> sorted;
 16         std::vector<std::string> unique;
 17         long long n[rules.size()];
 18
 19         for (int i = 0; i != rules.size(); i++) {
 20             int posColon = rules[i].find(":");
 21             std::string s = rules[i];
 22             std::string iName = s.substr(0,posColon);
 23             std::string iPara = s.substr(posColon+2, s.size());
 24             std::cout << iPara << std::endl;
 25             name.push_back(iName);
 26
 27             std::stringstream ss(iPara);
 28             std::string buf;
 29             std::vector<std::string> tokens;
 30             while (ss >> buf) {
 31                 tokens.push_back(buf);
 32             }
 33             choice.push_back(stoi(tokens[0]));
 34             blank.push_back(stoi(tokens[1]));
 35             sorted.push_back(tokens[2]);
 36             unique.push_back(tokens[3]);
 37         }
 38
 39         for (int i = 0; i != rules.size(); i++) {
 40             n[i] = compute_odds(choice[i],blank[i],sorted[i],unique[i]);
 41             std::cout << n[i] << std::endl;
 42         }
 43
 44         sort_result(name,n);
 45
 46         return name;
 47     }
 48
 49 private:
 50     long long compute_odds(int n, int k, std::string s, std::string u) {
 51         long long p;
 52         bool isSorted = s == "T" ? true : false;
 53         bool isUnique = u == "T" ? true : false;
 54
 55         if (isSorted && isUnique)
 56             p = nchoosek(n,k);
 57         else if (isSorted && !isUnique)
 58             p = nchoosek(n+k-1,k);
 59         else if (!isSorted && !isUnique)
 60             p = pow(n,k);
 61         else if (!isSorted && isUnique)
 62             p = nchoosek(n,k) * factorial(k);
 63
 64         return p;
 65     }
 66
 67
 68     long long pow(int n, int k) {
 69         long long p = 1;
 70         for (int i = 1; i <= k; i++) {
 71             p *= n;
 72         }
 73         return p;
 74     }
 75
 76
 77     long long factorial(int k) {
 78         long long p = 1;
 79         for (int i = 1; i <= k; i++) {
 80             p *= i;
 81         }
 82         return p;
 83     }
 84
 85
 86     long long factorial(int n, int k) {
 87         long long p = 1;
 88         for (int i = n; i > n-k; i--) {
 89             p *= i;
 90         }
 91         return p;
 92     }
 93
 94
 95     long long nchoosek(int n, int k) {
 96         long long p = factorial(n,k) / factorial(k);
 97         return p;
 98     }
 99
100
101     void sort_result(std::vector<std::string> & name, long long *n) {
102         // bubble sort
103         for (int i = 0; i != name.size()-1; i++) {
104             bool swapped = false;
105             for (int j = 0; j != name.size()-1; j++) {
106                 if (n[j] > n[j+1] || (n[j] == n[j+1] && name[j] > name[j+1])) {
107                     swap(name[j], name[j+1]);
108                     swap(n[j], n[j+1]);
109                     swapped = true;
110                 }
111             }
112             if (swapped == false) {
113                 break;
114             }
115         }
116     }
117
118     template <class T> void swap(T &a, T &b) {
119         T c(a);
120         a = b;
121         b = c;
122     }
123
124 };
125
126
127 int main(int argc, char** argv)
128 {
129     std::vector<std::string> test1;
130     test1.push_back("PICK ANY TWO: 10 2 F F");
131     test1.push_back("PICK TWO IN ORDER: 10 2 T F");
132     test1.push_back("PICK TWO DIFFERENT: 10 2 F T");
133     test1.push_back("PICK TWO LIMITED: 10 2 T T");
134     std::vector<std::string> test2;
135     test2.push_back("INDIGO: 93 8 T F");
136     test2.push_back("ORANGE: 29 8 F T");
137     test2.push_back("VIOLET: 76 6 F F");
138     test2.push_back("BLUE: 100 8 T T");
139     test2.push_back("RED: 99 8 T T");
140     test2.push_back("GREEN: 78 6 F T");
141     test2.push_back("YELLOW: 75 6 F F");
142     Lottery lot;
143     std::vector<std::string> res = lot.sortByOdds(test2);
144
145     for (int i = 0; i != res.size(); i++) {
146         std::cout << res[i] << std::endl;
147     }
148
149     return 0;
150 }

550 Points

  

时间: 2024-10-14 00:56:03

TopCoder Practice SRM 144 Div 1的相关文章

TopCoder SRM 144 DIV 2

200: Problem Statement   Computers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date. Your task in this problem is to write a method whatTime, which takes an int, seconds, re

Topcoder SRM 144 DIV 1

BinaryCode 模拟 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在给你Q,让你求出P. 做法是:枚举第一位是1还是0,然后就可以推到出P[i]=Q[i-1]-P[i-1]-P[i-2],需要注意一下边界就好. Lottery 组合数学 题意是:给你四种买彩票,将他们的中奖概率排序,这四种彩票都是从1到a中取b个数字,第一种是随便取,第二种是选取的必须是有序的,第三种是选取的必须是不同的,第四种是选取的必须是有序且不同的. 做法

【TopCoder】SRM 680 DIV 2

1. BearPair之bigDistance1.1 题目概述在 <= 50的字符串中找位置i,j 满足(1) s[i] != s[j];(2) abs(i-j)尽可能大.若不存在返回-1, 否则返回最大值. 1.2 基本思路没什么好说的,串长这么短 O(n^2)直接A了. 1.3 代码 1 class BearPair { 2 public: 3 int pos[26]; 4 5 int bigDistance(string s) { 6 int len = s.length(); 7 int

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization &amp; Codeforces 839 E

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

Topcoder口胡记 SRM 562 Div 1 ~ SRM 592 Div 1

传送门:https://284914869.github.io/AEoj/index.html Topcoder SRM 562 Div 1 - Problem 1000 InducedSubgraphs 当K*2<=N的时候,显而易见的是编号为i(K<=i<=N-K+1)的点一定会形成一条链. 枚举合法的这样的链,剩下的暴力dp吧. 当K*2>N的时候,显而易见的是编号为i(N-K+1<=i<=K)的点一定会形成一个联通快. 如果把这个联通块去掉,树会形成若干个不相交

TC Member SRM 478 DIV 1(CarrotJumping-操作观察)

Problem Statement   Rabbits often feel hungry, so when they go out to eat carrots, they jump as quickly as possible. Initially, rabbit Hanako stands at position init. From position x, she can jump to either position 4*x+3 or 8*x+7 in a single jump. S

竞赛图的得分序列 (SRM 717 div 1 250)

SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, v)有路径的点对数,似乎有不需要构造出原图的方法). 当时我的做法是 直接构造一个网络,跑最大流. 比赛后总觉得这个题有什么神奇的性质,于是搜了一下相关资料: 有一篇关于得分序列的论文:http://www.sciencedirect.com/science/article/pii/0095895