Thread: [Assorted-commits] SF.net SVN: assorted: [246] problems/topcoder
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-01-20 00:23:49
|
Revision: 246 http://assorted.svn.sourceforge.net/assorted/?rev=246&view=rev Author: yangzhang Date: 2008-01-19 16:23:46 -0800 (Sat, 19 Jan 2008) Log Message: ----------- left out some old files on laptop Added Paths: ----------- problems/topcoder/BinaryCode/BinaryCode.cs problems/topcoder/Lottery/Lottery.cs problems/topcoder/PenLift/PenLift.jnt Added: problems/topcoder/BinaryCode/BinaryCode.cs =================================================================== --- problems/topcoder/BinaryCode/BinaryCode.cs (rev 0) +++ problems/topcoder/BinaryCode/BinaryCode.cs 2008-01-20 00:23:46 UTC (rev 246) @@ -0,0 +1,129 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +public class BinaryCode { + public string[] decode(string message) { + //List<int> xs = new List<int>(); + //foreach(char c in message) xs.Add(c - '0'); + //List<string> ss = new List<string>(); + //for (int i = 0; i < 2; i++) { + // List<int> ys = new List<int>(); + // ys.Add(i); + // for (int j = 1; j < xs.Count; j++) + // ys.Add(xs[j - 1] - ys[j - 1] - (j > 1 ? ys[j - 2] : 0)); + // string s = ""; + // foreach (int j in ys) s += j; + // bool bad = false; + // foreach (int j in ys) if (j != 0 && j != 1) bad = true; + // { + // int j = xs.Count; + // if (xs[j - 1] - ys[j - 1] - (j > 1 ? ys[j - 2] : 0) != 0) bad = true; + // } + // if (bad) s = "NONE"; + // ss.Add(s); + //} + //return ss.ToArray(); + + string m = message; + string[] ss = new string[2]; + for (int i = 0; i < 2; i++) { + string s = "" + i; + for (int j = 1; j < m.Length; j++) + s += (m[j - 1] - s[j - 1] - (j > 1 ? s[j - 2] : '0') + '0'); + bool bad = false; + for (int j = 0; j < m.Length; j++) + if (s[j] != '0' && s[j] != '1') bad = true; + if (s[s.Length - 1] + (s.Length > 1 ? s[s.Length - 2] : '0') != '0' + m[m.Length - 1]) bad = true; + if (bad) s = "NONE"; + ss[i] = s; + } + return ss; + } + + + + + + + public void pp<T>(T x) { Console.WriteLine(x); } + + + + + + + + + // BEGIN CUT HERE + public static void Main(string[] args) { + eq(0,(new BinaryCode()).decode("123210122"),new string[] { "011100011", "NONE" }); + eq(1,(new BinaryCode()).decode("11"),new string[] { "01", "10" }); + eq(2,(new BinaryCode()).decode("22111"),new string[] { "NONE", "11001" }); + eq(3,(new BinaryCode()).decode("123210120"),new string[] { "NONE", "NONE" }); + eq(4,(new BinaryCode()).decode("3"),new string[] { "NONE", "NONE" }); + eq(5,(new BinaryCode()).decode("12221112222221112221111111112221111"),new string[] { "01101001101101001101001001001101001", "10110010110110010110010010010110010" }); + Console.WriteLine("done!"); + Console.Read(); + } + private static void eq( int n, object have, object need) { + if( eq( have, need ) ) { + Console.WriteLine( "Case "+n+" passed." ); + } else { + Console.Write( "Case "+n+" failed: expected " ); + print( need ); + Console.Write( ", received " ); + print( have ); + Console.WriteLine(); + } + } + private static void eq( int n, Array have, Array need) { + if( have == null || have.Length != need.Length ) { + Console.WriteLine("Case "+n+" failed: returned "+have.Length+" elements; expected "+need.Length+" elements."); + print( have ); + print( need ); + return; + } + for( int i= 0; i < have.Length; i++ ) { + if( ! eq( have.GetValue(i), need.GetValue(i) ) ) { + Console.WriteLine( "Case "+n+" failed. Expected and returned array differ in position "+i ); + print( have ); + print( need ); + return; + } + } + Console.WriteLine("Case "+n+" passed."); + } + private static bool eq( object a, object b ) { + if ( a is double && b is double ) { + return Math.Abs((double)a-(double)b) < 1E-9; + } else { + return a!=null && b!=null && a.Equals(b); + } + } + private static void print( object a ) { + if ( a is string ) { + Console.Write("\"{0}\"", a); + } else if ( a is long ) { + Console.Write("{0}L", a); + } else { + Console.Write(a); + } + } + private static void print( Array a ) { + if ( a == null) { + Console.WriteLine("<NULL>"); + } + Console.Write('{'); + for ( int i= 0; i < a.Length; i++ ) { + print( a.GetValue(i) ); + if( i != a.Length-1 ) { + Console.Write(", "); + } + } + Console.WriteLine( '}' ); + } + // END CUT HERE +} Added: problems/topcoder/Lottery/Lottery.cs =================================================================== --- problems/topcoder/Lottery/Lottery.cs (rev 0) +++ problems/topcoder/Lottery/Lottery.cs 2008-01-20 00:23:46 UTC (rev 246) @@ -0,0 +1,131 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +using P = System.Collections.Generic.KeyValuePair<double, string>; + +public class Lottery { + public string[] sortByOdds(string[] rules) { + List<P> list = new List<P>(); + foreach(string rule in rules) { + string[] parts = rule.Split(':'); + string name = parts[0]; + parts = parts[1].Trim().Split(); + int choi = int.Parse(parts[0]); + int blan = int.Parse(parts[1]); + bool sort = (parts[2] == "T"); + bool uniq = (parts[3] == "T"); + double z = 999; + if (!sort && !uniq) z = Math.Pow(choi, blan); + if (!sort && uniq) z = fact(choi) / fact(choi - blan); + if (sort && !uniq) z = fact(choi+blan-1) / fact(choi-1) / fact(blan); + if (sort && uniq) z = fact(choi) / fact(choi - blan) / fact(blan); + //pp(name + " " + choi + " " + blan + " " + " " + sort + " " + uniq + " " + z); + list.Add(new P(z, name)); + } + list.Sort(delegate(P x, P y) { return x.Key.CompareTo(y.Key) != 0 ? x.Key.CompareTo(y.Key) : x.Value.CompareTo(y.Value); }); + List<String> ys = new List<String>(); + foreach (P p in list) ys.Add(p.Value); + return ys.ToArray(); + } + double fact(int x) { + double y = 1; + for (int i = 0; i < x; i++) { + y *= i+1; + } + return y; + } + + + + + + + public void pp<T>(T x) { Console.WriteLine(x); } + + + + + + + + + // BEGIN CUT HERE + public static void Main(string[] args) { + eq(0,(new Lottery()).sortByOdds(new string[] {"PICK ANY TWO: 10 2 F F" + ,"PICK TWO IN ORDER: 10 2 T F" + ,"PICK TWO DIFFERENT: 10 2 F T" + ,"PICK TWO LIMITED: 10 2 T T"}),new string[] { "PICK TWO LIMITED", "PICK TWO IN ORDER", "PICK TWO DIFFERENT", "PICK ANY TWO" }); + eq(1,(new Lottery()).sortByOdds(new string[] {"INDIGO: 93 8 T F", + "ORANGE: 29 8 F T", + "VIOLET: 76 6 F F", + "BLUE: 100 8 T T", + "RED: 99 8 T T", + "GREEN: 78 6 F T", + "YELLOW: 75 6 F F"} + ),new string[] { "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET" }); + eq(2,(new Lottery()).sortByOdds(new string[] {}),new string[] { }); + Console.WriteLine("done!"); + Console.Read(); + } + private static void eq( int n, object have, object need) { + if( eq( have, need ) ) { + Console.WriteLine( "Case "+n+" passed." ); + } else { + Console.Write( "Case "+n+" failed: expected " ); + print( need ); + Console.Write( ", received " ); + print( have ); + Console.WriteLine(); + } + } + private static void eq( int n, Array have, Array need) { + if( have == null || have.Length != need.Length ) { + Console.WriteLine("Case "+n+" failed: returned "+have.Length+" elements; expected "+need.Length+" elements."); + print( have ); + print( need ); + return; + } + for( int i= 0; i < have.Length; i++ ) { + if( ! eq( have.GetValue(i), need.GetValue(i) ) ) { + Console.WriteLine( "Case "+n+" failed. Expected and returned array differ in position "+i ); + print( have ); + print( need ); + return; + } + } + Console.WriteLine("Case "+n+" passed."); + } + private static bool eq( object a, object b ) { + if ( a is double && b is double ) { + return Math.Abs((double)a-(double)b) < 1E-9; + } else { + return a!=null && b!=null && a.Equals(b); + } + } + private static void print( object a ) { + if ( a is string ) { + Console.Write("\"{0}\"", a); + } else if ( a is long ) { + Console.Write("{0}L", a); + } else { + Console.Write(a); + } + } + private static void print( Array a ) { + if ( a == null) { + Console.WriteLine("<NULL>"); + } + Console.Write('{'); + for ( int i= 0; i < a.Length; i++ ) { + print( a.GetValue(i) ); + if( i != a.Length-1 ) { + Console.Write(", "); + } + } + Console.WriteLine( '}' ); + } + // END CUT HERE +} Added: problems/topcoder/PenLift/PenLift.jnt =================================================================== (Binary files differ) Property changes on: problems/topcoder/PenLift/PenLift.jnt ___________________________________________________________________ Name: svn:mime-type + application/octet-stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-05-16 00:38:56
|
Revision: 826 http://assorted.svn.sourceforge.net/assorted/?rev=826&view=rev Author: yangzhang Date: 2008-05-15 17:38:51 -0700 (Thu, 15 May 2008) Log Message: ----------- picking up topcoder again... Added Paths: ----------- problems/topcoder/Bonuses/ problems/topcoder/Bonuses/Bonuses.cc problems/topcoder/Lottery/Lottery2.cc problems/topcoder/PenLift/PenLift.top Added: problems/topcoder/Bonuses/Bonuses.cc =================================================================== --- problems/topcoder/Bonuses/Bonuses.cc (rev 0) +++ problems/topcoder/Bonuses/Bonuses.cc 2008-05-16 00:38:51 UTC (rev 826) @@ -0,0 +1,297 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { cout << __FILE__ << ":" << __LINE__ << ": "; pp(x); x; } +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T xs, U x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T x) { stringstream s; s << x; return s.str(); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powl(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +#define pp(x) cout << #x << ' ' << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + bounds(i,xs) os << i ? ", " : "{ " << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + bounds(i,xs) os << (i ? ", " : "{ " )<< xs[i]; + return os << " }"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +// sum +class Bonuses { + public: + vector <int> getDivision(vector <int> points) { + vector <int> res(points.sz); + int tot = 0; + bounds(i,points) tot += points[i]; + int rem = 100; + vec(pair<int,int>) pairs(points.sz); + int n = points.sz; + vi per(n); + bounds(i,points) { + int p = floor(100.0 * points[i] / double(tot)); + pairs[i] = make_pair(-points[i],i); + per[i] = p; + rem -= p; + } +#if 1 + assert(rem>=0 && rem<points.sz); + ssort(pairs); + bounds(i,pairs) { + if (rem == 0) break; + per[pairs[i].second] += 1; + rem -= 1; + } + bounds(i,pairs) res[i] = per[i]; +#endif +#if 0 + vb done(n); + while (rem-- > 0) { + int h=1,b=0; + for(int i=0;i<n;i++) + if (!done[i] && points[i] > h) { + h = points[i]; + b = i; + } + done[b] = true; + pairs[b].first--; + } + bounds(i,pairs) res[pairs[i].second] = -pairs[i].first; +#endif + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + int pointsARRAY[] = {1,2,3,4,5}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 6, 13, 20, 27, 34 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(0, theObject.getDivision(points),retrunValue); + } + { + int pointsARRAY[] = {5,5,5,5,5,5}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 17, 17, 17, 17, 16, 16 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(1, theObject.getDivision(points),retrunValue); + } + { + int pointsARRAY[] = {485, 324, 263, 143, 470, 292, 304, 188, 100, 254, 296, + 255, 360, 231, 311, 275, 93, 463, 115, 366, 197, 470}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 8, 6, 4, 2, 8, 5, 5, 3, 1, 4, 5, 4, 6, 3, 5, 4, 1, 8, 1, 6, 3, 8 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(2, theObject.getDivision(points),retrunValue); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/Lottery/Lottery2.cc =================================================================== --- problems/topcoder/Lottery/Lottery2.cc (rev 0) +++ problems/topcoder/Lottery/Lottery2.cc 2008-05-16 00:38:51 UTC (rev 826) @@ -0,0 +1,338 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void print(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class Lottery { + public: + vector <string> sortByOdds(vector <string> rules) { + vector <string> res; + vec(pair<double, str>) pairs; + each(rule,rules) { + vs parts = splitstr(*rule, ": "); + string name = parts[0]; + vs attrs = split(parts[1]); + int choi = atoi(attrs[0].c_str()); + int blan = atoi(attrs[1].c_str()); + bool sort = attrs[2] == "T"; + bool uniq = attrs[3] == "T"; + pp(choi); + pp(blan); + pp(sort); + pp(uniq); + double poss = -1; + if (!sort && !uniq) { + trace(poss = powd(choi, blan)); + } else if (!sort && uniq) { + trace(poss = perms(choi, blan)); + } else if (sort && !uniq) { + trace(poss = combs(choi + blan - 1, blan)); + } else if (sort && uniq) { + trace(poss = combs(choi, blan)); + } else { + assert(false); + } + pairs.push_back(mkpair(poss, name)); + } + sort(pairs); + pp(pairs); + bounds(i,pairs) res.push_back(pairs[i].second); + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + string rulesARRAY[] = {"PICK ANY TWO: 10 2 F F" + ,"PICK TWO IN ORDER: 10 2 T F" + ,"PICK TWO DIFFERENT: 10 2 F T" + ,"PICK TWO LIMITED: 10 2 T T"}; + vector <string> rules( rulesARRAY, rulesARRAY+ARRSIZE(rulesARRAY) ); + string retrunValueARRAY[] = { "PICK TWO LIMITED", "PICK TWO IN ORDER", "PICK TWO DIFFERENT", "PICK ANY TWO" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Lottery theObject; + eq(0, theObject.sortByOdds(rules),retrunValue); + } + { + string rulesARRAY[] = {"INDIGO: 93 8 T F", + "ORANGE: 29 8 F T", + "VIOLET: 76 6 F F", + "BLUE: 100 8 T T", + "RED: 99 8 T T", + "GREEN: 78 6 F T", + "YELLOW: 75 6 F F"} + ; + vector <string> rules( rulesARRAY, rulesARRAY+ARRSIZE(rulesARRAY) ); + string retrunValueARRAY[] = { "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Lottery theObject; + eq(1, theObject.sortByOdds(rules),retrunValue); + } + { + Lottery theObject; + eq(2, theObject.sortByOdds(vector <string>()),vector <string>()); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/PenLift/PenLift.top =================================================================== --- problems/topcoder/PenLift/PenLift.top (rev 0) +++ problems/topcoder/PenLift/PenLift.top 2008-05-16 00:38:51 UTC (rev 826) @@ -0,0 +1,56 @@ +class PenLift + int numTimes(str* segments, int n) + // init + var xs={} ys={} + var segs = for seg in segments + int a b c d + seg ~ "a b c d" + xs ++= [a c] + ys ++= [b d] + (a b c d) + var packs = for zs in [xs ys] + var pack = {} + var j = 0 + for v in zs + pack(v) = j++ + assert j < 100 + pack + + // adj + var adj = arr((200,200),{}) + for (a b c d) in segs + [a c] = [a c] map packs(0) + [b d] = [b d] map packs(1) + if a == c + var x = a + if b > d: swap b d + for y in [b,d) + adj(x,y) += (x,y+1) + adj(x,y+1) += (x,y) + else b == d + var y = b + order a c // if a > c: swap a c + for x in [a,c) + adj(x,y) += (x+1,y) + adj(x+1,y) += (x,y) + + // search + var num = -1 + for x to 100 + for y to 100 + if adj(x y) not empty + var q = Q() + q push (x y) + var odds = 0 + while q not empty + (x y) = x pop + var ps = adj(x y) + if ps % 2 == 1: odds++ + for p=(x y) in ps + if adj(p) not empty + q push p + ps clear + if n % 2 == 0 or odds <= 2: odds = 2 + num += odds / 2 + + num This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-06-02 00:18:54
|
Revision: 840 http://assorted.svn.sourceforge.net/assorted/?rev=840&view=rev Author: yangzhang Date: 2008-06-01 17:18:59 -0700 (Sun, 01 Jun 2008) Log Message: ----------- added some new problems Added Paths: ----------- problems/topcoder/ImageDithering/ problems/topcoder/ImageDithering/ImageDithering.cc problems/topcoder/RectangularGrid/ problems/topcoder/RectangularGrid/RectangularGrid.cc problems/topcoder/Time/ problems/topcoder/Time/Time.cc Added: problems/topcoder/ImageDithering/ImageDithering.cc =================================================================== --- problems/topcoder/ImageDithering/ImageDithering.cc (rev 0) +++ problems/topcoder/ImageDithering/ImageDithering.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,332 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class ImageDithering { + public: + int count(string dithered, vector <string> screen) { + int res=0; + bounds(i,screen) { + bounds(j,screen[i]) { + if (dithered.find(screen[i][j]) != str::npos) { + res++; + } + } + } + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + string screenARRAY[] = {"AAAAAAAA", + "ABWBWBWA", + "AWBWBWBA", + "ABWBWBWA", + "AWBWBWBA", + "AAAAAAAA"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(0, theObject.count("BW", screen),24); + } + { + string screenARRAY[] = {"BBBBBBBB", + "BBWBWBWB", + "BWBWBWBB", + "BBWBWBWB", + "BWBWBWBB", + "BBBBBBBB"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(1, theObject.count("BW", screen),48); + } + { + string screenARRAY[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX", + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(2, theObject.count("ACEGIKMOQSUWY", screen),150); + } + { + string screenARRAY[] = {"BBBBBBB", + "BBBBBBB", + "BBBBBBB"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(3, theObject.count("CA", screen),0); + } + { + string screenARRAY[] = {"ACBD"}; + vector <string> screen( screenARRAY, screenARRAY+ARRSIZE(screenARRAY) ); + ImageDithering theObject; + eq(4, theObject.count("DCBA", screen),4); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/RectangularGrid/RectangularGrid.cc =================================================================== --- problems/topcoder/RectangularGrid/RectangularGrid.cc (rev 0) +++ problems/topcoder/RectangularGrid/RectangularGrid.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,309 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << (x) << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class RectangularGrid { + public: + long long countRectangles(int width, int height) { + long long res = 0; + pp(width); + range(i,1,width + 1) { + range(j,1,height + 1) { + res += i==j ? 0 : (width - i + 1) * (height - j + 1); // (width - i) * (height - j); + //pp(i); + //pp(j); + //pp(i==j?0:i*j); + //cout<<endl; + } + } + //if (width==5) exit(0); + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + RectangularGrid theObject; + eq<long long>(0, theObject.countRectangles(3, 3),22L); + } + { + RectangularGrid theObject; + eq<long long>(1, theObject.countRectangles(5, 2),31L); + } + { + RectangularGrid theObject; + eq<long long>(2, theObject.countRectangles(10, 10),2640L); + } + { + RectangularGrid theObject; + eq<long long>(3, theObject.countRectangles(1, 1),0L); + } + { + RectangularGrid theObject; + eq<long long>(4, theObject.countRectangles(592, 964),81508708664LL); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/Time/Time.cc =================================================================== --- problems/topcoder/Time/Time.cc (rev 0) +++ problems/topcoder/Time/Time.cc 2008-06-02 00:18:59 UTC (rev 840) @@ -0,0 +1,299 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class Time { + public: + string whatTime(int seconds) { + int s = seconds % 60; + int minutes = seconds / 60; + int m = minutes % 60; + int h = minutes / 60; + stringstream ss; + ss << h << ":" << m << ":" << s; + return ss.str(); + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + Time theObject; + eq(0, theObject.whatTime(0),"0:0:0"); + } + { + Time theObject; + eq(1, theObject.whatTime(3661),"1:1:1"); + } + { + Time theObject; + eq(2, theObject.whatTime(5436),"1:30:36"); + } + { + Time theObject; + eq(3, theObject.whatTime(86399),"23:59:59"); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-06-02 00:19:20
|
Revision: 841 http://assorted.svn.sourceforge.net/assorted/?rev=841&view=rev Author: yangzhang Date: 2008-06-01 17:19:27 -0700 (Sun, 01 Jun 2008) Log Message: ----------- added some problem retries Added Paths: ----------- problems/topcoder/BinaryCode/BinaryCode2.cc problems/topcoder/Bonuses/Bonuses2.cc Added: problems/topcoder/BinaryCode/BinaryCode2.cc =================================================================== --- problems/topcoder/BinaryCode/BinaryCode2.cc (rev 0) +++ problems/topcoder/BinaryCode/BinaryCode2.cc 2008-06-02 00:19:27 UTC (rev 841) @@ -0,0 +1,340 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { cout << __FILE__ << ":" << __LINE__ << ": "; pp(x); x; } +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T xs, U x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T x) { stringstream s; s << x; return s.str(); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powl(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void print(const T & x) { cout << x << endl; } +#define pp(x) cout << #x << ' ' << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + bounds(i,xs) os << i ? ", " : "{ " << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + bounds(i,xs) os << i ? ", " : "{ " << xs[i]; + return os << " }"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class BinaryCode { + public: + vector <string> decode(string message) { +#if 0 + vector <string> res(2); + string & msg = message; + for (int init = 0; init <= 1; init++) { + bool bad = false; + vi xs(msg.sz); + xs[0] = init; + if (msg.sz > 1) { + int x = msg[0] - '0'; + xs[1] = x - xs[0]; + + for (int i = 2; i < xs.sz; i++) { + int x = msg[i-1] - '0'; + xs[i] = x - xs[i-1] - xs[i-2]; + pp(i); + pp(x); + pp(xs[i]); + pp(xs[i-1]); + pp(xs[i-2]); + if (xs[i] != 0 && xs[i] != 1) event(bad = true); + cout << endl; + } + if (msg[msg.sz - 1] - '0' != xs[xs.sz - 1] + xs[xs.sz - 2]) { + event(bad = true); + } + } else { + if (msg[0] - '0' != xs[0]) event(bad = true); + } + + ping; + res[init] = bad ? string("NONE") : vi2str(xs); + pong; + } + return res; +#endif +#if 0 + vector <string> res(2); + string& msg = message; + for (int init = 0; init < 2; init++) { + vi xs(msg.sz); + bool bad = false; + int ln = msg.sz; + xs[0] = init; + for (int i = 1; i < msg.sz; i++) { + int m = msg[i-1] - '0'; + int x = xs[i] = m - xs[i-1] - (i-2>=0 ? xs[i-2] : 0); + if (x != 0 && x != 1) bad = true; + } + if (msg[ln - 1] - '0' != xs[ln-1] + (ln > 1 ? xs[ln-2] : 0)) + bad = true; + res[init] = bad ? string("NONE") : vi2str(xs); + } + return res; +#endif + vector<string> res(2); + string & msg = message; + for (int init = 0; init < 2; init++) { + int ln = msg.sz; + vi xs(ln); + xs[0] = init; + bool bad = false; + for (int i = 1; i < ln; i++) { + int m = msg[i-1] - '0'; + xs[i] = m - xs[i-1] - (i-2>=0 ? xs[i-2] : 0); + if (xs[i] != 0 && xs[i] != 1) bad = true; + } + if (msg[ln-1] - '0' != xs[ln-1] + xs[ln-2]) + bad = true; + res[init] = bad ? string("NONE") : vi2str(xs); + } + return res; + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + string retrunValueARRAY[] = { "011100011", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(0, theObject.decode("123210122"),retrunValue); + } + { + string retrunValueARRAY[] = { "01", "10" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(1, theObject.decode("11"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "11001" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(2, theObject.decode("22111"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(3, theObject.decode("123210120"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(4, theObject.decode("3"),retrunValue); + } + { + string retrunValueARRAY[] = { "01101001101101001101001001001101001", "10110010110110010110010010010110010" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(5, theObject.decode("12221112222221112221111111112221111"),retrunValue); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE Added: problems/topcoder/Bonuses/Bonuses2.cc =================================================================== --- problems/topcoder/Bonuses/Bonuses2.cc (rev 0) +++ problems/topcoder/Bonuses/Bonuses2.cc 2008-06-02 00:19:27 UTC (rev 841) @@ -0,0 +1,325 @@ +// vim:et:sw=2:ts=2 + +// TODO +// - strip out all comments +// - trim the unused includes/macros/typedefs/code + +// $BEGINREUSE$ +#include <algorithm> +#include <cassert> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <deque> +#include <functional> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <stack> +#include <string> +#include <utility> +#include <vector> +using namespace std; + +#define dd(x) cout << #x << " = " << x << endl +#define len length() +#define sz size() +#define pb(x) push_back((x)); +#define all(A) (A).begin(), (A).end() +#define rall(A) (A).rbegin(), (A).rend() +#define each(it,xs) for (typeof(xs.begin()) it = xs.begin(); it != xs.end(); it++) +#define bounds(i,xs) for (typeof((xs).size()) i = 0; i < (xs).size(); i++) +#define range(i,l,h) for (typeof(l) i = (l); i < (h); i++) +#define event(x) { cout << __FILE__ << ":" << __LINE__ << ": " << #x << endl; x; } +#define trace(x) { \ + typeof(x) __x = x; \ + cout << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << __x << endl; \ + __x; \ +} +#define ping cout << "ping" << endl; +#define pong cout << "pong" << endl; + +// CHOOSE +//int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1}; +//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1}; + +typedef vector<int> vi; +typedef vector<string> vs; +typedef vector<bool> vb; +typedef vector<vi> vvi; +typedef vector<vs> vvs; +typedef vector<vb> vvb; +typedef long long ll; +#define vec(x...) vector< x > +typedef string str; + +template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; } +template<typename T> inline void rev(T & xs) { std::reverse(all(xs)); } +template<typename T> inline void sort(T & xs) { std::sort(all(xs)); } +template<typename T> inline void ssort(T & xs) { std::stable_sort(all(xs)); } +template<typename T> inline void unique(T & xs) { std::unique(all(xs)); } +template<typename T> inline void uniq(T & xs) { xs.erase( std::unique(all(xs)), xs.end() ); } +template<typename T, typename U> inline void fill(T & xs, U & x) { std::fill(all(xs), x); } +template<typename T, typename U> inline U minim(const T & xs) { return *std::min_element(all(xs)); } +template<typename T, typename U> inline U maxim(const T & xs) { return *std::max_element(all(xs)); } +template<typename T> inline void nextp(T & xs) { return std::next_permutation(all(xs)); } +template<typename T> inline void prevp(T & xs) { return std::prev_permutation(all(xs)); } +template<typename T> inline string tos(T & x) { stringstream s; s << x; return s.str(); } +// pow, powl, powf are std +inline double powd(double a, double b) { return std::pow(a, b); } +inline int powi(int a, int b) { return int( std::pow(double(a), double(b)) ); } +inline int powll(ll a, ll b) { return ll( std::pow(double(a), double(b)) ); } +template<typename T> inline void pl(const T & x) { cout << x << endl; } +template<typename T, typename U> inline pair<T,U> mkpair(T t, U u) { return make_pair(t,u); } +#define pp(x) cout << #x << " = " << x << endl; + +inline ll gcd(ll a, ll b) { + if (a < 0 && b < 0) return gcd(-a,-b); + if (a == 0) return b; + if (b == 0) return a; + if (a > b) return gcd(b, a); + if (!(b % a)) return a; + return gcd(a, b % a); +} + +// TODO merge the following two pieces of code together + +template <typename T> +inline ostream& operator << (ostream& os, const set<T> & xs) { + os << "{ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " }"; +} + +template <typename T> +inline ostream& operator << (ostream& os, const vector<T> & xs) { + os << "[ "; + bounds(i,xs) os << (i ? ", " : "") << xs[i]; + return os << " ]"; +} + +template<class S,class T> +inline ostream & operator << (ostream & os, const pair<S,T> & a) { + os << "(" << a.first << ", " << a.second << ")"; + return os; +} + +vs split( const string & s, const string & delim = " " ) { + vs res; + string t; + each(c,s) { + if ( delim.find( *c ) != string::npos ) { + if ( !t.empty() ) { + res.pb( t ); + t = ""; + } + } else { + t += *c; + } + } + if ( ! t.empty() ) { + res.push_back(t); + } + return res; +} + +vs splitstr( const str & s, const str & glue = " " ) { + vs res; + str::size_type i = 0; + str::size_type ln = glue.sz; + while (true) { + str::size_type pos = s.find(glue, i); + res.push_back(string(s, i, pos)); + if (pos == str::npos) break; + i = pos + ln; + } + return res; +} + +vi ints( const str & s, const str & delim = " " ) { + vs ss = split( s, delim ); + vi is; + each(s,ss) is.push_back( atoi( s->c_str() ) ); + return is; +} + +string vi2str( const vi xs ) { + string s(xs.sz, '\0'); + bounds(i,xs) s[i] = xs[i] + '0'; + return s; +} + +template<typename T, typename U> inline U realfact(T x) { + ll f = 1; + range(i,1,x+1) f *= i; + return f; +} + +template<typename T> inline ll factl(T x) { return realfact<T,ll>(x); } +template<typename T> inline int facti(T x) { return realfact<T,int>(x); } + +ll perms(ll n, ll r) { + assert(n >= r); + ll p = 1; + for (ll i = n; i > n-r; i--) p *= i; + return p; +} + +ll combs(ll n, ll k) { + return perms(n,k) / factl(k); +} + +// following is needed for 'main' +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) +// $ENDREUSE$ + + + + + + + +// BEGIN CUT HERE +template<typename T> void print( T a ) { + cerr << a; +} +void print( long long a ) { + cerr << a << "L"; +} +void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + bounds(i,a) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + + + + + + + + + + + + + +// asdf +class Bonuses { + public: + vector <int> getDivision(vector <int> points) { +#if 0 + vector <int> res(points.size()); + int sum = 0; + int n = points.size(); + int rem = 100; + for (int i = 0; i < n; i++) sum += points[i]; + vector< pair<int,int> > a(n); + vector<int> p(n); + for (int i = 0; i < n; i++) { + a[i] = make_pair( -points[i], i ); + p[i] = 100 * points[i] / sum; + rem -= p[i]; + } + std::sort(a.begin(), a.end()); + //pp(a); + for (int i = 0; i < n; i++) { + if (rem == 0) break; + p[a[i].second]++; + rem--; + } + for (int i = 0; i < n; i++) + res[i] = p[i]; + return res; +#endif + } +}; + + + + + + + +// BEGIN CUT HERE +int main() { + { + int pointsARRAY[] = {1,2,3,4,5}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 6, 13, 20, 27, 34 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(0, theObject.getDivision(points),retrunValue); + } + { + int pointsARRAY[] = {5,5,5,5,5,5}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 17, 17, 17, 17, 16, 16 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(1, theObject.getDivision(points),retrunValue); + } + { + int pointsARRAY[] = {485, 324, 263, 143, 470, 292, 304, 188, 100, 254, 296, + 255, 360, 231, 311, 275, 93, 463, 115, 366, 197, 470}; + vector <int> points( pointsARRAY, pointsARRAY+ARRSIZE(pointsARRAY) ); + int retrunValueARRAY[] = { 8, 6, 4, 2, 8, 5, 5, 3, 1, 4, 5, 4, 6, 3, 5, 4, 1, 8, 1, 6, 3, 8 }; + vector <int> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + Bonuses theObject; + eq(2, theObject.getDivision(points),retrunValue); + } +#ifdef WIN32 + cin.get(); +#endif + return 0; +} +// END CUT HERE This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |