Created
October 30, 2012 19:43
-
-
Save SudhagarS/3982525 to your computer and use it in GitHub Desktop.
My solutions for USACO train.usaco.org, part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ID: sudhaga1 | |
PROG: milk | |
LANG: C++ | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
#define MAXCOSTS 1001 | |
int main() { | |
ofstream fout ("milk.out"); | |
ifstream fin ("milk.in"); | |
int req, farmers; | |
fin >> req >> farmers; | |
int available[MAXCOSTS] = {0}; | |
for(int i = 0; i < farmers; ++i){ | |
int price, amt; | |
fin >> price >> amt; | |
available[price % MAXCOSTS] += amt; | |
} | |
int cost = 0; | |
for(int i = 0; i < MAXCOSTS; ++i){ | |
if(available[i]){ | |
if(available[i] >= req){ | |
cost += i * req; | |
break; | |
} | |
else{ | |
cost += i * available[i]; | |
req -= available[i]; | |
} | |
} | |
} | |
fout << cost << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ID:sudhaga1 | |
LANG:C++ | |
TASK:packrec | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <stdio.h> | |
#include <sstream> | |
#include <algorithm> | |
#include <queue> | |
#include <vector> | |
#include <set> | |
#include <map> | |
#include <cmath> | |
#include <string.h> | |
#include <fstream> | |
using namespace std ; | |
#define _(x,a) memset(x,a,sizeof(x)) | |
#define LET(x,a) __typeof(a) x(a) | |
#define FOR(i,a,b) for(LET(i,a);i!=(b);++i) | |
#define REP(i,n) FOR(i,0,n) | |
//Globals | |
int answer=1e9; | |
int count_=0; | |
int diffdimensions[100]; | |
//rectangle | |
struct Rectangle{ | |
int l,w; | |
void rotate(){ int temp=l; l=w; w=temp; } | |
}rec[4]; | |
//to store pairs(dimensions) that give minimum area | |
struct Pair | |
{ | |
int a,b; | |
} p[100]; | |
//function to pass as comparator for sorting array of pairs | |
bool pair_sorter(const Pair &p1, const Pair &p2){ | |
return p1.a<p2.a; | |
} | |
//compute the area for given length and width, if area smaller than current area make this current area | |
void computeArea(int len,int width){ | |
int area=len*width; | |
//width must be lesser than length --- output constraint | |
if(len<width){ | |
int temp=width; | |
width=len; | |
len=temp; | |
} | |
if (area == answer ) { | |
//if the already found area has any dimension same as this width then ignore | |
for (int i = 0; i < count_; i++) | |
{ | |
if(p[i].a==width){ | |
return; | |
} | |
} | |
p[count_].a=width; | |
p[count_].b=len; | |
count_ ++; | |
} | |
if (area < answer ) { | |
count_=0; | |
answer =area; | |
p[count_].a=width; | |
p[count_].b=len; | |
count_ ++; | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
ifstream fin ("packrec.in") ; | |
ofstream fout ("packrec.out") ; | |
int perm[]={0,1,2,3}; | |
int width,length; | |
REP(i,4){ | |
fin>>rec[i].w>>rec[i].l; | |
} | |
do{ | |
Rectangle r1=rec[perm[0]],r2=rec[perm[1]],r3=rec[perm[2]],r4=rec[perm[3]]; | |
for (int a = 0; a < 2; ++a,r1.rotate()) | |
{ | |
for (int b = 0; b < 2; ++b,r2.rotate()) | |
{ | |
for (int c = 0; c < 2; ++c,r3.rotate()) | |
{ | |
for (int d = 0; d < 2; ++d,r4.rotate()) | |
{ | |
//structure 1 | |
width=r1.w+r2.w+r3.w+r4.w; | |
length=max(r1.l,max(r2.l,max(r3.l,r4.l))); | |
computeArea(width,length); | |
//structure 2 | |
width=max(r1.w+r2.w+r3.w,r4.w); | |
length=max(r1.l,max(r2.l,r3.l))+r4.l; | |
computeArea(width,length); | |
//structure 3 | |
width=r4.w+max(r3.w,r1.w+r2.w); | |
length=max(r4.l,max(r1.l,r2.l)+r3.l); | |
computeArea(width,length); | |
//structure 4 and 5 | |
width=r1.w+r4.w+max(r2.w,r3.w); | |
length=max(r1.l,max(r4.l,r2.l+r3.l)); | |
computeArea(width,length); | |
//structure 6 | |
length=max(r1.l+r2.l, r3.l+r4.l); | |
if(r2.l>=r3.l+r4.l) width=max(r1.w,max(r2.w+r3.w,r2.w+r4.w)); | |
else if(r2.l>r4.l && r2.l<r3.l+r4.l) width=max(r1.w+r3.w,max(r2.w+r4.w,r2.w+r3.w)); | |
else if(r4.l>r2.l && r4.l<r1.l+r2.l) width=max(r1.w+r3.w,max(r2.w+r4.w,r1.w+r4.w)); | |
else if(r4.l>=r1.l+r2.l) width=max(r3.w,max(r4.w+r1.w,r4.w+r2.w)); | |
else if(r2.l==r4.l) width=max(r2.w+r4.w,r1.w+r3.w); | |
computeArea(width,length); | |
} | |
} | |
} | |
} | |
} while(next_permutation (perm,perm+4) ); | |
fout<<answer<<endl; | |
//sort the pairs | |
sort(p,p+count_,&pair_sorter); | |
for (int i = 0; i < count_; ++i) | |
{ | |
fout<<p[i].a<<" "<<p[i].b<<endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
PROB:palsquare | |
ID:sudhaga1 | |
LANG:JAVA | |
*/ | |
import java.util.*; | |
import java.io.*; | |
public class palsquare { | |
public static String toBase(int i,int base){ | |
char[] digits="0123456789ABCDEFGHIJKL".toCharArray(); | |
String ret=""; | |
do{ | |
ret+=digits[i%base]; | |
}while((i=i/base)>0); | |
return new StringBuffer(ret).reverse().toString(); | |
} | |
public static void main(String args[]) throws IOException{ | |
//Scanner sc=new Scanner(System.in); | |
BufferedReader br=new BufferedReader(new FileReader("palsquare.in")); | |
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out"))); | |
int base=Integer.parseInt(br.readLine()); | |
for(int i=1;i<=300;i++){ | |
String n=toBase(i,base); | |
String n2=toBase(i*i,base); | |
String reverse=new StringBuffer(n2).reverse().toString(); | |
if(n2.equals(reverse))out.println(n+" "+n2); | |
} | |
out.close(); | |
br.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ID: sudhaga1 | |
PROG: transform | |
LANG: C++ | |
*/ | |
#include <iostream> | |
#include <set> | |
#include <map> | |
#include <string> | |
#include <stdio.h> | |
#include <sstream> | |
#include <algorithm> | |
#include <queue> | |
#include <cmath> | |
#include <string.h> | |
#include <fstream> | |
#define sz size() | |
#define pb push_back | |
#define _(x,a) memset(x,a,sizeof(x)) | |
#define LET(x,a) __typeof(a) x(a) | |
#define FOR(i,a,b) for(LET(i,a);i!=(b);++i) | |
#define REP(i,n) FOR(i,0,n) | |
#define EACH(i,v) FOR(i,(v).begin(),(v).end()) | |
#define LL long long | |
#define GI ({int t;scanf("%d",&t);t;}) | |
#define GL ({LL t;scanf("%lld",&t);t;}) | |
#define GD ({double t;scanf("%lf",&t);t;}) | |
using namespace std ; | |
template <class T> struct matrix | |
{ | |
int size; | |
T** storage; | |
matrix(int _size){ | |
size=_size; | |
storage= new T*[size]; | |
REP(i,size) storage[i]=new T[size]; | |
} | |
~matrix(){ | |
REP(i,size) delete[] storage[i]; | |
delete[] storage; | |
} | |
bool operator == (const matrix &t){ | |
REP(x,size)REP(y,size)if(storage[x][y]!=t.storage[x][y])return false; | |
return true; | |
} | |
void rot90(){ | |
T old[size][size]; | |
REP(x,size) REP(y,size) old[x][y]=storage[x][y]; | |
REP(x,size) REP(y,size) storage[y][size-1-x]=old[x][y]; | |
} | |
void reflect(){ | |
T old[size][size]; | |
REP(x,size) REP(y,size) old[x][y]=storage[x][y]; | |
REP(x,size) REP(y,size) storage[x][y]=old[x][size-1-y]; | |
} | |
void printmatrix(){ | |
REP(x,size) {REP(y,size) cout<<storage[x][y]; cout<<endl;}; | |
} | |
}; | |
int main() | |
{ | |
int N; | |
ifstream fin ("transform.in"); | |
ofstream fout ("transform.out"); | |
fin>>N; | |
//initialise matrices | |
matrix<char> m1(N),m2(N); | |
//read them | |
REP(x,N){string in;fin>>in;REP(y,N)m1.storage[x][y]=in[y];} | |
REP(x,N){string in;fin>>in;REP(y,N)m2.storage[x][y]=in[y];} | |
//check all the conditions | |
m1.rot90(); | |
if(m1==m2){ | |
cout<<1; | |
fout<<1; | |
} | |
else{ | |
m1.rot90(); | |
if(m1==m2){ | |
fout<<2; | |
cout<<2; | |
} | |
else{ | |
m1.rot90(); | |
if(m1==m2){ | |
fout<<3; | |
cout<<3; | |
} | |
else{ | |
m1.rot90();//back to original layout | |
m1.reflect(); | |
if(m1==m2){ | |
fout<<4; | |
cout<<4; | |
} | |
else{ | |
bool found=false; | |
REP(i,3){ | |
m1.rot90(); | |
if(m1==m2){ | |
found=true; | |
fout<<5; | |
cout<<5; | |
break; | |
} | |
} | |
if(!found){ | |
m1.rot90(); | |
if(m1==m2){ | |
cout<<6; | |
fout<<6; | |
} | |
else{ | |
fout<<7; | |
cout<<7; | |
} | |
} | |
} | |
} | |
} | |
} | |
fout<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment