The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C++ Code
Posted by: SRM 363 Div 2 1000 | February 13, 2011 @ 8:24am
C++ Code
[
Download
]
/* FORHAD-SUST-BD */ #include <iostream> #include <string> #include <map> #include <queue> #include <stack> #include <algorithm> #include <list> #include <set> #include <cmath> #include <cstring> #include <stdio.h> #include <string.h> #include <sstream> #include <stdlib.h> #include <vector> #include <iomanip> using namespace std; #ifdef __GNUC__ typedef long long ll;typedef unsigned long long ull; #else typedef __int64 ll; typedef unsigned __int64 ull; #endif #define INF 1<<28 #define SIZE 100 #define PI 2*acos(0) #define fg %I64d #define REP(i,n) for (i=0;i<n;i++) #define REV(i,n) for (i=n;i>=0;i--) #define FOR(i,p,k) for (i=p; i<k;i++) #define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it) #define bug(x) cout<< "->" <<#x<<": "<<x<<endl #define Sort(x) sort(x.begin(),x.end()) #define Reverse(x) reverse(x.begin(),x.end()) #define MP(a,b) make_pair(a,b) #define Clear(x,with) memset(x,with,sizeof(x)) #define Copy(c,r) memcpy(c,r,sizeof(r)) #define SZ(x) (int)x.size() #define length(x) (int)x.length() #define All(x) x.begin(),x.end() #define pb push_back #define popcount(i) __builtin_popcount(i) #define gcd(a,b) __gcd(a,b) #define fs first #define sc second #define two(X) (1<<(X)) #define twoL(X) (((int64)(1))<<(X)) #define contain(S,X) (((S)&two(X))!=0) #define containL(S,X) (((S)&twoL(X))!=0) typedef pair<int,int> pii; typedef pair<double,double> pdd; typedef vector<int> vi; typedef vector<double>vd; typedef vector<ll>vll; typedef vector<string> vs; typedef vector<vi>vvi; typedef vector<vll>vvll; typedef vector<vd>vvd; typedef vector<pii>vpii; typedef map<string,int> msi; typedef map<int,int>mii; typedef map<pii,int>mpi; template<class T> inline T sqr(T x){return x*x;} template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;} template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No. template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();} int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;} template<class T> void debug(const T& e){cout<<e<<endl;} template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;} template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;} template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;} template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;} template<class T> void debug(vector<T>& e){int i;REP(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;} template<class T> void debug(vector< basic_string<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;} template<class T> void debug(vector< vector<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;} template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}} ll Pow(ll B,ll P){ ll R=1; while(P>0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p //struct pq{ int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue //bool comp(pq a,pq b){ return a.cost > b.cost;} //Comp Sort //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction //int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction //int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction double dp[1<<15][55]; vi profit; int N,K,req[15]; double rec(int mask,int k) { double &ret=dp[mask][k]; int i; if(ret>-INF) return ret; if(k==K) return 0; ret=0; REP(i,N) { if((mask&req[i])!=req[i]) { ret+=rec(mask,k+1); } else { ret+=max(rec(mask|two(i),k+1)+profit[i],rec(mask,k+1)); } } ret/=N; return ret; } class CrazyComponents { public: double expectedProfit(int k, vector <string> components, vector <int> income, vector <int> expense) { int i,j,val; double res; N=SZ(income); profit.clear(); K=k; REP(i,N) { istringstream iss(components[i]); req[i]=0; while(iss>>val) { req[i]|=two(val); } profit.pb(income[i]-expense[i]); } REP(i,two(N)) REP(j,K) dp[i][j]=-INF; res=rec(0,0); return res; } }; 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 << "{"; for ( int i = 0 ; i != a.size() ; i++ ) { if ( i != 0 ) cerr << ", "; print( a[i] ); } cerr << "}" << endl; } template<typename T> void assert_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 assert_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."; print( have ); print( need ); return; } for( int i= 0; i < have.size(); i++ ) { if( have[i] != need[i] ) { cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "."; print( have ); print( need ); return; } } cerr << "Case " << n << " passed." << endl; } void assert_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; } } int main( int argc, char* argv[] ) { CrazyComponents objectCrazyComponents; //test case0 int param00 = 1; vector <string> param01; param01.push_back(""); param01.push_back(""); vector <int> param02; param02.push_back(1); param02.push_back(2); vector <int> param03; param03.push_back(0); param03.push_back(0); double ret0 = objectCrazyComponents.expectedProfit(param00,param01,param02,param03); double need0 = 1.5; assert_eq(0,ret0,need0); //test case1 int param10 = 2; vector <string> param11; param11.push_back("1"); param11.push_back(""); vector <int> param12; param12.push_back(10); param12.push_back(0); vector <int> param13; param13.push_back(0); param13.push_back(2); double ret1 = objectCrazyComponents.expectedProfit(param10,param11,param12,param13); double need1 = 1.5; assert_eq(1,ret1,need1); //test case2 int param20 = 3; vector <string> param21; param21.push_back("1 2"); param21.push_back(""); param21.push_back(""); vector <int> param22; param22.push_back(100); param22.push_back(0); param22.push_back(0); vector <int> param23; param23.push_back(0); param23.push_back(0); param23.push_back(0); double ret2 = objectCrazyComponents.expectedProfit(param20,param21,param22,param23); double need2 = 7.407407407407408; assert_eq(2,ret2,need2); //test case3 int param30 = 5; vector <string> param31; param31.push_back("1"); param31.push_back("2"); param31.push_back("0"); param31.push_back(""); vector <int> param32; param32.push_back(4); param32.push_back(5); param32.push_back(6); param32.push_back(7); vector <int> param33; param33.push_back(0); param33.push_back(0); param33.push_back(0); param33.push_back(8); double ret3 = objectCrazyComponents.expectedProfit(param30,param31,param32,param33); double need3 = 0.0; assert_eq(3,ret3,need3); //test case4 int param40 = 10; vector <string> param41; param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); param41.push_back(""); vector <int> param42; param42.push_back(142352); param42.push_back(2342); param42.push_back(34534); param42.push_back(2344); param42.push_back(12346); param42.push_back(7589); param42.push_back(79872); param42.push_back(973453); param42.push_back(96233); param42.push_back(34567); vector <int> param43; param43.push_back(12432); param43.push_back(2345); param43.push_back(3678); param43.push_back(456); param43.push_back(345); param43.push_back(457); param43.push_back(56758); param43.push_back(4564); param43.push_back(774); param43.push_back(34567); double ret4 = objectCrazyComponents.expectedProfit(param40,param41,param42,param43); double need4 = 1269258.9999999998; assert_eq(4,ret4,need4); }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.50 / 8
52 Votes