├── .gitignore ├── README.md ├── compile ├── run ├── spoj └── nkmobile.cpp ├── spojbr └── homem.cpp ├── timus ├── 1017.cpp ├── 1018.cpp ├── 1020.cpp ├── 1030.cpp ├── 1111.cpp ├── 1159.cpp ├── 1185.cpp ├── 1258.cpp ├── 1332.cpp ├── 1373.cpp ├── 1422.cpp ├── 1578.cpp └── 1658.cpp ├── tools ├── Boo.Lang.CodeDom.dll ├── Boo.Lang.Compiler.dll ├── Boo.Lang.Extensions.dll ├── Boo.Lang.Interpreter.dll ├── Boo.Lang.Parser.dll ├── Boo.Lang.PatternMatching.dll ├── Boo.Lang.Useful.dll ├── Boo.Lang.dll ├── Boo.Microsoft.Build.targets ├── Boo.NAnt.Tasks.dll ├── booc.exe ├── booc.exe.config ├── booc.rsp ├── booi.exe ├── booi.exe.config ├── booish.exe ├── booish.exe.config ├── old │ ├── compiled.css │ ├── merge.rb │ ├── shBrushCpp.js │ ├── shCore.js │ └── shCoreDefault.css └── update.boo ├── uri ├── 1001.cpp ├── 1002.cpp ├── 1003.cpp ├── 1004.cpp ├── 1005.cpp ├── 1006.cpp ├── 1007.cpp ├── 1008.cpp ├── 1009.cpp ├── 1010.cpp ├── 1011.cpp ├── 1012.cpp ├── 1013.cpp ├── 1014.cpp ├── 1015.cpp ├── 1016.cpp ├── 1017.cpp ├── 1018.cpp ├── 1019.cpp ├── 1020.cpp ├── 1021.cpp ├── 1035.cpp ├── 1036.cpp ├── 1037.cpp ├── 1038.cpp ├── 1040.cpp ├── 1041.cpp ├── 1042.cpp ├── 1043.cpp ├── 1044.cpp ├── 1045.cpp ├── 1046.cpp ├── 1047.cpp ├── 1048.cpp ├── 1049.cpp ├── 1050.cpp ├── 1051.cpp ├── 1052.cpp ├── 1059.cpp ├── 1060.cpp ├── 1424.cpp ├── 1435.cpp ├── 1519.cpp ├── 1520.cpp ├── 1521.cpp ├── 1523.cpp ├── 1524.cpp ├── 1525.cpp ├── 1527.cpp ├── 1529.cpp ├── 1556.cpp └── 2244.cpp └── uva ├── 10003.cpp ├── 10015.cpp ├── 10018.cpp ├── 10035.cpp ├── 10044.cpp ├── 10051.cpp ├── 10065.cpp ├── 10066.cpp ├── 10090.cpp ├── 10092.cpp ├── 10104.cpp ├── 10113.cpp ├── 10154.cpp ├── 10158.cpp ├── 1016.cpp ├── 10189.cpp ├── 10192.cpp ├── 10199.cpp ├── 10243.cpp ├── 10259.cpp ├── 10278.cpp ├── 10298.cpp ├── 10300.cpp ├── 10304.cpp ├── 10316.cpp ├── 10319.cpp ├── 10389.cpp ├── 10397.cpp ├── 10420.cpp ├── 10444.cpp ├── 10462.cpp ├── 10480.cpp ├── 10511.cpp ├── 10557.cpp ├── 1056.cpp ├── 10594.cpp ├── 10635.cpp ├── 10652.cpp ├── 10684.cpp ├── 10696.cpp ├── 10723.cpp ├── 10724.cpp ├── 10739.cpp ├── 10746.cpp ├── 10783.cpp ├── 10793.cpp ├── 10827.cpp ├── 10891.cpp ├── 109.cpp ├── 10930.cpp ├── 10986.cpp ├── 11003.cpp ├── 11059.cpp ├── 11096.cpp ├── 11110.cpp ├── 11151.cpp ├── 11157.cpp ├── 11159.cpp ├── 11172.cpp ├── 11235.cpp ├── 11280.cpp ├── 11294.cpp ├── 11297.cpp ├── 11375.cpp ├── 11402.cpp ├── 11419.cpp ├── 11423.cpp ├── 11475.cpp ├── 11494.cpp ├── 11503.cpp ├── 11512.cpp ├── 11518.cpp ├── 11525.cpp ├── 11532.cpp ├── 11576.cpp ├── 1158.cpp ├── 11590.cpp ├── 11597.cpp ├── 11610.cpp ├── 11626.cpp ├── 11629.cpp ├── 11631.cpp ├── 11658.cpp ├── 11686.cpp ├── 11703.cpp ├── 11709.cpp ├── 11733.cpp ├── 1174.cpp ├── 11747.cpp ├── 11762.cpp ├── 11770.cpp ├── 11833.cpp ├── 11838.cpp ├── 11857.cpp ├── 119.cpp ├── 11966.cpp ├── 1197.cpp ├── 1200.cpp ├── 1203.cpp ├── 1205.cpp ├── 1207.cpp ├── 1208.cpp ├── 12086.cpp ├── 12101.cpp ├── 12103.cpp ├── 1213.cpp ├── 12135.cpp ├── 12137.cpp ├── 12144.cpp ├── 12147.cpp ├── 12148.cpp ├── 1215.cpp ├── 12155.cpp ├── 12159.cpp ├── 1216.cpp ├── 12160.cpp ├── 12168.cpp ├── 12172.cpp ├── 12179.cpp ├── 12184.cpp ├── 12186.cpp ├── 12189.cpp ├── 12190.cpp ├── 12192.cpp ├── 12194.cpp ├── 12195.cpp ├── 12196.cpp ├── 1220.cpp ├── 1223.cpp ├── 1229.cpp ├── 12300.cpp ├── 1231.cpp ├── 1232.cpp ├── 1233.cpp ├── 1234.cpp ├── 1235.cpp ├── 12361.cpp ├── 12363.cpp ├── 12365.cpp ├── 1239.cpp ├── 1246.cpp ├── 1247.cpp ├── 12482.cpp ├── 12483.cpp ├── 12484.cpp ├── 12485.cpp ├── 12486.cpp ├── 12487.cpp ├── 12488.cpp ├── 12489.cpp ├── 12490.cpp ├── 12491.cpp ├── 12492.cpp ├── 12493.cpp ├── 12506.cpp ├── 1251.cpp ├── 1263.cpp ├── 1265.cpp ├── 136.cpp ├── 160.cpp ├── 245.cpp ├── 273.cpp ├── 298.cpp ├── 314.cpp ├── 315.cpp ├── 321.cpp ├── 361.cpp ├── 422.cpp ├── 424.cpp ├── 458.cpp ├── 473.cpp ├── 494.cpp ├── 573.cpp ├── 579.cpp ├── 591.cpp ├── 610.cpp ├── 627.cpp ├── 652.cpp ├── 719.cpp ├── 760.cpp ├── 796.cpp ├── 811.cpp ├── 820.cpp ├── 825.cpp ├── 902.cpp ├── 907.cpp ├── 908.cpp ├── 929.cpp └── 986.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | uva/*.exe 2 | timus/*.exe 3 | *.*~ 4 | *~ 5 | *.sol 6 | *.in 7 | *.out 8 | /*.html 9 | -------------------------------------------------------------------------------- /compile: -------------------------------------------------------------------------------- 1 | cd $(dirname $0) 2 | ruby tools/old/merge.rb $* 3 | -------------------------------------------------------------------------------- /run: -------------------------------------------------------------------------------- 1 | cd $(dirname $0) 2 | mono tools/booi.exe tools/update.boo $* 3 | -------------------------------------------------------------------------------- /spoj/nkmobile.cpp: -------------------------------------------------------------------------------- 1 | //NKMOBILE 2 | //IOI01 Mobiles 3 | //Misc;Fenwick Tree;2D 4 | #include 5 | #include 6 | #define MAX 1030 7 | using namespace std; 8 | 9 | struct Fenwick2D { 10 | int T[MAX][MAX]; 11 | int n, m; 12 | 13 | void clear(int n, int m) { 14 | for(int i=1; i<=n; i++) 15 | for(int j=1; j<=m; j++) 16 | T[i][j] = 0; 17 | 18 | this->n = n; 19 | this->m = m; 20 | } 21 | 22 | void adjust(int x, int y, int v) { 23 | for (int i=x; i <= n; i += (i&-i)) 24 | for(int j=y; j <= m; j += (j&-j)) 25 | T[i][j] += v; 26 | } 27 | 28 | int rsq(int x, int y) { 29 | int sum = 0; 30 | for(int i=x; i; i -= (i&-i)) 31 | for(int j=y; j; j -= (j&-j)) 32 | sum += T[i][j]; 33 | return sum; 34 | } 35 | 36 | int rsq(int x1, int y1, int x2, int y2) { 37 | return rsq(x2, y2) - rsq(x2, y1-1) - rsq(x1-1, y2) + rsq(x1-1, y1-1); 38 | } 39 | }; 40 | 41 | Fenwick2D T; 42 | 43 | int main() { 44 | int cmd; 45 | while(cin >> cmd, cmd != 3) { 46 | if (cmd == 0) { 47 | int s; 48 | cin >> s; 49 | T.clear(s, s); 50 | } else if (cmd == 1) { 51 | int x, y, a; 52 | cin >> x >> y >> a; 53 | x++; y++; 54 | T.adjust(x, y, a); 55 | } else if (cmd == 2) { 56 | int x1, y1, x2, y2; 57 | cin >> x1 >> y1 >> x2 >> y2; 58 | x1++; y1++; x2++; y2++; 59 | cout << T.rsq(x1, y1, x2, y2) << endl; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /timus/1017.cpp: -------------------------------------------------------------------------------- 1 | //1017 2 | //Staircases 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #define MAX 506 6 | using namespace std; 7 | 8 | long long T[MAX][MAX]; 9 | 10 | int main() { 11 | for(int i=1;i> n) 21 | cout << T[n][1]-1 << endl; 22 | } 23 | -------------------------------------------------------------------------------- /timus/1018.cpp: -------------------------------------------------------------------------------- 1 | //1018 2 | //Binary Apple Tree 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #define MAX 105 8 | using namespace std; 9 | 10 | struct Node { 11 | int x, v; 12 | Node(int x, int v) : x(x), v(v) {} 13 | }; 14 | vector T[MAX]; 15 | int S[MAX], TT[MAX][MAX]; 16 | bool V[MAX][MAX]; 17 | 18 | void adjust(int root, int parent) { 19 | for(int i=0; i> n >> q) { 54 | memset(T, 0, sizeof(vector)*n); 55 | memset(V, 0, sizeof(vector)*n); 56 | memset(S, 0, sizeof(S)); 57 | 58 | for(int i=0; i> a >> b >> c; 60 | T[b].push_back(Node(a,c)); 61 | T[a].push_back(Node(b,c)); 62 | } 63 | adjust(1, -1); 64 | cout << answer(1, q+1) << endl; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /timus/1020.cpp: -------------------------------------------------------------------------------- 1 | //1020 2 | //Rope 3 | //Math;Geometry 4 | #include 5 | #include 6 | #include 7 | #define PI 3.14159265 8 | using namespace std; 9 | 10 | struct Point { 11 | double x, y; 12 | 13 | Point() {} 14 | 15 | double dist(Point A) { 16 | return sqrt(pow(A.x-x,2)+pow(A.y-y,2)); 17 | } 18 | }; 19 | 20 | int main() { 21 | int n; 22 | double r; 23 | Point a, b, c; 24 | while(cin >> n >> r) { 25 | double total = 0; 26 | 27 | cin >> a.x >> a.y; 28 | b = a; 29 | for(int i=1; i> c.x >> c.y; 31 | total += b.dist(c); 32 | swap(b, c); 33 | } 34 | total += a.dist(b); 35 | 36 | cout << fixed << setprecision(2) << total + 2*PI*r << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /timus/1030.cpp: -------------------------------------------------------------------------------- 1 | //1030 2 | //Titanic 3 | //Math;Geometry;Great-Circle Distance 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define PI 3.14159265 9 | using namespace std; 10 | 11 | double distance(double r, double x1, double y1, double x2, double y2) { 12 | return r*acos(sin(x1)*sin(x2) + cos(x1)*cos(x2)*cos(fabs(y1-y2))); 13 | } 14 | 15 | double readCoord() { 16 | int a1, a2, a3; char c; string s; 17 | cin >> a1 >> c >> a2 >> c >> a3 >> c >> s; 18 | double ret = (a1 + a2/60.0 + a3/3600.0) / 180.0 * PI; 19 | if (s=="WL." || s=="SL") 20 | ret = -ret; 21 | return ret; 22 | } 23 | 24 | double round(double d) 25 | { 26 | return floor(d + 0.5); 27 | } 28 | 29 | int main() { 30 | string s; 31 | 32 | getline(cin, s); 33 | getline(cin, s); 34 | getline(cin, s); 35 | 36 | double X1 = readCoord(); 37 | cin >> s; 38 | double Y1 = readCoord(); 39 | getline(cin, s); 40 | getline(cin, s); 41 | double X2 = readCoord(); 42 | cin >> s; 43 | double Y2 = readCoord(); 44 | 45 | double d = distance(6875.0/2, X1, Y1, X2, Y2); 46 | cout << "The distance to the iceberg: " << fixed << setprecision(2) << round(d*100)/100.0 << " miles." << endl; 47 | if (d < 99.995) 48 | cout << "DANGER!" << endl; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /timus/1185.cpp: -------------------------------------------------------------------------------- 1 | //1185 2 | //Wall 3 | //Math;Geometry;Convex Hull;Monotone Chain 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define PI 3.14159265 9 | using namespace std; 10 | 11 | struct Point { 12 | int x, y; 13 | 14 | Point() {} 15 | Point(int x, int y) : x(x), y(y) {} 16 | 17 | bool left(Point& a, Point& b) { 18 | return (this->x - a.x)*(b.y - a.y) - (this->y - a.y)*(b.x - a.x) < 0; 19 | } 20 | 21 | bool operator <(const Point& p) const { 22 | if (this->x != p.x) return this->x < p.x; 23 | return this->y < p.y; 24 | } 25 | 26 | bool operator ==(const Point& p) const { 27 | return this->x == p.x and this->y == p.y; 28 | } 29 | 30 | double dist(Point A) { 31 | return sqrt(pow(A.x-x,2.0)+pow(A.y-y,2.0)); 32 | } 33 | }; 34 | 35 | int convexHull(Point* P, int n, Point* S) { 36 | sort(P, P+n); 37 | 38 | int m=0; 39 | for(int i=0; i= 2 && S[m-1].left(S[m-2], P[i])) m--; 41 | S[m++] = P[i]; 42 | } 43 | m--; 44 | 45 | for(int i=n-1, k=m; i >= 0; i--) { 46 | while(m >= k+2 && S[m-1].left(S[m-2], P[i])) m--; 47 | S[m++] = P[i]; 48 | } 49 | m--; 50 | 51 | return m; 52 | } 53 | 54 | Point P[1010], S[1010]; 55 | 56 | int main() { 57 | int n, r; 58 | while(cin >> n >> r) { 59 | for(int i=0; i> P[i].x >> P[i].y; 61 | 62 | int s = convexHull(P, n, S); 63 | 64 | double total = 0; 65 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #define ull long long 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | ull W, D, a, b, c, d; 14 | string s; 15 | while(cin >> W >> D >> a >> b >> c >> d) { 16 | cin >> s; 17 | ull x=0, y=0; 18 | ull sr=1<<30, sl=1<<30, sf=1<<30, sb=1<<30; 19 | 20 | for(ull i=0; i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | double PI = 2*acos(0.0); 10 | 11 | struct Point { 12 | double x, y; 13 | 14 | Point() {} 15 | Point(double x, double y) : x(x), y(y) {} 16 | 17 | Point rotateWith(const Point origin, double si, double co, double scale) const { 18 | double tx = this->x - origin.x; 19 | double ty = this->y - origin.y; 20 | double x = (tx * co + ty * si)/scale; 21 | double y = (tx * -si + ty * co)/scale; 22 | return Point(origin.x + x, origin.y + y); 23 | 24 | } 25 | }; 26 | 27 | double round4(double a) { 28 | if (a<0) return 0.0; 29 | return floor(a*10000+0.5)/10000.0; 30 | } 31 | 32 | int main() { 33 | Point a, b; 34 | double minx=1<<30, maxx=0, miny=1<<30, maxy=0; 35 | while(cin >> a.x >> a.y >> b.x >> b.y) { 36 | Point c = b.rotateWith(a, sin(-PI/4), cos(-PI/4), sqrt(2.0)); 37 | 38 | minx = min(minx, min(min(a.x, b.x), c.x)); 39 | miny = min(miny, min(min(a.y, b.y), c.y)); 40 | maxx = max(maxx, max(max(a.x, b.x), c.x)); 41 | maxy = max(maxy, max(max(a.y, b.y), c.y)); 42 | } 43 | 44 | cout << fixed << setprecision(4) << round4(maxx - minx) << " " << round4(maxy - miny) << endl; 45 | } 46 | -------------------------------------------------------------------------------- /timus/1422.cpp: -------------------------------------------------------------------------------- 1 | //1422 2 | //Fireflies 3 | //Math;Geometry;3D Line Detection 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int gcd(int a, int b) { 10 | while(b) 11 | swap(a=a%b,b); 12 | return a; 13 | } 14 | 15 | struct Vector { 16 | int x, y, z; 17 | 18 | Vector() : x(0), y(0), z(0) {} 19 | Vector(int x, int y, int z) : x(x), y(y), z(z) {} 20 | 21 | 22 | Vector normalize() const { 23 | int d = gcd(x, gcd(y, z)); 24 | return Vector(x/d, y/d, z/d); 25 | } 26 | 27 | Vector operator -(const Vector& that) const { 28 | return Vector(x-that.x, y-that.y, z-that.z); 29 | } 30 | 31 | bool operator <(const Vector& that) const { 32 | if (x!=that.x) return x M; 42 | 43 | int main() { 44 | int n; 45 | while(cin >> n) { 46 | for(int i=0; i> P[i].x >> P[i].y >> P[i].z; 48 | 49 | int maxx = 0; 50 | for(int i=0; i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | short S[1010][9000]; 10 | short T[1010][9000]; 11 | 12 | int main() { 13 | T[0][0] = 1; 14 | for(int i=1; i<=1000; i++) { 15 | for(int j=1; j<=8100; j++) { 16 | S[i][j] = 102; 17 | for(int k=1; k<=9; k++) { 18 | int a = i-k; 19 | int b = j-k*k; 20 | if (a>=0 && b>=0 && T[a][b] && S[a][b]+1> t; 29 | while(t--) { 30 | int s1, s2; 31 | cin >> s1 >> s2; 32 | 33 | if (s1 > 1000 || s2 > 8100 || S[s1][s2] > 100) { 34 | cout << "No solution" << endl; 35 | continue; 36 | } 37 | 38 | int n = S[s1][s2]; 39 | for(int i=0; i 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /tools/booc.rsp: -------------------------------------------------------------------------------- 1 | # This file contains command-line options that the Boo 2 | # command-line compiler (booc) will process as part 3 | # of every compilation, unless the "-noconfig" option 4 | # is specified. 5 | 6 | # Reference the common Framework libraries 7 | #-r:System (already included by boo) 8 | #-r:System.Data 9 | -r:System.Drawing 10 | #-r:System.Management 11 | #-r:System.Messaging 12 | #-r:System.Runtime.Remoting 13 | #-r:System.Runtime.Serialization.Formatters.Soap 14 | # does not work nicely on mono 15 | -r:System.Security 16 | #-r:System.ServiceProcess 17 | #-r:System.Web 18 | #-r:System.Web.Services 19 | #-r:System.Windows.Forms 20 | -r:System.Xml 21 | -------------------------------------------------------------------------------- /tools/booi.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanplopes/icpc/3e65ffe9f59973714ff25f3f9bb061b98455f781/tools/booi.exe -------------------------------------------------------------------------------- /tools/booi.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /tools/booish.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanplopes/icpc/3e65ffe9f59973714ff25f3f9bb061b98455f781/tools/booish.exe -------------------------------------------------------------------------------- /tools/booish.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /tools/old/compiled.css: -------------------------------------------------------------------------------- 1 | a, p, ul { 2 | font-family: sans-serif; 3 | text-decoration: none; 4 | font-size: 16px; 5 | color: black; 6 | } 7 | 8 | h1, li p { 9 | page-break-after: avoid; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /tools/old/merge.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'redcarpet' 3 | require 'cgi' 4 | 5 | def markdown(text) 6 | options = [:hard_wrap, :filter_html, :autolink, :no_intraemphasis, :fenced_code, :gh_blockcode] 7 | RedcarpetCompat.new(text, *options).to_html 8 | end 9 | 10 | puts ' 11 | 12 | 13 | 14 | compiled' 15 | puts '' 18 | 19 | puts '' 22 | 23 | puts '' 26 | 27 | puts '' 30 | 31 | 32 | puts ' 33 | 34 | ' 35 | 36 | puts "" 37 | 38 | puts "
" 39 | puts markdown(File.read('README.md')) 40 | puts "
" 41 | 42 | puts "
" 43 | ['uva', 'timus', 'spoj', 'spojbr'].each do |dir| 44 | Dir.glob(File.join(dir, "*.cpp")).sort_by {|s| File.split(s)[1].split('.')[0].to_i }.each do |file| 45 | puts "

#{file}

" 46 | puts '
'
47 |         puts CGI::escapeHTML(File.read(file))
48 |         puts '
' 49 | end 50 | end 51 | puts "
" 52 | 53 | puts '' 54 | 55 | -------------------------------------------------------------------------------- /uri/1001.cpp: -------------------------------------------------------------------------------- 1 | //1001 2 | //Extremely Basic 3 | //Misc;Beginner 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b; 9 | while(cin >> a >> b) 10 | cout << "X = " << a+b << endl; 11 | } 12 | -------------------------------------------------------------------------------- /uri/1002.cpp: -------------------------------------------------------------------------------- 1 | //1002 2 | //Area of a Circle 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double pi = 3.14159; 10 | int r; 11 | while(cin >> r) 12 | cout << "A=" << fixed << setprecision(4) << pi*r*r << endl; 13 | } 14 | -------------------------------------------------------------------------------- /uri/1003.cpp: -------------------------------------------------------------------------------- 1 | //1003 2 | //Simple Sum 3 | //Misc;Beginner 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b; 9 | while(cin >> a >> b) 10 | cout << "SOMA = " << a+b << endl; 11 | } 12 | -------------------------------------------------------------------------------- /uri/1004.cpp: -------------------------------------------------------------------------------- 1 | //1004 2 | //Simple Product 3 | //Misc;Beginner 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b; 9 | while(cin >> a >> b) 10 | cout << "PROD = " << a*b << endl; 11 | } 12 | -------------------------------------------------------------------------------- /uri/1005.cpp: -------------------------------------------------------------------------------- 1 | //1005 2 | //Average 1 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double a, b; 10 | while(cin >> a >> b) 11 | cout << "MEDIA = " << fixed << setprecision(5) << (3.5*a+7.5*b)/11 << endl; 12 | } 13 | -------------------------------------------------------------------------------- /uri/1006.cpp: -------------------------------------------------------------------------------- 1 | //1006 2 | //Average 2 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double a, b, c; 10 | while(cin >> a >> b >> c) 11 | cout << "MEDIA = " << fixed << setprecision(1) << (2*a+3*b+5*c)/10 << endl; 12 | } 13 | -------------------------------------------------------------------------------- /uri/1007.cpp: -------------------------------------------------------------------------------- 1 | //1007 2 | //Difference 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int a, b, c, d; 10 | while(cin >> a >> b >> c >> d) 11 | cout << "DIFERENCA = " << a*b-c*d << endl; 12 | } 13 | -------------------------------------------------------------------------------- /uri/1008.cpp: -------------------------------------------------------------------------------- 1 | //1008 2 | //Salary 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int a, b; float c; 10 | while(cin >> a >> b >> c) { 11 | cout << "NUMBER = " << a << endl; 12 | cout << "SALARY = U$ " << fixed << setprecision(2) << b*c << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1009.cpp: -------------------------------------------------------------------------------- 1 | //1009 2 | //Salary with Bonus 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | string s; 11 | float a, b; 12 | while(cin >> s >> a >> b) { 13 | cout << "TOTAL = R$ " << fixed << setprecision(2) << a+b*0.15 << endl; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /uri/1010.cpp: -------------------------------------------------------------------------------- 1 | //1010 2 | //Simple Calculate 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | float a, b, c, d; 11 | while(cin >> a >> a >> b >> c >> c >> d) { 12 | cout << "VALOR A PAGAR: R$ " << fixed << setprecision(2) << a*b+c*d << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1011.cpp: -------------------------------------------------------------------------------- 1 | //1011 2 | //Sphere 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double pi = 3.14159; 10 | int r; 11 | while(cin >> r) 12 | cout << "VOLUME = " << fixed << setprecision(3) << pi*r*r*r*4/3 << endl; 13 | } 14 | -------------------------------------------------------------------------------- /uri/1012.cpp: -------------------------------------------------------------------------------- 1 | //1012 2 | //Area 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double pi = 3.14159; 10 | double a, b, c; 11 | while(cin >> a >> b >> c) { 12 | cout << "TRIANGULO: " << fixed << setprecision(3) << a*c/2 << endl; 13 | cout << "CIRCULO: " << fixed << setprecision(3) << pi*c*c << endl; 14 | cout << "TRAPEZIO: " << fixed << setprecision(3) << c*(a+b)/2 << endl; 15 | cout << "QUADRADO: " << fixed << setprecision(3) << b*b << endl; 16 | cout << "RETANGULO: " << fixed << setprecision(3) << a*b << endl; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /uri/1013.cpp: -------------------------------------------------------------------------------- 1 | //1013 2 | //The Greatest 3 | //Misc;Beginner 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b, c; 9 | while(cin >> a >> b >> c) { 10 | cout << max(a, max(b, c)) << " eh o maior" << endl; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /uri/1014.cpp: -------------------------------------------------------------------------------- 1 | //1014 2 | //Consumption 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double x, y; 10 | while(cin >> x >> y) { 11 | cout << fixed << setprecision(3) << x/y << " km/l" << endl; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /uri/1015.cpp: -------------------------------------------------------------------------------- 1 | //1015 2 | //Distance Between two points 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double x1, y1, x2, y2; 11 | while(cin >> x1 >> y1 >> x2 >> y2) { 12 | cout << fixed << setprecision(4) << sqrt(pow(x2-x1, 2) + pow(y2-y1,2)) << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1016.cpp: -------------------------------------------------------------------------------- 1 | //1016 2 | //Distance 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int x; 11 | while(cin >> x) { 12 | cout << x*2 << " minutos" << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1017.cpp: -------------------------------------------------------------------------------- 1 | //1017 2 | //Fuel Spent 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double a, b; 11 | while(cin >> a >> b) { 12 | cout << fixed << setprecision(3) << a*b/12 << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1018.cpp: -------------------------------------------------------------------------------- 1 | //1018 2 | //Banknotes 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int note(int x, int note) { 10 | int n = 0; 11 | while(x >= note) { 12 | n+=1; 13 | x-=note; 14 | } 15 | cout << n << " nota(s) de R$ " << note << ",00" << endl; 16 | return x; 17 | } 18 | 19 | int main() { 20 | int x; 21 | while(cin >> x) { 22 | cout << x << endl; 23 | x = note(x, 100); 24 | x = note(x, 50); 25 | x = note(x, 20); 26 | x = note(x, 10); 27 | x = note(x, 5); 28 | x = note(x, 2); 29 | x = note(x, 1); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /uri/1019.cpp: -------------------------------------------------------------------------------- 1 | //1019 2 | //Time Conversion 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int x; 11 | while(cin >> x) { 12 | cout << x/60/60 << ":" << x/60%60 << ":" << x%60 << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1020.cpp: -------------------------------------------------------------------------------- 1 | //1020 2 | //Age in Days 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int x; 11 | while(cin >> x) { 12 | cout << x/365 << " ano(s)" << endl; 13 | cout << x%365/30 << " mes(es)" << endl; 14 | cout << x%365%30 << " dia(s)" << endl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /uri/1021.cpp: -------------------------------------------------------------------------------- 1 | //1021 2 | //Banknotes and Coins 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | double note(double x, string type, double note) { 10 | int n = 0; 11 | while(x >= note) { 12 | n+=1; 13 | x-=note; 14 | } 15 | cout << n << " " << type << "(s) de R$ " << fixed << setprecision(2) <> x) { 22 | cout << "NOTAS:" << endl; 23 | x = note(x, "nota", 100); 24 | x = note(x, "nota", 50); 25 | x = note(x, "nota", 20); 26 | x = note(x, "nota", 10); 27 | x = note(x, "nota", 5); 28 | x = note(x, "nota", 2); 29 | 30 | cout << "MOEDAS:" << endl; 31 | x = note(x, "moeda", 1); 32 | x = note(x, "moeda", 0.50); 33 | x = note(x, "moeda", 0.25); 34 | x = note(x, "moeda", 0.10); 35 | x = note(x, "moeda", 0.05); 36 | x = note(x, "moeda", 0.01); 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /uri/1035.cpp: -------------------------------------------------------------------------------- 1 | //1035 2 | //Selection Test 1 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int a, b, c, d; 11 | while(cin >> a >> b >> c >> d) { 12 | if (b>c and d>a and c+d>a+b and c>0 and d>0 and a%2==0) 13 | cout << "Valores aceitos" << endl; 14 | else 15 | cout << "Valores nao aceitos" << endl; 16 | 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /uri/1036.cpp: -------------------------------------------------------------------------------- 1 | //1036 2 | //Bhaskara's Fomula 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double a, b, c; 11 | while(cin >> a >> b >> c) { 12 | double delta = b*b-4*a*c; 13 | if (a == 0 || delta < 0) { 14 | cout << "Impossivel calcular" << endl; 15 | continue; 16 | } 17 | double r1 = (-b + sqrt(delta))/(2*a); 18 | double r2 = (-b - sqrt(delta))/(2*a); 19 | cout << "R1 = " << fixed << setprecision(5) << r1 << endl; 20 | cout << "R2 = " << fixed << setprecision(5) << r2 << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /uri/1037.cpp: -------------------------------------------------------------------------------- 1 | //1037 2 | //Interval 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double x; 11 | while(cin >> x) { 12 | if (x >= 0 && x<=25) 13 | cout << "Intervalo [0,25]" << endl; 14 | else if (x > 25 && x<=50) 15 | cout << "Intervalo (25,50]" << endl; 16 | else if (x > 50 && x<=75) 17 | cout << "Intervalo (50,75]" << endl; 18 | else if (x > 75 && x<=100) 19 | cout << "Intervalo (75,100]" << endl; 20 | else 21 | cout << "Fora de intervalo" << endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /uri/1038.cpp: -------------------------------------------------------------------------------- 1 | //1038 2 | //Snack 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int x, y; 11 | while(cin >> x >> y) { 12 | double price = x==1 ? 4 : x==2 ? 4.5 : x==3 ? 5 : x==4 ? 2 : 1.5; 13 | cout << "Total: R$ " << fixed << setprecision(2) << y*price << endl; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /uri/1040.cpp: -------------------------------------------------------------------------------- 1 | //1040 2 | //Average 3 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double n1, n2, n3, n4, n5; 11 | while(cin >> n1 >> n2 >> n3 >> n4) { 12 | double media = (2*n1 + 3*n2 + 4*n3 + n4)/10; 13 | cout << fixed << setprecision(1); 14 | cout << "Media: " << media << endl; 15 | if (media >= 7.0) { 16 | cout << "Aluno aprovado." << endl; 17 | } else if (media < 5) { 18 | cout << "Aluno reprovado." << endl; 19 | } else { 20 | cin >> n5; 21 | cout << "Aluno em exame." << endl; 22 | cout << "Nota do exame: " << n5 << endl; 23 | 24 | 25 | media = (media + n5)/2; 26 | if (media >= 5.0) { 27 | cout << "Aluno aprovado." << endl; 28 | } else { 29 | cout << "Aluno reprovado." << endl; 30 | } 31 | cout << "Media final: " << media << endl; 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /uri/1041.cpp: -------------------------------------------------------------------------------- 1 | //1041 2 | //Coordinates of a Point 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double x, y; 11 | while(cin >> x >> y) { 12 | if (x>0 && y>0) 13 | cout << "Q1" << endl; 14 | 15 | if (x<0 && y>0) 16 | cout << "Q2" << endl; 17 | 18 | if (x<0 && y<0) 19 | cout << "Q3" << endl; 20 | 21 | if (x>0 && y<0) 22 | cout << "Q4" << endl; 23 | 24 | if (x==0 && y!=0) 25 | cout << "Eixo Y" << endl; 26 | 27 | if (x!=0 && y==0) 28 | cout << "Eixo X" << endl; 29 | 30 | if (x==0 && y==0) 31 | cout << "Origem" << endl; 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /uri/1042.cpp: -------------------------------------------------------------------------------- 1 | //1042 2 | //Simple Sort 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int V[3]; 10 | int main() { 11 | int a, b, c; 12 | while(cin >> a >> b >> c) { 13 | V[0] = a; 14 | V[1] = b; 15 | V[2] = c; 16 | sort(V, V+3); 17 | 18 | for(int i=0; i<3; i++) 19 | cout << V[i] << endl; 20 | cout << endl; 21 | 22 | cout << a << endl << b << endl << c << endl; 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /uri/1043.cpp: -------------------------------------------------------------------------------- 1 | //1043 2 | //Triangle 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double a, b, c; 10 | while(cin >> a >> b >> c) { 11 | cout << fixed << setprecision(1); 12 | if (a+b>c && a+c>b && b+c>a) { 13 | cout << "Perimetro = " << (a+b+c) << endl; 14 | } else { 15 | cout << "Area = " << (b*c + 0.5*(a-b)*c) << endl; 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /uri/1044.cpp: -------------------------------------------------------------------------------- 1 | //1044 2 | //Multiples 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int a, b; 10 | while(cin >> a >> b) { 11 | if (a%b==0 || b%a==0) { 12 | cout << "Sao Multiplos" << endl; 13 | } else { 14 | cout << "Nao sao Multiplos" << endl; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /uri/1045.cpp: -------------------------------------------------------------------------------- 1 | //1045 2 | //Triangle Types 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | double V[3]; 10 | int main() { 11 | while(cin >> V[0] >> V[1] >> V[2]) { 12 | sort(V, V+3, greater()); 13 | 14 | if (V[0] > V[1] + V[2]) 15 | cout << "NAO FORMA TRIANGULO" << endl; 16 | if (V[0]*V[0] == V[1]*V[1] + V[2]*V[2]) 17 | cout << "TRIANGULO RETANGULO" << endl; 18 | if (V[0]*V[0] > V[1]*V[1] + V[2]*V[2]) 19 | cout << "TRIANGULO OBTUSANGULO" << endl; 20 | if (V[0]*V[0] < V[1]*V[1] + V[2]*V[2]) 21 | cout << "TRIANGULO ACUTANGULO" << endl; 22 | if (V[0] == V[1] && V[1] == V[2]) 23 | cout << "TRIANGULO EQUILATERO" << endl; 24 | if (V[0] == V[1] ^ V[1] == V[2] ^ V[0] == V[2]) 25 | cout << "TRIANGULO ISOSCELES" << endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /uri/1046.cpp: -------------------------------------------------------------------------------- 1 | //1046 2 | //Game Time 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int a, b; 11 | while(cin >> a >> b) { 12 | cout << "O JOGO DUROU " << ((b-a)+24)%24 << " HORA(S)" << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uri/1047.cpp: -------------------------------------------------------------------------------- 1 | //1047 2 | //Game Time with Minutes 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int a, b, c, d; 11 | while(cin >> a >> b >> c >> d) { 12 | int h = ((c-a)+24)%24; 13 | int m = d-b; 14 | if (m<0) { m+=60; h--; } 15 | 16 | cout << "O JOGO DUROU " << h << " HORA(S) E " << m << " MINUTO(S)" << endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /uri/1048.cpp: -------------------------------------------------------------------------------- 1 | //1048 2 | //Salary Increase 3 | //Misc;Beginner 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | double v; 10 | while(cin >> v) { 11 | double p; 12 | if (v<=400) 13 | p = 0.15; 14 | else if (v<=800) 15 | p = 0.12; 16 | else if (v<=1200) 17 | p = 0.10; 18 | else if (v<=1600) 19 | p = 0.07; 20 | else 21 | p = 0.04; 22 | 23 | cout << fixed << setprecision(2); 24 | 25 | cout << "Novo salario: " << v+(v*p) << endl; 26 | cout << "Reajuste ganho: " << (v*p) << endl; 27 | cout << setprecision(0) << "Em percentual: " << p*100 << " %" << endl; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /uri/1049.cpp: -------------------------------------------------------------------------------- 1 | //1049 2 | //Animal 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | string s1, s2, s3; 11 | while(cin >> s1 >> s2 >> s3) { 12 | if (s1 == "vertebrado") { 13 | if (s2 == "ave") { 14 | if (s3 == "carnivoro") { 15 | cout << "aguia" << endl; 16 | } else { 17 | cout << "pomba" << endl; 18 | } 19 | } else { 20 | if (s3 == "onivoro") { 21 | cout << "homem" << endl; 22 | } else { 23 | cout << "vaca" << endl; 24 | } 25 | } 26 | } else { 27 | if (s2 == "inseto") { 28 | if (s3 == "hematofago") { 29 | cout << "pulga" << endl; 30 | } else { 31 | cout << "lagarta" << endl; 32 | } 33 | } else { 34 | if (s3 == "hematofago") { 35 | cout << "sanguessuga" << endl; 36 | } else { 37 | cout << "minhoca" << endl; 38 | } 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /uri/1050.cpp: -------------------------------------------------------------------------------- 1 | //1050 2 | //DDD 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int a; 11 | while(cin >> a) { 12 | if (a==61) 13 | cout << "Brasilia" << endl; 14 | else if (a==71) 15 | cout << "Salvador" << endl; 16 | else if (a==11) 17 | cout << "Sao Paulo" << endl; 18 | else if (a==21) 19 | cout << "Rio de Janeiro" << endl; 20 | else if (a==32) 21 | cout << "Juiz de Fora" << endl; 22 | else if (a==19) 23 | cout << "Campinas" << endl; 24 | else if (a==27) 25 | cout << "Vitoria" << endl; 26 | else if (a==31) 27 | cout << "Belo Horizonte" << endl; 28 | else 29 | cout << "DDD not found" << endl; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /uri/1051.cpp: -------------------------------------------------------------------------------- 1 | //1051 2 | //Taxes 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | double v; 11 | while(cin >> v) { 12 | double x = 0; 13 | if (v>0) { 14 | x += min(v, 2000.0) * 0; 15 | v -= 2000; 16 | } 17 | 18 | if (v>0) { 19 | x += min(v, 1000.0) * 0.08; 20 | v -= 1000; 21 | } 22 | 23 | if (v>0) { 24 | x += min(v, 1500.0) * 0.18; 25 | v -= 1500; 26 | } 27 | 28 | if (v>0) 29 | x += v*0.28; 30 | 31 | cout << fixed << setprecision(2) << "R$ " << x << endl; 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /uri/1052.cpp: -------------------------------------------------------------------------------- 1 | //1052 2 | //Month 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int v; 11 | while(cin >> v) { 12 | if (v==1) 13 | cout << "January" << endl; 14 | else if (v==2) 15 | cout << "February" << endl; 16 | else if (v==3) 17 | cout << "March" << endl; 18 | else if (v==4) 19 | cout << "April" << endl; 20 | else if (v==5) 21 | cout << "May" << endl; 22 | else if (v==6) 23 | cout << "June" << endl; 24 | else if (v==7) 25 | cout << "July" << endl; 26 | else if (v==8) 27 | cout << "August" << endl; 28 | else if (v==9) 29 | cout << "September" << endl; 30 | else if (v==10) 31 | cout << "October" << endl; 32 | else if (v==11) 33 | cout << "November" << endl; 34 | else if (v==12) 35 | cout << "December" << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /uri/1059.cpp: -------------------------------------------------------------------------------- 1 | //1059 2 | //Even Numbers 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | for(int i=2; i<=100; i+=2) 11 | cout << i << endl; 12 | } 13 | -------------------------------------------------------------------------------- /uri/1060.cpp: -------------------------------------------------------------------------------- 1 | //1060 2 | //Positive Numbers 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int sum = 0; 11 | for(int i=0;i<6; i++) { 12 | double temp; cin >> temp; 13 | if (temp > 0) sum++; 14 | } 15 | cout << sum << " valores positivos" << endl; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /uri/1424.cpp: -------------------------------------------------------------------------------- 1 | //1424 2 | //Problema Fácil de Rujia Liu? 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector T[1000010]; 10 | 11 | int main() { 12 | int n, m; 13 | while(cin >> n >> m) { 14 | memset(T, 0, sizeof T); 15 | for(int i=1; i<=n; i++) { 16 | int temp; cin >> temp; 17 | T[temp].push_back(i); 18 | } 19 | 20 | while(m--) { 21 | int k, v; 22 | cin >> k >> v; 23 | k--; 24 | if (k 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int n; 10 | while(cin >> n, n) { 11 | for(int i=0; i0) cout << " "; 15 | 16 | cout << right << setw(3) << number; 17 | } 18 | cout << endl; 19 | } 20 | cout << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /uri/1520.cpp: -------------------------------------------------------------------------------- 1 | //1520 2 | //Screws and Nuts 3 | //Misc;Binary Search 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int T[100000]; 9 | 10 | int main() { 11 | int n, k; 12 | while(cin >> n) { 13 | int tt = 0; 14 | for(int i=0; i> a >> b; 16 | for(int j=a; j<=b; j++) { 17 | T[tt++] = j; 18 | } 19 | } 20 | 21 | sort(T, T+tt); 22 | 23 | cin >> k; 24 | int begin = lower_bound(T, T+tt, k)-T; 25 | int end = upper_bound(T, T+tt, k)-T; 26 | if (begin < end) { 27 | cout << k << " found from " << begin << " to " << end-1 << endl; 28 | } else { 29 | cout << k << " not found" << endl; 30 | } 31 | 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /uri/1521.cpp: -------------------------------------------------------------------------------- 1 | //1521 2 | //The Guilty 3 | //Misc;Beginner 4 | #include 5 | using namespace std; 6 | 7 | int T[100]; 8 | 9 | int main() { 10 | int n, k; 11 | while(cin >> n, n) { 12 | for(int i=0; i> T[i]; 14 | cin >> k; 15 | while(T[k-1] != k) { 16 | k = T[k-1]; 17 | } 18 | cout << k << endl; 19 | 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uri/1523.cpp: -------------------------------------------------------------------------------- 1 | //1523 2 | //Linear Parking Lot 3 | //Misc;Beginner 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | struct Car { 10 | int a, b; 11 | Car(int a, int b) : a(a), b(b) {} 12 | }; 13 | 14 | stack S; 15 | vector V; 16 | 17 | int main() { 18 | int n, k; 19 | while(cin >> n >> k, n||k) { 20 | bool A = true; 21 | V.clear(); 22 | 23 | for(int i=0; i> a >> b; 25 | 26 | int time = 0; 27 | while(S.size() > 0 && a>=S.top().b) { 28 | A &= S.top().b >= time; 29 | time = S.top().b; 30 | S.pop(); 31 | } 32 | 33 | A &= S.size() < k; 34 | S.push(Car(a, b)); 35 | } 36 | 37 | int time = 0; 38 | while(S.size() > 0) { 39 | A &= S.top().b >= time; 40 | time = S.top().b; 41 | S.pop(); 42 | } 43 | 44 | cout << (A?"Sim":"Nao") << endl; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /uri/1524.cpp: -------------------------------------------------------------------------------- 1 | //1524 2 | //Cafeteria Queue 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int T[1100][1100]; 9 | int L[1100][1100]; 10 | int V[1100][1100]; 11 | 12 | int main() { 13 | int n, k, run = 0; 14 | while(scanf("%d %d", &n, &k) != EOF) { 15 | run++; 16 | 17 | T[1][1] = 0; 18 | L[1][1] = 0; 19 | V[1][1] = run; 20 | 21 | for(int i=2; i<=n; i++) { 22 | int temp; scanf("%d", &temp); 23 | 24 | for(int j=1; j<=i; j++) { 25 | T[i][j] = 0x3f3f3f3f; 26 | 27 | if (V[i-1][j]) { 28 | T[i][j] = min(T[i][j], T[i-1][j] + (temp-L[i-1][j])); 29 | V[i][j] = run; 30 | } 31 | 32 | if (j > 1 && V[i-1][j-1]) { 33 | T[i][j] = min(T[i][j], T[i-1][j-1]); 34 | V[i][j] = run; 35 | } 36 | 37 | L[i][j] = temp; 38 | } 39 | } 40 | 41 | cout << T[n][k] << endl; 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /uri/1527.cpp: -------------------------------------------------------------------------------- 1 | //1527 2 | //Guilds 3 | //Misc;Union-Find 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int P[100100]; 10 | int L[100100]; 11 | int V[100100]; 12 | int rg; 13 | 14 | int findset(int v) { 15 | if (P[v] == v) return v; 16 | return P[v] = findset(P[v]); 17 | } 18 | 19 | bool unionset(int x, int y) { 20 | int a = findset(x), b = findset(y); 21 | if (a==b) return false; 22 | 23 | P[b] = a; 24 | L[a] += L[b]; 25 | V[a] += V[b]; 26 | if (rg==b) rg = a; 27 | } 28 | 29 | int main() { 30 | int n, k; 31 | while(scanf("%d %d", &n, &k), n|k) { 32 | int rw = 0; 33 | rg = 1; 34 | for(int i=1; i<=n; i++) { 35 | scanf("%d", L+i); 36 | P[i] = i; 37 | V[i] = 0; 38 | } 39 | 40 | for(int i=0; i L[fb]) { 49 | V[fa]++; 50 | if (rg == fa) rw++; 51 | } 52 | if (L[fa] < L[fb]) { 53 | V[fb]++; 54 | if (rg == fb) rw++; 55 | } 56 | } 57 | } 58 | printf("%d\n", rw); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /uri/1556.cpp: -------------------------------------------------------------------------------- 1 | //1556 2 | //Removing Letters 3 | //Misc;Backtracking 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | set A; 10 | string S; 11 | int N[2000]; 12 | char T[2000]; 13 | 14 | void generate(int t, int k) { 15 | if (k==S.size()) { 16 | if (t!=0) 17 | A.insert(string(T, t)); 18 | } else { 19 | generate(t, N[k]); 20 | for(int i=k; i> S) { 29 | int nextK = S.size(); 30 | for(int i=S.size()-1; i>=0; i--) { 31 | N[i] = nextK; 32 | if (i>0 && S[i]!=S[i-1]) 33 | nextK = i; 34 | } 35 | 36 | A.clear(); 37 | generate(0, 0); 38 | for(set::iterator it=A.begin(); it!=A.end(); it++) 39 | cout << *it << endl; 40 | cout < 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int T[MAX][MAX], S[MAX], n; 11 | bool V[MAX][MAX]; 12 | 13 | int TT(int a, int b) { 14 | if (a+1==b) return 0; 15 | if (V[a][b]) return T[a][b]; 16 | 17 | int minn = INT_MAX; 18 | for(int i=a+1; i> t, t) { 28 | cin >> n; 29 | 30 | memset(S, 0, sizeof(S)); 31 | memset(V, 0, sizeof(V)); 32 | S[0] = 0; 33 | for(int i=1; i<=n; i++) { 34 | cin >> S[i]; 35 | } 36 | S[n+1] = t; 37 | 38 | cout << "The minimum cutting is " << TT(0, n+1) << "." << endl; 39 | 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /uva/10015.cpp: -------------------------------------------------------------------------------- 1 | //10015 2 | //Joseph's Cousin 3 | //Dynamic Programming;Josephus Problem 4 | #include 5 | #include 6 | #define MAX 35000 7 | #define MAX2 3503 8 | using namespace std; 9 | 10 | bool P[MAX]; 11 | vector W; 12 | int T[MAX2][MAX2]; 13 | 14 | int main() { 15 | P[0] = P[1] = true; 16 | for(int i=2; i> n, n) { 30 | cout << T[n][0]+1 << endl; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /uva/10018.cpp: -------------------------------------------------------------------------------- 1 | //10018 2 | //Reverse and Add 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | long reverse(long a) { 8 | long b = 0; 9 | while(a) { 10 | b = b*10 + a%10; 11 | a /= 10; 12 | } 13 | return b; 14 | } 15 | 16 | int main() { 17 | int n; 18 | cin >> n; 19 | while(n--) { 20 | long a; cin >> a; 21 | for(int i=1; i<=1000; i++ ) { 22 | a = a + reverse(a); 23 | if (a == reverse(a)) { 24 | cout << i << " " << a << endl; 25 | break; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /uva/10035.cpp: -------------------------------------------------------------------------------- 1 | //10035 2 | //Primary Arithmetic 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b; 9 | while(cin >> a >> b, a|b) { 10 | int carries = 0; 11 | int c = 0; 12 | while(a|b) { 13 | int s = a%10 + b%10 + c; 14 | c = s/10; 15 | if (c) carries++; 16 | 17 | a/=10; 18 | b/=10; 19 | } 20 | 21 | if (carries == 0) { 22 | cout << "No carry operation." << endl; 23 | } else if (carries==1) { 24 | cout << "1 carry operation." << endl; 25 | } else { 26 | cout << carries << " carry operations." << endl; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /uva/10066.cpp: -------------------------------------------------------------------------------- 1 | //10066 2 | //The Twin Towers 3 | //Dynamic Programming;Longest Common Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 105 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | int P[MAX], Q[MAX]; 13 | 14 | int main() { 15 | int p, q, tt=0; 16 | while(cin >> p >> q, tt++, p&&q) { 17 | memset(T, 0, sizeof(T)); 18 | 19 | for(int i=0; i> P[i]; 20 | for(int i=0; i> Q[i]; 21 | 22 | for(int i=0; i<=p; i++) T[i][0] = 0; 23 | for(int i=0; i<=q; i++) T[0][i] = 0; 24 | 25 | for(int i=1; i<=p; i++) { 26 | for(int j=1; j<=q; j++) { 27 | if (P[i-1] == Q[j-1]) 28 | T[i][j] = T[i-1][j-1] + 1; 29 | else 30 | T[i][j] = max(T[i-1][j], T[i][j-1]); 31 | } 32 | } 33 | cout << "Twin Towers #" << tt << endl; 34 | cout << "Number of Tiles : " << T[p][q] << endl; 35 | cout << endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /uva/10090.cpp: -------------------------------------------------------------------------------- 1 | //10090 2 | //Marbles 3 | //Math;Extended Euclid 4 | #include 5 | #define ull long long 6 | using namespace std; 7 | 8 | ull euclid(ull a, ull b, ull& rx, ull& ry) { 9 | if (!b) return rx=1, ry=0, a; 10 | 11 | ull q = a/b; 12 | ull x, y; 13 | ull g = euclid(b, a-q*b, x, y); 14 | return rx=y, ry=x-q*y, g; 15 | } 16 | 17 | ull solve(ull a, ull b, ull c) { 18 | ull x, y; 19 | ull g = euclid(a, b, x, y); 20 | if (c%g) return -1; 21 | ull ag=a/g, bg=b/g, cg=c/g; 22 | return (x*cg%bg+bg)%bg; 23 | } 24 | 25 | int main() { 26 | ull n, c1, n1, c2, n2; 27 | 28 | while(cin >> n, n) { 29 | cin >> c1 >> n1 >> c2 >> n2; 30 | 31 | ull sol1=solve(n1, n2, n), sol2=solve(n2, n1, n); 32 | 33 | ull sol12=(n-n1*sol1)/n2, sol21=(n-n2*sol2)/n1; 34 | 35 | if (sol1 < 0 || sol12 < 0) { 36 | cout << "failed" << endl; 37 | continue; 38 | } 39 | 40 | ull cos1=c1*sol1+sol12*c2; 41 | ull cos2=c2*sol2+sol21*c1; 42 | if (cos1 < cos2) 43 | cout << sol1 << " " << sol12 << endl; 44 | else 45 | cout << sol21 << " " << sol2 << endl; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /uva/10104.cpp: -------------------------------------------------------------------------------- 1 | //10104 2 | //Euclid Problem 3 | //Math;Extended Euclid 4 | #include 5 | #define ull long long 6 | using namespace std; 7 | 8 | int euclid(int a, int b, int& rx, int& ry) { 9 | if (!b) return rx=1, ry=0, a; 10 | 11 | int q = a/b; 12 | int x, y; 13 | int g = euclid(b, a-q*b, x, y); 14 | return rx=y, ry=x-q*y, g; 15 | } 16 | 17 | int main() { 18 | int a, b; 19 | 20 | while(cin >> a >> b) { 21 | int x, y; 22 | int d = euclid(a,b,x,y); 23 | 24 | cout << x << " " << y << " " << d << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /uva/10154.cpp: -------------------------------------------------------------------------------- 1 | //10154 2 | //Weights and Measures 3 | //Dynamic Programming;Longest Increasing Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #define MAX 10005 12 | using namespace std; 13 | 14 | struct Turtle { 15 | int w,c; 16 | Turtle() {} 17 | Turtle(int w, int c) : w(w), c(c) {} 18 | }; 19 | 20 | bool compare(const Turtle& a, const Turtle& b) { 21 | return a.c > b.c; 22 | } 23 | 24 | vector V; 25 | int T[MAX]; 26 | int main() { 27 | int w, c, k=0; 28 | T[0] = INT_MAX; 29 | 30 | while(cin >> w >> c) { 31 | V.push_back(Turtle(w, c-w)); 32 | } 33 | sort(V.begin(), V.end(), compare); 34 | 35 | for(int i=0; i=0; j--) { 39 | int next = min(T[j]-w, c); 40 | if (next >= T[j+1]) { 41 | T[j+1] = next; 42 | k=max(k, j+1); 43 | } 44 | } 45 | } 46 | cout << k << endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /uva/10158.cpp: -------------------------------------------------------------------------------- 1 | //10158 2 | //War 3 | //Misc;Union-Find 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int P[20000]; 12 | 13 | inline int enemy(int v) { return v+10000; } 14 | 15 | inline int findset(int v) { 16 | if (P[v] != -1 && P[v] != v) 17 | return P[v] = findset(P[v]); 18 | return v; 19 | } 20 | 21 | inline int unionset(int x, int y) { 22 | int a = findset(x), b = findset(y); 23 | if (a> n; 31 | while(cin >> c >> x >> y, c|x|y) { 32 | if (c==1) { 33 | if (findset(x) == findset(enemy(y))) { cout << -1 << endl; continue; } 34 | unionset(x, y); 35 | unionset(enemy(x), enemy(y)); 36 | } else if (c==2) { 37 | if (findset(x) == findset(y)) { cout << -1 << endl; continue; } 38 | unionset(x, enemy(y)); 39 | unionset(enemy(x), y); 40 | } else if (c==3) { 41 | cout << (findset(x) == findset(y)) << endl; 42 | } else if (c==4) { 43 | cout << (findset(x) == findset(enemy(y))) << endl; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /uva/1016.cpp: -------------------------------------------------------------------------------- 1 | //1016 2 | //Silly Sort 3 | //Misc;Permutation Cycle 4 | #include 5 | #include 6 | #include 7 | #define MAX 2000 8 | using namespace std; 9 | 10 | int T[MAX], Q[MAX], M[MAX]; 11 | bool V[MAX]; 12 | 13 | int main() { 14 | int n, t=0; 15 | while(cin >> n, n) { 16 | memset(V, 0, sizeof(V)); 17 | 18 | int minn=1<<30; 19 | for(int i=0; i> T[i]; 21 | minn = min(minn, T[i]); 22 | } 23 | 24 | memcpy(Q, T, sizeof(T)); 25 | sort(Q, Q+n); 26 | for(int i=0; i 5 | #include 6 | using namespace std; 7 | 8 | char T[200][200]; 9 | int N[200][200]; 10 | int n, m, t=0; 11 | 12 | void add(int i, int j) { 13 | if (i<0 || i>=n || j<0 || j>=m) return; 14 | N[i][j]++; 15 | } 16 | 17 | int main() { 18 | while(cin >> n >> m, n|m) { 19 | memset(N, 0, sizeof(N)); 20 | for(int i=0;i> T[i][j]; 23 | if (T[i][j] == '*') { 24 | add(i-1, j-1); 25 | add(i-1, j); 26 | add(i-1, j+1); 27 | add(i, j-1); 28 | add(i, j+1); 29 | add(i+1, j-1); 30 | add(i+1, j); 31 | add(i+1, j+1); 32 | } 33 | } 34 | } 35 | 36 | if (t++>0) cout << endl; 37 | cout << "Field #" << t << ":" << endl; 38 | for(int i=0;i 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | string P, Q; 13 | 14 | int main() { 15 | int p, q, tt=0; 16 | while(getline(cin, P), P!="#") { 17 | tt++; 18 | getline(cin, Q); 19 | int p = P.size(), q = Q.size(); 20 | 21 | memset(T, 0, sizeof(T)); 22 | 23 | for(int i=0; i<=p; i++) T[i][0] = 0; 24 | for(int i=0; i<=q; i++) T[0][i] = 0; 25 | 26 | for(int i=1; i<=p; i++) { 27 | for(int j=1; j<=q; j++) { 28 | if (P[i-1] == Q[j-1]) 29 | T[i][j] = T[i-1][j-1] + 1; 30 | else 31 | T[i][j] = max(T[i-1][j], T[i][j-1]); 32 | } 33 | } 34 | cout << "Case #" << tt << ": you can visit at most " << T[p][q] << " cities." << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /uva/10243.cpp: -------------------------------------------------------------------------------- 1 | //10243 2 | //Fire! Fire! Fire! 3 | //Graphs;DFS 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 1005 10 | using namespace std; 11 | 12 | int n; 13 | bool G[MAX][MAX]; 14 | bool L[MAX]; 15 | bool V[MAX]; 16 | 17 | void dfs(int v, bool start) { 18 | //cout << "*" << v << endl; 19 | if (V[v]) return; 20 | V[v] = true; 21 | bool all = true; 22 | int children = 0; 23 | for(int i=0;i 0 || start && children==0) 31 | L[v] = true; 32 | } 33 | 34 | int main() { 35 | int a, b, m; 36 | 37 | while(cin >> n, n) { 38 | memset(G, 0, sizeof(G)); 39 | memset(L, 0, sizeof(L)); 40 | memset(V, 0, sizeof(V)); 41 | 42 | for(int i=0;i> m; 44 | for(int j=0; j> a; 46 | a--; 47 | G[i][a] = G[a][i] = true; 48 | } 49 | } 50 | 51 | int count = 0; 52 | for(int i=0;i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 101 10 | using namespace std; 11 | 12 | int T[MAX][MAX], M[MAX][MAX], n, k; 13 | 14 | int walk(int x, int y, int curr) { 15 | if (x < 0 || x >= n || y < 0 || y >= n) return 0; 16 | if (T[x][y] <= curr) return 0; 17 | if (M[x][y] >= 0) return M[x][y]; 18 | 19 | int maxx = 0; 20 | for(int i=1; i<=k; i++) { 21 | maxx = max(maxx, walk(x-i, y, T[x][y])+T[x][y]); 22 | maxx = max(maxx, walk(x+i, y, T[x][y])+T[x][y]); 23 | maxx = max(maxx, walk(x, y-i, T[x][y])+T[x][y]); 24 | maxx = max(maxx, walk(x, y+i, T[x][y])+T[x][y]); 25 | } 26 | return M[x][y] = maxx; 27 | } 28 | 29 | int main() { 30 | int t; 31 | cin >> t; 32 | while(t--) { 33 | cin >> n >> k; 34 | memset(M, -1, sizeof(M)); 35 | for(int i=0; i> T[i][j]; 38 | 39 | cout << walk(0,0, -1) << endl; 40 | if (t) cout << endl; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /uva/10278.cpp: -------------------------------------------------------------------------------- 1 | //10278 2 | //Fire Station 3 | //Graphs;Shortest Path;Floyd-Warshall 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #define MAX 502 12 | using namespace std; 13 | 14 | int G[MAX][MAX], f, n; 15 | bool F[MAX]; 16 | 17 | int main() { 18 | int t; cin >> t; 19 | string s; 20 | while(t--) { 21 | cin >> f >> n; 22 | memset(G, 0x3F, sizeof(G)); 23 | memset(F, 0, sizeof(F)); 24 | 25 | for(int i=0;i> a; F[a] = true; 27 | } 28 | getline(cin, s); 29 | while(getline(cin, s), cin && s!="") { 30 | int a, b, c; 31 | stringstream inter(s); 32 | inter >> a >> b >> c; 33 | G[a][b] = G[b][a] = c; 34 | } 35 | 36 | 37 | for(int k=1; k<=n; k++) { 38 | G[k][k] = 0; 39 | for(int i=1; i<=n; i++) 40 | for(int j=1; j<=n; j++) 41 | G[i][j] = min(G[i][j], G[i][k] + G[k][j]); 42 | } 43 | 44 | int minn = INT_MAX, minv; 45 | for(int i=1; i<=n; i++) { 46 | int maxx = 0; 47 | for(int j=1; j<=n; j++) { 48 | int nearest = INT_MAX; 49 | for(int k=1; k<=n; k++) { 50 | if (!F[k] && k!=i) continue; 51 | nearest = min(nearest, G[k][j]); 52 | } 53 | maxx = max(maxx, nearest); 54 | } 55 | if (maxx < minn) { 56 | minn = maxx; 57 | minv = i; 58 | } 59 | } 60 | cout << minv << endl; 61 | if (t) cout << endl; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /uva/10298.cpp: -------------------------------------------------------------------------------- 1 | //10298 2 | //Power Strings 3 | //Misc;String Matching;KMP 4 | #include 5 | #include 6 | #include 7 | #define MAX 1000010 8 | using namespace std; 9 | 10 | int F[MAX]; 11 | 12 | void kmp_init(string& P) { 13 | F[0] = 0; F[1] = 0; 14 | int i = 1, j = 0; 15 | while(i> P, P!=".") { 46 | T = P+P; 47 | cout << P.size() / kmp(P, T, 1) << endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uva/10300.cpp: -------------------------------------------------------------------------------- 1 | //10300 2 | //Ecological Premium 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n, f; 9 | cin >> n; 10 | while(n--) { 11 | cin >> f; 12 | double total = 0; 13 | for(int i=0; i> a >> b >> c; 16 | total += a*c; 17 | } 18 | cout << (int)total << endl; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /uva/10304.cpp: -------------------------------------------------------------------------------- 1 | //10304 2 | //Optimal Binary Search Tree 3 | //Dynamic Programming;Optimal Search Tree 4 | #define MAX 252 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int T[MAX][MAX], S[MAX], n; 11 | bool V[MAX][MAX]; 12 | 13 | int TT(int a, int b) { 14 | if (b < a) return 0; 15 | if (V[a][b]) return T[a][b]; 16 | 17 | int minn = INT_MAX; 18 | for(int i=a; i<=b; i++) 19 | minn = min(minn, TT(a,i-1) + TT(i+1,b) + (S[b]-S[a-1])-(S[i]-S[i-1])); 20 | 21 | V[a][b] = true; 22 | return T[a][b] = minn; 23 | } 24 | 25 | int main() { 26 | while(cin >> n) { 27 | memset(V, 0, sizeof(V)); 28 | S[0] = 0; 29 | for(int i=1; i<=n; i++) { 30 | cin >> S[i]; 31 | S[i] += S[i-1]; 32 | } 33 | 34 | cout << TT(1, n) << endl; 35 | 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /uva/10316.cpp: -------------------------------------------------------------------------------- 1 | //10316 2 | //Airline Hub 3 | //Math;Geometry;Great-Circle Distance 4 | #define PI 3.14159265 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | struct Point { 12 | double x, y; 13 | double dx, dy; 14 | 15 | Point() {} 16 | Point(double x, double y) : x(x), y(y) { 17 | dx = x/180.0*PI; 18 | dy = y/180.0*PI; 19 | } 20 | 21 | double distance(Point& p) { 22 | return acos(cos(p.dx) * cos(this->dx) * cos(this->dy - p.dy) + sin(p.dx) * sin(this->dx)); 23 | } 24 | }; 25 | 26 | Point P[1050]; 27 | 28 | int main() { 29 | int n; 30 | while(cin >> n) { 31 | for(int i=0; i> x >> y; 33 | P[i] = Point(x,y); 34 | } 35 | 36 | double minn = 1000000000; 37 | int mini = 0; 38 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 105 10 | using namespace std; 11 | 12 | map women; 13 | int main() { 14 | int n; 15 | string s; 16 | cin >> n; 17 | while(n--) { 18 | cin >> s; 19 | women[s]++; 20 | getline(cin, s); 21 | } 22 | 23 | for(map::const_iterator it = women.begin(); it != women.end(); it++) { 24 | cout << it->first << " " << it->second << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /uva/10444.cpp: -------------------------------------------------------------------------------- 1 | //10444 2 | //Multi-peg Towers of Hanoi 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 205 10 | using namespace std; 11 | 12 | int T[MAX][MAX]; 13 | int main() { 14 | int n=201, p=21, t=0; 15 | 16 | for(int i=0;i<=n;i++) { 17 | if (i<31) 18 | T[i][3] = (1<= 0) //avoid overflow 32 | minn = min(minn, value); 33 | } 34 | T[i][j] = minn; 35 | } 36 | 37 | } 38 | } 39 | 40 | while(cin >> n >> p, n | p) { 41 | cout << "Case " << ++t << ": " << T[n][p] << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /uva/10480.cpp: -------------------------------------------------------------------------------- 1 | //10480 2 | //Sabotage 3 | //Graphs;Maximum Flow;Ford-Fulkerson 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 1006 11 | using namespace std; 12 | 13 | int G[MAX][MAX], O[MAX][MAX], n, m; 14 | bool V[MAX]; 15 | 16 | int send(int s, int t, int minn) { 17 | V[s] = true; 18 | 19 | if (s==t) return minn; 20 | for(int i=1; i<=n; i++) { 21 | if (!V[i] && G[s][i] > 0) { 22 | if (int sent = send(i, t, min(minn, G[s][i]))) { 23 | G[s][i] -= sent; 24 | G[i][s] += sent; 25 | return sent; 26 | } 27 | } 28 | } 29 | return 0; 30 | } 31 | 32 | int main() { 33 | int tt=0; 34 | while(cin >> n >> m, n|m) { 35 | if (tt++) cout << endl; 36 | 37 | memset(G, 0, sizeof(G)); 38 | memset(V, 0, sizeof(V)); 39 | memset(O, 0, sizeof(O)); 40 | 41 | for(int i=0;i> a >> b >> f; 44 | G[a][b] = G[b][a] += f; 45 | O[a][b] += f; 46 | } 47 | 48 | int total = 0; 49 | while(int sent = send(1, 2, INT_MAX)) { 50 | total += sent; 51 | memset(V, 0, sizeof(V)); 52 | } 53 | for(int i=1;i<=n;i++) { 54 | for(int j=1;j<=n;j++) { 55 | if (O[i][j] > 0 && V[i] != V[j]) 56 | cout << i << " " << j << endl; 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /uva/10557.cpp: -------------------------------------------------------------------------------- 1 | //10557 2 | //XYZZY 3 | //Graphs;Shortest Path;Bellman Ford 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1001 9 | using namespace std; 10 | 11 | struct Edge { 12 | int a, b; 13 | Edge(int a, int b) : a(a), b(b) {} 14 | }; 15 | 16 | int X[MAX], M[MAX], V[MAX], n; 17 | vector E; 18 | 19 | bool reach(int v, int t) { 20 | if (v==t) return true; 21 | V[v] = true; 22 | for(int i=0; i> n, n!=-1) { 32 | memset(V, 0, sizeof(V)); 33 | E.clear(); 34 | for(int a=1; a<=n; a++) { 35 | int k; cin >> X[a] >> k; 36 | 37 | for(int j=0; j> b; 39 | E.push_back(Edge(a, b)); 40 | } 41 | } 42 | 43 | M[1] = 100; 44 | for(int a=2; a<=n; a++) 45 | M[a] = -1<<29; 46 | 47 | for(int k=0; k M[e.b] && reach(e.a, n); 60 | } 61 | 62 | 63 | if (M[n] > 0 || cycle) { 64 | cout << "winnable" << endl; 65 | } else { 66 | cout << "hopeless" << endl; 67 | } 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /uva/1056.cpp: -------------------------------------------------------------------------------- 1 | //1056 2 | //Degrees of Separation 3 | //Graphs;Shortest Path;Floyd-Warshall 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 51 10 | 11 | using namespace std; 12 | 13 | int G[MAX][MAX]; 14 | int n, m; 15 | map M; 16 | 17 | int person(string& s) { 18 | if (M.find(s) != M.end()) 19 | return M[s]; 20 | else 21 | return M[s]=M.size(); 22 | 23 | } 24 | 25 | int main() { 26 | int t=0; 27 | while(cin >> n >> m, n|m) { 28 | memset(G, 0x1f, sizeof(G)); 29 | M.clear(); 30 | 31 | for(int i=0; i> p >> q; 34 | int a = person(p), b=person(q); 35 | G[a][b] = G[b][a] = 1; 36 | } 37 | for(int i=1; i<=n; i++) G[i][i] = 0; 38 | 39 | assert(M.size() <= n); 40 | 41 | for(int k=1; k<=n; k++) 42 | for(int i=1; i<=n; i++) 43 | for(int j=1; j<=n; j++) 44 | G[i][j] = min(G[i][j], G[i][k] + G[k][j]); 45 | 46 | int maxx = 0; 47 | for(int i=1; i<=n; i++) 48 | for(int j=1; j<=n; j++) 49 | maxx = max(maxx, G[i][j]); 50 | 51 | cout << "Network " << ++t << ": "; 52 | 53 | if (maxx <= n) 54 | cout << maxx << endl; 55 | else 56 | cout << "DISCONNECTED" << endl; 57 | 58 | cout << endl; 59 | 60 | 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /uva/10635.cpp: -------------------------------------------------------------------------------- 1 | //10635 2 | //Prince and Princess 3 | //Dynamic Programming;Longest Increasing Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 255*255 10 | using namespace std; 11 | 12 | int P[MAX], Q[MAX], M[MAX]; 13 | 14 | int main() { 15 | int n, p, q, tt=0, temp; 16 | cin >> n; 17 | while(cin >> n >> p >> q) { 18 | memset(P, 0, sizeof(P)); 19 | q++; p++; 20 | for(int i=1;i<=p;i++) { 21 | cin >> temp; 22 | P[temp] = i; 23 | } 24 | 25 | for (int i=1;i<=q;i++) { 26 | cin >> temp; 27 | Q[i] = P[temp]; 28 | } 29 | 30 | int k=0; M[0]=0; 31 | for(int i=1;i<=q;i++) { 32 | if (Q[i] > M[k]) { 33 | k++; M[k] = Q[i]; 34 | } else { 35 | int j = (int)(lower_bound(M, M+k+1, Q[i])-M); 36 | if (Q[i] > M[j]) j++; 37 | M[j] = Q[i]; 38 | } 39 | } 40 | 41 | cout << "Case " << ++tt << ": " << k << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /uva/10684.cpp: -------------------------------------------------------------------------------- 1 | //10684 2 | //The Jackpot 3 | //Dynamic Programming;Maximum Sum Contiguous Subsequence 4 | #include 5 | #include 6 | #define MAX 1005 7 | using namespace std; 8 | 9 | int main() { 10 | int n, a; 11 | while(cin >> n, n) { 12 | int t=0, s=0; 13 | for(int i=0;i> a; 15 | if (s+a>=0) 16 | t = max(t, s+=a); 17 | else 18 | s = 0; 19 | } 20 | if (s>0) { 21 | cout << "The maximum winning streak is " << t << "." << endl; 22 | } else { 23 | cout << "Losing streak." << endl; 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /uva/10696.cpp: -------------------------------------------------------------------------------- 1 | //10694 2 | //f91 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int f91(int x) { 8 | if (x<=100) 9 | return f91(f91(x+11)); 10 | else 11 | return x-10; 12 | } 13 | 14 | int main() { 15 | int n; 16 | while(cin >> n, n) 17 | cout << "f91(" << n << ") = " << f91(n) << endl; 18 | } 19 | -------------------------------------------------------------------------------- /uva/10723.cpp: -------------------------------------------------------------------------------- 1 | //10723 2 | //Cyborg Genes 3 | //Dynamic Programming;Longest Common Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX], D[MAX][MAX]; 12 | string P, Q; 13 | 14 | int main() { 15 | int p, q, t, tt=0; 16 | cin >> t; 17 | getline(cin, P); 18 | while(tt++ < t) { 19 | getline(cin, P); 20 | getline(cin, Q); 21 | int p = P.size(), q = Q.size(); 22 | 23 | for(int i=0; i<=p; i++) { T[i][0] = 0; D[i][0] = 1; } 24 | for(int i=0; i<=q; i++) { T[0][i] = 0; D[0][i] = 1; } 25 | 26 | for(int i=1; i<=p; i++) { 27 | for(int j=1; j<=q; j++) { 28 | D[i][j] = 0; 29 | if (P[i-1] == Q[j-1]) { 30 | T[i][j] = T[i-1][j-1] + 1; 31 | D[i][j] = D[i-1][j-1]; 32 | } 33 | else { 34 | T[i][j] = max(T[i-1][j], T[i][j-1]); 35 | if (T[i-1][j] == T[i][j]) D[i][j] += D[i-1][j]; 36 | if (T[i][j-1] == T[i][j]) D[i][j] += D[i][j-1]; 37 | } 38 | } 39 | } 40 | 41 | cout << "Case #" << tt << ": " << p+q-T[p][q] << " " << D[p][q] << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /uva/10739.cpp: -------------------------------------------------------------------------------- 1 | //10739 2 | //String to Palindrome 3 | //Dynamic Programming;Edit Distance 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | string P, Q; 13 | 14 | int main() { 15 | int p, q, t, tt=0; 16 | cin >> t; 17 | getline(cin, P); 18 | while(tt++ < t) { 19 | getline(cin, P); 20 | Q = string(P.rbegin(), P.rend()); 21 | int p = P.size(), q = Q.size(); 22 | 23 | for(int i=0; i<=p; i++) { T[i][0] = i; } 24 | for(int i=0; i<=q; i++) { T[0][i] = i; } 25 | 26 | for(int i=1; i<=p; i++) { 27 | for(int j=1; j<=q; j++) { 28 | if (P[i-1] == Q[j-1]) 29 | T[i][j] = T[i-1][j-1]; 30 | else 31 | T[i][j] = min(min(T[i-1][j], T[i][j-1]), T[i-1][j-1])+1; 32 | } 33 | } 34 | 35 | cout << "Case " << tt << ": " << T[p][q]/2 << endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /uva/10783.cpp: -------------------------------------------------------------------------------- 1 | //10783 2 | //Odd Sum 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int t; 11 | cin >> t; 12 | for(int tt=1;tt<=t;tt++) { 13 | int a, b; 14 | cin >> a >> b; 15 | int s = 0; 16 | for(int i=a;i<=b;i++) { 17 | if (i&1) 18 | s+=i; 19 | } 20 | cout << "Case " << tt << ": " << s << endl; 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /uva/10793.cpp: -------------------------------------------------------------------------------- 1 | //10793 2 | //The Orc Attack 3 | //Graphs;Shortest Path;Floyd-Warshall 4 | #include 5 | #include 6 | #define MAX 105 7 | using namespace std; 8 | 9 | int G[MAX][MAX]; 10 | 11 | int main() { 12 | int t; cin >> t; 13 | for(int tt=1; tt<=t; tt++) { 14 | int n, m; cin >> n >> m; 15 | for(int i=1; i<=n; i++) { 16 | for(int j=1; j<=n; j++) 17 | G[i][j] = 1<<29; 18 | G[i][i] = 0; 19 | } 20 | 21 | for(int i=0; i> x >> y >> c; 24 | G[x][y] = G[y][x] = min(G[x][y], c); 25 | } 26 | 27 | for(int k=1; k<=n; k++) 28 | for(int i=1; i<=n; i++) 29 | for(int j=1; j<=n; j++) 30 | G[i][j] = min(G[i][j], G[i][k] + G[k][j]); 31 | 32 | int minn = 1<<29; 33 | for(int i=1; i<=n; i++) 34 | if (*min_element(G[i]+1, G[i]+6) == *max_element(G[i]+1, G[i]+6)) 35 | minn = min(minn, *max_element(G[i]+1, G[i]+n+1)); 36 | 37 | cout << "Map " << tt << ": "; 38 | if (minn < 1<<29) 39 | cout << minn << endl; 40 | else 41 | cout << -1 << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /uva/10827.cpp: -------------------------------------------------------------------------------- 1 | //10827 2 | //Maximum sum on a torus 3 | //Dynamic Programming;Maximum Sum Sub-rectangle 4 | #include 5 | #include 6 | #define MAX 160 7 | using namespace std; 8 | 9 | int T[MAX][MAX]; 10 | 11 | int main() { 12 | int n, a, cases; 13 | cin >> cases; 14 | while(cin >> n) { 15 | for(int i=1; i<=n; i++) { 16 | for(int j=1;j<=n;j++) { 17 | cin >> T[i][j]; 18 | T[i+n][j] = T[i][j]; 19 | } 20 | } 21 | 22 | for(int i=1; i<=2*n; i++) 23 | for(int j=1;j<=n; j++) 24 | T[i][j]+=T[i-1][j]; 25 | 26 | int t = 0; 27 | for(int i=1;i<=2*n; i++) { 28 | for(int j=i;j<=min(i+n-1, 2*n);j++) { 29 | int smax=0, smin=0, ssum=0, tmax=0, tmin=0; 30 | for(int k=1;k<=n; k++) 31 | ssum += T[j][k] - T[i-1][k]; 32 | 33 | for(int k=1;k<=n; k++) { 34 | int a = T[j][k] - T[i-1][k]; 35 | smax += a; 36 | smin += a; 37 | 38 | tmax = max(tmax, smax); 39 | tmin = min(tmin, smin); 40 | 41 | if (smax < 0) smax = 0; 42 | if (smin > 0) smin = 0; 43 | } 44 | t = max(t, max(tmax, ssum-tmin)); 45 | } 46 | } 47 | 48 | cout << t << endl; 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /uva/10891.cpp: -------------------------------------------------------------------------------- 1 | //10891 2 | //Game of Sum 3 | //Dynamic Programming;Matrix Multiplication 4 | #define MAX 101 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int T[MAX][MAX], S[MAX], n; 11 | bool V[MAX][MAX]; 12 | 13 | int TT(int a, int b) { 14 | if (b=a; i--) 22 | maxx = max(maxx, S[b]-S[a-1] - TT(a,i-1)); 23 | 24 | V[a][b] = true; 25 | return T[a][b] = maxx; 26 | } 27 | 28 | int main() { 29 | while(cin >> n, n) { 30 | memset(S, 0, sizeof(S)); 31 | memset(V, 0, sizeof(V)); 32 | S[0] = 0; 33 | for(int i=1; i<=n; i++) { 34 | cin >> S[i]; 35 | S[i] += S[i-1]; 36 | } 37 | 38 | cout << 2*TT(1, n)-S[n]-S[0] << endl; 39 | 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /uva/10930.cpp: -------------------------------------------------------------------------------- 1 | //10930 2 | //A-Sequence 3 | //Dynamic Programming;Knapsack;Binary Knapsack 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int K[30001]; 10 | 11 | int main() { 12 | int n, t=0, w; 13 | while(t++, cin >> n) { 14 | cout << "Case #" << t << ":"; 15 | memset(K, 0, sizeof(K)); 16 | 17 | bool ok=true; 18 | 19 | K[0] = 1; int last = 0; 20 | for(int i=1; i<=n; i++) { 21 | cin >> w; cout << " " << w; 22 | ok &= !K[w] && w > last; 23 | for(int j=10000; j>=w; j--) 24 | if (K[j-w]) 25 | K[j] = 1; 26 | last = w; 27 | } 28 | cout << endl; 29 | cout << "This is" << (ok?"":" not") << " an A-sequence." << endl; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /uva/10986.cpp: -------------------------------------------------------------------------------- 1 | //10986 2 | //Sending email 3 | //Graphs;Shortest Path;Dijkstra 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 200010 11 | 12 | using namespace std; 13 | 14 | struct Edge { 15 | int v, c; 16 | Edge(int v, int c) : v(v), c(c) {} 17 | inline bool operator < (const Edge& that) const { return c > that.c; } 18 | }; 19 | 20 | vector G[MAX]; 21 | priority_queue Q; 22 | int n, m, s, t; 23 | int V[MAX]; 24 | 25 | 26 | int main() { 27 | int tt; cin >> tt; tt=0; 28 | while(cin >> n >> m >> s >> t) { 29 | int before = 0; 30 | memset(V, 0x3f, sizeof(V)); 31 | memset(G, 0, sizeof(G)); 32 | Q = priority_queue(); 33 | 34 | for(int i=0; i> a >> b >> c; 37 | G[a].push_back(Edge(b, c)); 38 | G[b].push_back(Edge(a, c)); 39 | before += c; 40 | } 41 | 42 | int totalc=0; 43 | 44 | Q.push(Edge(s, 0)); 45 | 46 | while(totalc < n && !Q.empty()) { 47 | Edge item = Q.top(); Q.pop(); 48 | if (item.c >= V[item.v]) continue; 49 | 50 | V[item.v] = item.c; 51 | totalc++; 52 | 53 | for(int j=0; j 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 10005 10 | using namespace std; 11 | 12 | int T[MAX]; 13 | int main() { 14 | int n, w, c; 15 | while(cin >> n, n) { 16 | memset(T, 0, sizeof(T)); 17 | 18 | int k = 0; 19 | T[0] = INT_MAX; 20 | for(int i=1; i<=n; i++) { 21 | cin >> w >> c; 22 | for(int j=k; j>=0; j--) { 23 | int next = min(T[j]-w, c); 24 | if (next >= T[j+1]) { 25 | T[j+1] = next; 26 | k=max(k, j+1); 27 | } 28 | } 29 | } 30 | 31 | cout << k << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /uva/11059.cpp: -------------------------------------------------------------------------------- 1 | //11059 2 | //Maximum Product 3 | //Dynamic Programming;Maximum Sum Contiguous Subsequence 4 | #include 5 | #include 6 | #include 7 | #define MAX 1005 8 | using namespace std; 9 | 10 | int main() { 11 | long long n, a, t=0; 12 | while(cin >> n) { 13 | long long maxx=0, newneg=0, newpos=0, spos=1, sneg=1; 14 | bool valid = false; 15 | for(int i=0;i> a; 17 | if (spos*a>0) { 18 | valid = true; 19 | spos*=a; 20 | } else { 21 | newneg = spos*a; 22 | spos = 1; 23 | } 24 | 25 | 26 | if (sneg*a<0) { 27 | sneg*=a; 28 | } else { 29 | if (sneg*a>0) valid = true; 30 | newpos = sneg*a; 31 | sneg = 1; 32 | } 33 | 34 | maxx = max(maxx, spos = max(spos, newpos)); 35 | sneg = min(sneg, newneg); 36 | newpos = newneg = 0; 37 | } 38 | if (!valid) maxx = 0; 39 | cout << "Case #" << ++t << ": The maximum product is " << maxx << "." << endl; 40 | cout << endl; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /uva/11096.cpp: -------------------------------------------------------------------------------- 1 | //11096 2 | //Nails 3 | //Math;Geometry;Convex Hull;Monotone Chain 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define ull long long 9 | using namespace std; 10 | 11 | struct Point { 12 | ull x, y; 13 | 14 | ull product(Point a, Point b) { 15 | return (this->x - a.x)*(b.y - a.y) - (this->y - a.y)*(b.x - a.x); 16 | } 17 | 18 | bool right(Point a, Point b) { 19 | return product(a, b) > 0; 20 | } 21 | 22 | double dist(Point b) { 23 | return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y)); 24 | } 25 | 26 | bool operator <(const Point& p) const { 27 | if (this->x != p.x) return this->x < p.x; 28 | return this->y < p.y; 29 | } 30 | 31 | bool operator ==(const Point& p) const { 32 | return this->x == p.x and this->y == p.y; 33 | } 34 | }; 35 | 36 | int convexHull(Point* P, int n, Point* S) { 37 | sort(P, P+n); 38 | 39 | int m=0; 40 | for(int i=0; i= 2 && !S[m-1].right(S[m-2], P[i])) m--; 42 | S[m++] = P[i]; 43 | } 44 | m--; 45 | 46 | for(int i=n-1, k=m; i >= 0; i--) { 47 | while(m >= k+2 && !S[m-1].right(S[m-2], P[i])) m--; 48 | S[m++] = P[i]; 49 | } 50 | m--; 51 | 52 | return m; 53 | } 54 | 55 | 56 | Point P[120], S[120]; 57 | 58 | int main() { 59 | int tt; cin >> tt; 60 | while(tt--) { 61 | int r, n; 62 | cin >> r >> n; 63 | for(int i=0; i> P[i].x >> P[i].y; 65 | 66 | int s = convexHull(P, n, S); 67 | 68 | double final = 0.0; 69 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #define MAX 102 9 | using namespace std; 10 | 11 | int G[MAX][MAX]; 12 | int n; 13 | 14 | int fill(int x, int y, int v) { 15 | if (G[x][y] != v) return 0; 16 | if (x<=0 || x>n || y<=0 || y>n) return 0; 17 | 18 | G[x][y] = -1; 19 | return 1 + 20 | fill(x-1, y, v) + fill(x+1, y, v) + 21 | fill(x, y-1, v) + fill(x, y+1, v); 22 | } 23 | 24 | int main() { 25 | while(cin >> n, n) { 26 | int a, b; string s; 27 | memset(G, 0, sizeof(G)); 28 | 29 | getline(cin, s); 30 | for(int i=1;i> a >> b) 34 | G[a][b] = i; 35 | } 36 | bool good = true; 37 | for(int i=1;i<=n;i++) { 38 | for(int j=1;j<=n;j++) { 39 | if (G[i][j] >= 0) 40 | good &= fill(i,j, G[i][j]) == n; 41 | } 42 | } 43 | 44 | cout << (good?"good":"wrong") << endl; 45 | 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /uva/11151.cpp: -------------------------------------------------------------------------------- 1 | //11151 2 | //Longest Palindrome 3 | //Dynamic Programming;Longest Common Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | string P, Q; 13 | 14 | int main() { 15 | int p, q, t; 16 | cin >> t; 17 | getline(cin, P); 18 | while(t--) { 19 | getline(cin, P); 20 | Q = string(P.rbegin(), P.rend()); 21 | int p = P.size(), q = Q.size(); 22 | 23 | for(int i=0; i<=p; i++) { T[i][0] = 0; } 24 | for(int i=0; i<=q; i++) { T[0][i] = 0; } 25 | 26 | for(int i=1; i<=p; i++) { 27 | for(int j=1; j<=q; j++) { 28 | if (P[i-1] == Q[j-1]) { 29 | T[i][j] = T[i-1][j-1] + 1; 30 | } 31 | else { 32 | T[i][j] = max(T[i-1][j], T[i][j-1]); 33 | } 34 | } 35 | } 36 | 37 | cout << T[p][q] << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /uva/11157.cpp: -------------------------------------------------------------------------------- 1 | //11157 2 | //Dynamic Frog 3 | //Misc;Sort 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector V; 10 | 11 | int main() { 12 | int t, n, d; 13 | cin >> t; t=0; 14 | 15 | while(cin >> n >> d) { 16 | char a; int b; 17 | V.clear(); 18 | V.push_back(0); 19 | V.push_back(d); 20 | for(int i=0;i> a; cin.ignore(); cin >> b; 22 | V.push_back(b); 23 | if (a=='B') 24 | V.push_back(b); 25 | } 26 | sort(V.begin(), V.end()); 27 | 28 | int maxx = 0; 29 | for(int i=3;i 5 | #include 6 | #include 7 | #include 8 | #define MAX 205 9 | using namespace std; 10 | 11 | int A[MAX], B[MAX], G[MAX][MAX], n; 12 | bool V[MAX]; 13 | 14 | int send(int s, int t, int minn) { 15 | V[s] = true; 16 | 17 | if (s==t) return minn; 18 | for(int i=0; i<=n; i++) { 19 | if (!V[i] && G[s][i] > 0) { 20 | if (int sent = send(i, t, min(minn, G[s][i]))) { 21 | G[s][i] -= sent; 22 | G[i][s] += sent; 23 | return sent; 24 | } 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | int main() { 31 | int t; cin >> t; 32 | 33 | for(int tt=1; tt<=t; tt++) { 34 | memset(G, 0, sizeof(G)); 35 | memset(V, 0, sizeof(V)); 36 | 37 | int a; cin >> a; 38 | for(int i=1; i<=a; i++) 39 | cin >> A[i]; 40 | 41 | int b; cin >> b; 42 | for(int i=1; i<=b; i++) 43 | cin >> B[i]; 44 | 45 | for(int i=1; i<=a; i++) { 46 | G[0][i] = 1; 47 | for(int j=1; j<=b; j++) 48 | if (B[j]==0 || A[i] != 0 && B[j]%A[i] == 0) 49 | G[i][a+j] = 1; 50 | } 51 | 52 | for(int i=a+1; i<=a+b; i++) 53 | G[i][a+b+1] = 1; 54 | 55 | n = a+b+1; 56 | 57 | int total = 0; 58 | while(int sent = send(0, n, INT_MAX)) { 59 | total += sent; 60 | memset(V, 0, sizeof(V)); 61 | } 62 | cout << "Case " << tt << ": " << total << endl; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /uva/11172.cpp: -------------------------------------------------------------------------------- 1 | //579 2 | //ClockHands 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int n; 11 | cin >> n; 12 | while(n--) { 13 | int x, y; 14 | cin >> x >> y; 15 | if (x>y) { 16 | cout << ">" << endl; 17 | } else if (x 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | long long T[51][51]; 10 | 11 | int main() { 12 | for(int i=1;i<=50;i++) { 13 | for(int j=0;i+j<=50;j++) { 14 | int p=i, q=j; 15 | long long n = 0L; 16 | if (p%2!=0 && p/21;p-=2) { 22 | if (q>0) 23 | n = (n<<3) | 5L; 24 | else 25 | n = (n<<2) | 3L; 26 | q--; 27 | } 28 | 29 | if (p==1) n = (n<<1) | 1L; 30 | T[i][j] = n; 31 | } 32 | } 33 | 34 | 35 | int t, p, q; 36 | cin >> t; 37 | while(cin >> p >> q) { 38 | cout << T[p][q] << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /uva/11576.cpp: -------------------------------------------------------------------------------- 1 | //11576 2 | //Scrolling Sign 3 | //Misc;String Matching;KMP;Suffix-Prefix 4 | #include 5 | #include 6 | #include 7 | #define MAX 100010 8 | using namespace std; 9 | 10 | int F[MAX]; 11 | 12 | void kmp_init(string& P) { 13 | F[0] = 0; F[1] = 0; 14 | int i = 1, j = 0; 15 | while(i> t; t=0; 46 | int k, w; 47 | while(cin >> k >> w) { 48 | int sum = 0; 49 | string Q, P = ""; 50 | while(w--) { 51 | cin >> Q; 52 | sum += k-kmp(Q, P); 53 | P = Q; 54 | } 55 | cout << sum << endl; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /uva/1158.cpp: -------------------------------------------------------------------------------- 1 | //1158 2 | //CubesSquared 3 | //Dynamic Programming;Knapsack;Infinite Items Knapsack 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int K[400001]; 10 | vector W; 11 | 12 | int main() { 13 | 14 | for(int i=1; i*i*i<=400000; i++) 15 | W.push_back(i*i*i); 16 | 17 | for(int a=1, i=1; a<=400000; i++, a+=i*i) 18 | W.push_back(a); 19 | 20 | memset(K, 0x3f, sizeof(K)); 21 | K[0] = 0; 22 | for(int i=0; i> n, n!=-1) 28 | cout << K[n] << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /uva/11597.cpp: -------------------------------------------------------------------------------- 1 | //11597 2 | //Spanning Subtree 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n, t=0; 9 | while(cin >> n, t++, n) { 10 | cout << "Case " << t << ": " << n/2 << endl; 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /uva/11629.cpp: -------------------------------------------------------------------------------- 1 | //11629 2 | //Ballot evaluation 3 | //Misc;STL map 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | map P; 10 | 11 | int main() { 12 | int n, g; 13 | while(cin >> n >> g) { 14 | P.clear(); 15 | for(int i=0; i> s >> a; cin.get(); cin >> b; 18 | P[s] = a*10+b; 19 | } 20 | 21 | for(int i=1; i<=g; i++) { 22 | string s = "+"; int d=0; int r; 23 | 24 | while(s=="+") { 25 | cin >> s; 26 | d += P[s]; 27 | cin >> s; 28 | } 29 | cin >> r; r*=10; 30 | 31 | bool result; 32 | if ( s=="<") result = d < r; 33 | if ( s=="<=") result = d <= r; 34 | if ( s==">") result = d > r; 35 | if ( s==">=") result = d >= r; 36 | if ( s=="=") result = d == r; 37 | cout << "Guess #" << i << " was " << (result?"correct":"incorrect") << "." << endl; 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /uva/11631.cpp: -------------------------------------------------------------------------------- 1 | //11631 2 | //Dark roads 3 | //Graphs;Minimum Spanning Tree;Prim;Priority Queue 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 200010 11 | 12 | using namespace std; 13 | 14 | struct Road { 15 | int v, c; 16 | Road(int v, int c) : v(v), c(c) {} 17 | inline bool operator < (const Road& that) const { return c > that.c; } 18 | }; 19 | 20 | vector G[MAX]; 21 | priority_queue Q; 22 | int n, m; 23 | bool V[MAX]; 24 | 25 | 26 | int main() { 27 | while(cin >> n >> m, n|m) { 28 | int before = 0; 29 | memset(V, 0, sizeof(V)); 30 | memset(G, 0, sizeof(G)); 31 | Q = priority_queue(); 32 | 33 | for(int i=0; i> a >> b >> c; 36 | G[a].push_back(Road(b, c)); 37 | G[b].push_back(Road(a, c)); 38 | before += c; 39 | } 40 | 41 | int total = 0, totalc=0; 42 | 43 | Q.push(Road(0, 0)); 44 | 45 | while(totalc < n) { 46 | Road item = Q.top(); Q.pop(); 47 | if (V[item.v]) continue; 48 | 49 | V[item.v] = true; 50 | total += item.c; 51 | totalc++; 52 | 53 | for(int j=0; j 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int K[10001], W[102]; 10 | 11 | int main() { 12 | int n, x, a, b; 13 | 14 | while(cin >> n >> x, n|x) { 15 | memset(K, 0, sizeof(K)); 16 | 17 | for(int i=1; i<=n; i++) { 18 | cin >> a; cin.ignore(); cin >> b; 19 | W[i] = a*100+b; 20 | } 21 | 22 | K[W[x]] = 1; 23 | for(int i=1; i<=n; i++) { 24 | if (i==x) continue; 25 | for(int j=10000; j>=W[i]; j--) 26 | if (K[j-W[i]]) 27 | K[j] = 1; 28 | } 29 | 30 | int maxx = 0; 31 | for(int i=5001; i<=10000; i++) { 32 | if (K[i]) { 33 | maxx = i; 34 | break; 35 | } 36 | } 37 | 38 | cout << fixed << setprecision(2) << (W[x]/((double)maxx)*100.0) << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /uva/11686.cpp: -------------------------------------------------------------------------------- 1 | //11686 2 | //Pick up Sticks 3 | //Graphs;Topological Sorting 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1000001 9 | using namespace std; 10 | 11 | int V[MAX]; 12 | int O[MAX], npv; 13 | vector G[MAX]; 14 | int n, m; 15 | 16 | bool DFS(int d, int v){ 17 | V[v] = 1; 18 | 19 | for(int i=0;i 0; i--) 52 | printf("%d\n", O[i]); 53 | else 54 | printf("IMPOSSIBLE\n"); 55 | } 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /uva/11703.cpp: -------------------------------------------------------------------------------- 1 | //11703 2 | //sqrt log sin 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int K[1000001]; 11 | 12 | int main() { 13 | K[0] = 1; 14 | for(int i=1; i<1000001; i++) { 15 | int a = (int)(i-sqrt(i)); 16 | int b = (int)log(i); 17 | int c = (int)(i*pow(sin(i), 2)); 18 | K[i] = (K[a] + K[b] + K[c])%1000000; 19 | } 20 | 21 | int n; 22 | while(cin >> n, n>-1) 23 | cout << K[n] << endl; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /uva/11709.cpp: -------------------------------------------------------------------------------- 1 | //11709 2 | //Trust Groups 3 | //Graphs;Strongly Connected Components 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1001 9 | using namespace std; 10 | 11 | map P; 12 | int person(const string& p) { 13 | if (P.find(p) != P.end()) 14 | return P[p]; 15 | else 16 | return P[p] = P.size(); 17 | } 18 | 19 | bool V[MAX]; 20 | int O[MAX], npv; 21 | bool G[MAX][MAX]; 22 | int n, m; 23 | 24 | void DFS(int v){ 25 | V[v] = true; 26 | for(int i = 1; i <= n; i++) 27 | if (G[v][i] && !V[i]) 28 | DFS(i); 29 | O[++npv] = v; 30 | } 31 | 32 | void DFSt(int v){ 33 | V[v] = true; 34 | for(int i = 1; i <= n; i++) 35 | if (G[i][v] && !V[i]) 36 | DFSt(i); 37 | } 38 | 39 | 40 | int main() { 41 | int a, b, t; string p, q; 42 | while(cin >> n >> m, n|m) { 43 | memset(G, 0, sizeof(G)); 44 | P.clear(); 45 | getline(cin, p); 46 | 47 | for(int i=0; i 0; i--) 66 | if(!V[O[i]]) { 67 | comp++; 68 | DFSt(O[i]); 69 | } 70 | 71 | cout << comp << endl; 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /uva/11762.cpp: -------------------------------------------------------------------------------- 1 | //11762 2 | //Race to 1 3 | //Math;Probability 4 | #include 5 | #include 6 | #define MAX 1000010 7 | using namespace std; 8 | 9 | int P[MAX], K[MAX], D[MAX][30]; 10 | double R[MAX]; 11 | 12 | int main() { 13 | for(int i=2; i 0) continue; 17 | K[i]++; 18 | 19 | for(int j=i, k=1; j> t; 33 | for(int tt = 1; tt<=t; tt++) { 34 | int a; cin >> a; 35 | cout << "Case " << tt << ": " << fixed << setprecision(10) << R[a] << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /uva/11770.cpp: -------------------------------------------------------------------------------- 1 | //11770 2 | //Lighting Away 3 | //Graphs;Topological Sorting 4 | #include 5 | #include 6 | #include 7 | #define MAX 10001 8 | using namespace std; 9 | 10 | bool V[MAX]; 11 | int O[MAX], npv; 12 | vector G[MAX]; 13 | int n, m; 14 | 15 | void DFS(int v){ 16 | if (V[v]) return; 17 | V[v] = true; 18 | for(int i = 0; i < G[v].size(); i++) 19 | DFS(G[v][i]); 20 | O[++npv] = v; 21 | } 22 | 23 | void DFSt(int v){ 24 | if (V[v]) return; 25 | V[v] = true; 26 | for(int i = 0; i < G[v].size(); i++) 27 | DFSt(G[v][i]); 28 | } 29 | 30 | 31 | int main() { 32 | int a, b; 33 | int t; cin >> t; t=0; 34 | while(cin >> n >> m) { 35 | memset(G, 0, sizeof(G)); 36 | 37 | while(m--) { 38 | cin >> a >> b; 39 | G[a].push_back(b); 40 | } 41 | 42 | npv = 0; 43 | memset(V, 0, sizeof(V)); 44 | memset(O, 0, sizeof(O)); 45 | 46 | for(int i = 1; i <= n; i++) 47 | if(!V[i]) DFS(i); 48 | 49 | memset(V, 0, sizeof(V)); 50 | 51 | int comp = 0; 52 | for(int i = n; i > 0; i--) 53 | if(!V[O[i]]) { 54 | comp++; 55 | DFSt(O[i]); 56 | } 57 | 58 | cout << "Case " << ++t << ": " << comp << endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /uva/11833.cpp: -------------------------------------------------------------------------------- 1 | //11833 2 | //Route Change 3 | //Graphs;Shortest Path;Dijkstra 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 252 11 | 12 | using namespace std; 13 | 14 | struct Edge { 15 | int v, c; 16 | Edge(int v, int c) : v(v), c(c) {} 17 | inline bool operator < (const Edge& that) const { return c > that.c; } 18 | }; 19 | 20 | int G[MAX][MAX]; 21 | int V[MAX], S[MAX]; 22 | int n, m, cc, kk; 23 | 24 | int main() { 25 | while(cin >> n >> m >> cc >> kk, n|m|cc|kk) { 26 | memset(V, 0x3f, sizeof(V)); 27 | memset(S, 0, sizeof(S)); 28 | memset(G, -1, sizeof(G)); 29 | 30 | for(int i=0; i> a >> b >> c; 33 | G[a][b] = G[b][a] = c; 34 | } 35 | 36 | for(int i=cc-2; i>=0; i--) { 37 | S[i] = S[i+1] + G[i][i+1]; 38 | } 39 | 40 | int totalc=0; 41 | 42 | priority_queue Q; 43 | Q.push(Edge(kk, 0)); 44 | 45 | while(totalc < n && !Q.empty()) { 46 | Edge item = Q.top(); Q.pop(); 47 | if (item.c >= V[item.v]) continue; 48 | V[item.v] = item.c; 49 | totalc++; 50 | if (item.v < cc) continue; 51 | for(int j=0; j=0) { 53 | Edge e = Edge(j, G[item.v][j]); 54 | if (item.c + e.c < V[e.v]) 55 | Q.push(Edge(e.v, item.c + e.c)); 56 | } 57 | } 58 | } 59 | 60 | int minn = 0x3f3f3f3f; 61 | for(int i=0;i 5 | #include 6 | #define MAX 1001 7 | using namespace std; 8 | 9 | bool V[MAX]; 10 | int O[MAX], npv; 11 | bool G[MAX][MAX]; 12 | int n, m; 13 | 14 | void DFS(int v){ 15 | V[v] = true; 16 | for(int i = 1; i <= n; i++) 17 | if (G[v][i] && !V[i]) 18 | DFS(i); 19 | O[++npv] = v; 20 | } 21 | 22 | void DFSt(int v){ 23 | V[v] = true; 24 | for(int i = 1; i <= n; i++) 25 | if (G[i][v] && !V[i]) 26 | DFSt(i); 27 | } 28 | 29 | 30 | int main() { 31 | int a, b, t; 32 | while(cin >> n >> m, n|m) { 33 | memset(G, 0, sizeof(G)); 34 | 35 | while(m--) { 36 | cin >> a >> b >> t; 37 | G[a][b] = true; 38 | if (t==2) 39 | G[b][a] = true; 40 | } 41 | 42 | npv = 0; 43 | memset(V, 0, sizeof(V)); 44 | memset(O, 0, sizeof(O)); 45 | 46 | for(int i = 1; i <= n; i++) 47 | if(!V[i]) DFS(i); 48 | 49 | memset(V, 0, sizeof(V)); 50 | 51 | int comp = 0; 52 | for(int i = n; i > 0; i--) 53 | if(!V[O[i]]) { 54 | comp++; 55 | DFSt(O[i]); 56 | } 57 | 58 | cout << (comp==1) << endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /uva/11857.cpp: -------------------------------------------------------------------------------- 1 | //11857 2 | //Driving Range 3 | //Graphs;Minimum Spanning Tree;Kruskal 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | struct Edge { 12 | int x, y, v; 13 | inline bool operator <(const Edge& that) const { 14 | return this->v < that.v; 15 | } 16 | }; 17 | 18 | Edge E[1000005]; 19 | int P[1000005]; 20 | 21 | inline int findset(int v) { 22 | if (P[v] != v) 23 | return P[v] = findset(P[v]); 24 | return v; 25 | } 26 | 27 | inline int unionset(int x, int y) { 28 | int a = findset(x), b = findset(y); 29 | if (a==b) return -1; 30 | if (a>b) swap(a,b); 31 | P[b] = a; 32 | return a; 33 | } 34 | 35 | int main() { 36 | int n, m; 37 | while(cin >> n >> m, n||m) { 38 | for(int i=0; i> E[i].x >> E[i].y >> E[i].v; 43 | 44 | sort(E, E+m); 45 | 46 | int maxx=0, count=0; 47 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | map M; 11 | vector V; 12 | 13 | int main() { 14 | int n, t=0; 15 | while(cin >> n) { 16 | M.clear(); V.clear(); 17 | if (t++) cout << endl; 18 | 19 | for(int i=0; i> s; 22 | M[s] = 0; 23 | V.push_back(s); 24 | } 25 | 26 | for(int i=0; i> a >> g >> m; 29 | if (!m) continue; 30 | g /= m; 31 | 32 | for(int j=0; j> b; 35 | M[b] += g; 36 | M[a] -= g; 37 | } 38 | } 39 | 40 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int P[1000]; 13 | double X[1000], Y[1000]; 14 | 15 | inline int findset(int v) { 16 | if (P[v] == v) return v; 17 | return P[v] = findset(P[v]); 18 | } 19 | 20 | inline bool unionset(int x, int y) { 21 | int a = findset(x), b = findset(y); 22 | if (a==b) return false; 23 | P[b] = a; 24 | return true; 25 | } 26 | 27 | inline double dist(int a, int b) { 28 | return pow(X[a]-X[b], 2.0)+pow(Y[a]-Y[b], 2.0); 29 | } 30 | 31 | int main() { 32 | int t; cin >> t; t=0; 33 | 34 | int n; double d; 35 | while(cin >> n >> d) { 36 | for(int i=0; i> X[i] >> Y[i]; 41 | for(int j=0;j 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | vector G[501], P[30001]; 11 | bool VG[501], VP[30001]; 12 | int n, m; 13 | 14 | int dfs(int v) { 15 | int sum = 1; 16 | VP[v] = true; 17 | 18 | for(int i=0; i> n >> m, n|m) { 35 | memset(G, 0, sizeof(G)); 36 | memset(P, 0, sizeof(P)); 37 | memset(VG, 0, sizeof(VG)); 38 | memset(VP, 0, sizeof(VP)); 39 | 40 | for(int i=0; i> k; 42 | while(k--) { 43 | int a; cin >> a; 44 | G[i].push_back(a); 45 | P[a].push_back(i); 46 | } 47 | } 48 | 49 | cout << dfs(0) << endl; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /uva/1200.cpp: -------------------------------------------------------------------------------- 1 | //1200 2 | //A DP Problem 3 | //Misc;String parsing 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int getSign(string& s, int &i) { 10 | if (s[i] == '+') { i++; return 1; } 11 | if (s[i] == '-') { i++; return -1; } 12 | return 1; 13 | } 14 | 15 | int getNumber(string& s, int& i, bool& got) { 16 | int result = 0; 17 | while(s[i] >='0' && s[i] <= '9') { 18 | result = result*10 + (s[i]-'0'); 19 | i++; 20 | got = true; 21 | } 22 | return result; 23 | } 24 | 25 | bool getX(string& s, int& i) { 26 | return i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int t; cin >> t; 11 | while(t--) { 12 | string s; cin >> s; 13 | 14 | int notzero = 0, visited = 0; 15 | for(int i=0; i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | long K[20][1300]; 10 | bool P[1300]; 11 | vector W(); 12 | 13 | int main() { 14 | int n, k; 15 | 16 | memset(P, true, sizeof(P)); 17 | P[0] = P[1] = false; 18 | for(int i=2; i<1300; i++) { 19 | if (P[i]) { 20 | W.push_back(i); 21 | for(int j=i*i; j<1300; j+=i) 22 | P[j] = false; 23 | } 24 | } 25 | 26 | K[0][0] = 1; 27 | for(int i=0; i0; p--) 29 | for(int j = W[i]; j<1300; j++) 30 | K[p][j]+=K[p-1][j-W[i]]; 31 | 32 | while(cin >> n >> k, n|k) 33 | cout << K[k][n] << endl; 34 | } 35 | -------------------------------------------------------------------------------- /uva/12135.cpp: -------------------------------------------------------------------------------- 1 | //12135 2 | //Switch Bulbs 3 | //Graphs;Shortest Path;BFS 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 33000 9 | using namespace std; 10 | 11 | vector G[MAX]; 12 | int V[MAX]; 13 | 14 | int n, m; 15 | struct Step { 16 | int x, w; 17 | Step() {} 18 | Step(int x, int w) : x(x), w(w) {} 19 | }; 20 | 21 | int main() { 22 | int t; cin >> t; int tt=0; 23 | while(cin >> n >> m, t--) { 24 | memset(G, 0, sizeof(G)); 25 | memset(V, -1, sizeof(V)); 26 | 27 | n = 1<> a; 32 | while(a--) { 33 | cin >> b; 34 | mask = mask | (1< Q; 41 | Q.push(Step(0, 0)); 42 | while(!Q.empty()) { 43 | Step step = Q.front(); Q.pop(); 44 | if (V[step.x] >= 0) continue; 45 | 46 | V[step.x] = step.w; 47 | for(int i=0; i> q; 54 | while(q--) { 55 | int b = 0; 56 | cin >> s; 57 | for(int i=0; i 5 | #include 6 | #define PP 20000 7 | #define ull unsigned long long 8 | 9 | int W[PP], wn=0; 10 | bool P[PP]; 11 | 12 | inline ull div(const ull& a, const ull& b, ull &r) { 13 | r = a/b; 14 | return a-r*b; 15 | } 16 | 17 | inline ull pow(const ull& a, const int b) { 18 | if (b==0) return 1; 19 | ull tmp = b&1 ? a : 1; 20 | ull r = pow(a, b>>1); 21 | return tmp*r*r; 22 | } 23 | 24 | int main() { 25 | for(long long i=2; i*i1 && i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 501 11 | 12 | using namespace std; 13 | 14 | struct Edge { 15 | int u, v, c; 16 | Edge(int u, int v, int c) : u(u), v(v), c(c) {} 17 | inline bool operator < (const Edge& that) const { return c > that.c; } 18 | }; 19 | 20 | int G[MAX][MAX]; 21 | int V[MAX]; 22 | vector D[MAX]; 23 | int n, m, s, t; 24 | 25 | void remove(int t) { 26 | if (D[t].size() == 0 || t == D[t][0]) return; 27 | for(int i=0; i Q; 37 | Q.push(Edge(s, s, 0)); 38 | 39 | while(!Q.empty()) { 40 | Edge item = Q.top(); Q.pop(); 41 | if (item.c > V[item.v]) continue; 42 | V[item.v] = item.c; 43 | D[item.v].push_back(item.u); 44 | 45 | for(int j=0; j> n >> m, n|m) { 63 | cin >> s >> t; 64 | memset(G, 0, sizeof(G)); 65 | 66 | for(int i=0; i> a >> b >> c; 69 | G[a][b] = c; 70 | } 71 | 72 | shortest(); 73 | cout << shortest() << endl; 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /uva/12147.cpp: -------------------------------------------------------------------------------- 1 | //12147 2 | //DNA Sequences 3 | //Dynamic Programming;Longest Common Subsequence 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | int S[MAX][MAX]; 13 | string P, Q; 14 | 15 | int main() { 16 | int k; 17 | while(cin >> k, k) { 18 | cin >> P >> Q; 19 | int p = P.size(), q = Q.size(); 20 | 21 | for(int i=0; i<=p; i++) T[i][0] = S[i][0] = 0; 22 | for(int i=0; i<=q; i++) T[0][i] = S[0][i] = 0; 23 | 24 | for(int i=1; i<=p; i++) { 25 | for(int j=1; j<=q; j++) { 26 | if (P[i-1] == Q[j-1]) 27 | S[i][j] = S[i-1][j-1] + 1; 28 | else 29 | S[i][j] = 0; 30 | } 31 | } 32 | 33 | for(int i=1; i<=p; i++) { 34 | for(int j=1; j<=q; j++) { 35 | T[i][j] = max(T[i-1][j], T[i][j-1]); 36 | 37 | for(int s=k; s<=S[i][j]; s++) 38 | T[i][j] = max(T[i][j], T[i-s][j-s]+s); 39 | } 40 | } 41 | cout << T[p][q] << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /uva/12148.cpp: -------------------------------------------------------------------------------- 1 | //12148 2 | //Electricity 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int M[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 8 | 9 | bool oneday(int ad, int am, int ay, int bd, int bm, int by) { 10 | if (--bd == 0) { 11 | if (--bm == 0) { 12 | --by; 13 | bm=12; 14 | } 15 | 16 | bd = M[bm-1]; 17 | 18 | bool isleap = (by%4==0 && (by%100!=0 || by%400==0)); 19 | if (bm==2 && isleap) bd=29; 20 | } 21 | return ad==bd && am==bm && ay==by; 22 | 23 | } 24 | 25 | int main() { 26 | int n, ad=0, am=0, ay=0, ac=0; 27 | while(cin >> n, n) { 28 | int sum = 0, count=0; 29 | while(n--) { 30 | int bd, bm, by, bc; 31 | cin >> bd >> bm >> by >> bc; 32 | if (oneday(ad, am, ay, bd, bm, by)) { 33 | sum += bc-ac; count++; 34 | } 35 | ad = bd; am = bm; ay = by; ac = bc; 36 | } 37 | cout << count << " " << sum << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /uva/1215.cpp: -------------------------------------------------------------------------------- 1 | //1215 2 | //String Cutting 3 | //Misc;Binary Search 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int T[10001][26]; 10 | int C[1001]; 11 | set K; 12 | 13 | int main() { 14 | int t; cin >> t; t=0; 15 | int k; string s; 16 | while(cin >> k) { 17 | K.clear(); 18 | for(int i=0; i> C[i]; 20 | 21 | cin >> s; 22 | for(int i=1; i<=s.size(); i++) 23 | for(int j=0; j<26; j++) 24 | T[i][j] = T[i-1][j] + (s[i-1] == j+'a'); 25 | 26 | 27 | K.insert(0); 28 | K.insert(s.size()); 29 | 30 | int total = 0; 31 | for(int i=0; i::iterator it = K.lower_bound(mid); 34 | int hi = *it; it--; 35 | int lo = *it; 36 | 37 | for(int j=0; j<26; j++) { 38 | int sidea = T[mid][j]-T[lo][j]; 39 | int sideb = T[hi][j]-T[mid][j]; 40 | 41 | if (sidea>0 ^ sideb>0) total++; 42 | } 43 | K.insert(mid); 44 | } 45 | cout << total << endl; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /uva/12155.cpp: -------------------------------------------------------------------------------- 1 | //12155 2 | //ASCII Diamondi 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | inline int abs(int n) { return n>0?n:-n; } 8 | 9 | inline char charAt(int n, int x, int y) { 10 | x%=n*2-1; y%=n*2-1; 11 | int dist = abs(n-x-1)+abs(n-y-1); 12 | if (dist < n) 13 | return (char)(dist%26+'a'); 14 | else 15 | return '.'; 16 | } 17 | 18 | int main() { 19 | int n, ax, ay, bx, by, t=0; 20 | while(cin >> n, n) { 21 | cin >> ax >> ay >> bx >> by; 22 | cout << "Case " << ++t << ":" << endl; 23 | for(int i=ax; i<=bx; i++) { 24 | for(int j=ay; j<=by; j++) { 25 | cout << charAt(n, i, j); 26 | } 27 | cout << endl; 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /uva/1216.cpp: -------------------------------------------------------------------------------- 1 | //1216 2 | //The Bug Sensor Problem 3 | //Graphs;DFS 4 | #include 5 | #include 6 | #include 7 | #define MAX 1000 8 | using namespace std; 9 | 10 | double G[MAX][MAX]; 11 | int X[MAX], Y[MAX], n, k; 12 | int V[MAX]; 13 | 14 | void dfs(int v, int comp, int max) { 15 | V[v] = comp; 16 | for(int i=0; i> t; t=0; 24 | while(cin >> k) { 25 | n=0; 26 | 27 | double maxd=0; 28 | while(cin >> X[n], X[n]!=-1) { 29 | cin >> Y[n]; 30 | for(int i=0; i k) 51 | begin = mid; 52 | else { 53 | if (comp == k) best = mid; 54 | end = mid; 55 | } 56 | } 57 | 58 | cout << best << endl; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /uva/12160.cpp: -------------------------------------------------------------------------------- 1 | //12160 2 | //Unlock the Lock 3 | //Graphs;Shortest Path;BFS 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | bool V[10000]; 11 | int R[10]; 12 | 13 | struct Step { 14 | int x, w; 15 | Step() {} 16 | Step(int x, int w) : x(x), w(w) {} 17 | 18 | Step sum(int n) { 19 | return Step((x+n)%10000, w+1); 20 | } 21 | }; 22 | 23 | int main() { 24 | int a, b, n, t=0; 25 | while(cin >> a >> b >> n, a|b|n) { 26 | for(int i=0;i> R[i]; 28 | 29 | cout << "Case " << ++t << ": "; 30 | memset(V, 0, sizeof(V)); 31 | queue Q; 32 | Q.push(Step(a, 0)); 33 | bool found = false; 34 | while(!Q.empty()) { 35 | Step step = Q.front(); Q.pop(); 36 | if (V[step.x]) continue; 37 | V[step.x] = true; 38 | if (step.x == b) { 39 | cout << step.w << endl; 40 | found = true; 41 | break; 42 | } 43 | for(int i=0;i 5 | #include 6 | #include 7 | #include 8 | #define MAX 505 9 | using namespace std; 10 | 11 | string V1[MAX], V2[MAX]; 12 | int G[MAX][MAX], n; 13 | bool V[MAX]; 14 | 15 | int send(int s, int t, int minn) { 16 | V[s] = true; 17 | 18 | if (s==t) return minn; 19 | for(int i=0; i<=n; i++) { 20 | if (!V[i] && G[s][i] > 0) { 21 | if (int sent = send(i, t, min(minn, G[s][i]))) { 22 | G[s][i] -= sent; 23 | G[i][s] += sent; 24 | return sent; 25 | } 26 | } 27 | } 28 | return 0; 29 | } 30 | 31 | int main() { 32 | int t; cin >> t; 33 | int c, d, v; 34 | 35 | while(cin >> c >> d >> v, t--) { 36 | memset(G, 0, sizeof(G)); 37 | memset(V, 0, sizeof(V)); 38 | 39 | string s1, s2; 40 | for(int i=1; i<=v; i++) { 41 | cin >> s1 >> s2; 42 | V1[i] = s1; V2[i] = s2; 43 | 44 | bool dog = s1[0] == 'D'; 45 | 46 | if (dog) 47 | G[0][i] = 1; 48 | else 49 | G[i][v+1] = 1; 50 | 51 | for(int j=1; j 5 | #include 6 | #include 7 | #define MAX 101 8 | using namespace std; 9 | 10 | void printMax(int n) { 11 | if (n&1) { cout << "7"; n-=3; } 12 | for(;n;n-=2) cout << "1"; 13 | } 14 | 15 | void printMin(int n) { 16 | switch(n) { 17 | case 2: cout << "1"; return; 18 | case 3: cout << "7"; return; 19 | case 4: cout << "4"; return; 20 | case 5: cout << "2"; return; 21 | case 6: cout << "6"; return; 22 | } 23 | 24 | switch(n%7) { 25 | case 1: cout << "10"; n-=8; break; 26 | case 2: cout << "1"; n-=2; break; 27 | case 3: 28 | if (n==10) { 29 | cout << "22"; n-= 10; 30 | } else{ 31 | cout << "200"; n-=17; 32 | } 33 | break; 34 | case 4: cout << "20"; n-= 11; break; 35 | case 5: cout << "2"; n-= 5; break; 36 | case 6: cout << "6"; n-= 6; break; 37 | } 38 | for(;n;n-=7) cout << "8"; 39 | } 40 | 41 | int main() { 42 | 43 | 44 | int n; 45 | int t; cin >> t; t=0; 46 | 47 | while(cin >> n) { 48 | printMin(n); 49 | cout << " "; 50 | printMax(n); 51 | cout << endl; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /uva/12179.cpp: -------------------------------------------------------------------------------- 1 | //12179 2 | //Randomly-priced Tickets 3 | //Graphs;Shortest Path;Floyd-Warshall 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 101 9 | using namespace std; 10 | 11 | int G[MAX][MAX], n, r, c; 12 | double P[101][10001]; 13 | 14 | int main() { 15 | int t; cin >> t; t=0; 16 | cout << fixed << setprecision(6); 17 | 18 | while(cin >> n >> r) { 19 | memset(G, 0x3F, sizeof(G)); 20 | memset(P, 0, sizeof(P)); 21 | 22 | char cc; 23 | for(int i=0; i> cc; 26 | if (cc=='Y') G[i][j] = 1; 27 | } 28 | } 29 | 30 | for(int k=0; k> c; 44 | while(c--) { 45 | int a, b, m; 46 | cin >> a >> b >> m; 47 | a--; b--; 48 | 49 | int d=G[a][b]; 50 | 51 | double total = 0; 52 | for(int i=0; i<=m; i++) 53 | total += P[d][i]; 54 | cout << total << endl; 55 | } 56 | cout << endl; 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /uva/12184.cpp: -------------------------------------------------------------------------------- 1 | //12184 2 | //Transcribed Books 3 | //Math;GCD 4 | #include 5 | using namespace std; 6 | 7 | long gcd(long a, long b) { 8 | while(b) { 9 | long c = a%b; 10 | a = b; 11 | b = c; 12 | } 13 | return a; 14 | } 15 | 16 | int main() { 17 | int t; cin >> t; 18 | int n; 19 | while(cin >> n) { 20 | long result = 0; 21 | long maxSerial = 0; 22 | for(int i=0; i> tmp; s+=tmp; 26 | } 27 | cin >> tmp; 28 | s -= tmp; 29 | maxSerial = max(maxSerial, tmp); 30 | result = gcd(result, s); 31 | } 32 | if (result>1 && maxSerial < result) 33 | cout << result << endl; 34 | else 35 | cout << "impossible" << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /uva/12186.cpp: -------------------------------------------------------------------------------- 1 | //12186 2 | //Another Crisis 3 | //Graphs;DFS 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 100002 10 | using namespace std; 11 | 12 | vector G[MAX]; 13 | int n, t; 14 | 15 | int dfs(int v) { 16 | if (G[v].empty()) return 1; 17 | vector mins; 18 | for(int i=0; i> n >> t, n|t) { 31 | memset(G, 0, sizeof(G)); 32 | for(int i=1; i<=n; i++) { 33 | cin >> boss; G[boss].push_back(i); 34 | } 35 | cout << dfs(0) << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /uva/12189.cpp: -------------------------------------------------------------------------------- 1 | //12189 2 | //Dinner Hall 3 | //Misc;Sort 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | struct Event { 10 | int s; char t; 11 | Event() {} 12 | Event(int s, char t) : s(s), t(t) {} 13 | int entry() { return t=='E'?1:0; } 14 | int exit() { return t=='X'?1:0; } 15 | int unknown() { return t=='?'?1:0; } 16 | }; 17 | 18 | bool compare(const Event& a, const Event& b) { 19 | return a.s < b.s; 20 | } 21 | 22 | vector V; 23 | 24 | int main() { 25 | int n; 26 | while(cin >> n, n) { 27 | int entries=0, exits=0, unknowns=0; 28 | int a, b, c; char t; 29 | V.clear(); 30 | for(int i=0; i> a >> t >> b >> t >> c >> t; 32 | Event e = Event(a*60*60+b*60+c, t); 33 | entries += e.entry(); 34 | exits += e.exit(); 35 | unknowns += e.unknown(); 36 | V.push_back(e); 37 | } 38 | sort(V.begin(), V.end(), compare); 39 | 40 | int maxEntries = (unknowns-(entries-exits))/2; 41 | int maxx = 0, current=0; 42 | for(int i=0; i 5 | using namespace std; 6 | 7 | int C(int price) { 8 | int cons = 0; 9 | cons += min(max(0, price/2), 100); price -= 2*100; 10 | cons += min(max(0, price/3), 9900); price -= 3*9900; 11 | cons += min(max(0, price/5), 990000); price -= 5*990000; 12 | cons += max(0, price/7); 13 | return cons; 14 | } 15 | 16 | int V(int cons) { 17 | int price = 0; 18 | price += min(max(0, cons*2), 2*100); cons -= 100; 19 | price += min(max(0, cons*3), 3*9900); cons -= 9900; 20 | price += min(max(0, cons*5), 5*990000); cons -= 990000; 21 | price += max(0, cons*7); 22 | return price; 23 | } 24 | 25 | int main() { 26 | int a, b; 27 | while(cin >> a >> b,a|b) { 28 | int total = C(a); 29 | int begin = 0, end = total; 30 | int answer = 0; 31 | while(begin < end) { 32 | int mine = (begin+end)/2; 33 | int diff = V(total-mine)-V(mine); 34 | if (diff > b) 35 | begin = mine; 36 | else if (diff < b) 37 | end = mine; 38 | else { answer = mine; break; } 39 | } 40 | 41 | cout << V(answer) << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /uva/12192.cpp: -------------------------------------------------------------------------------- 1 | //12192 2 | //Grapevine 3 | //Misc;Binary Search 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int T[1001][501]; 11 | int S[1001]; 12 | 13 | int main() { 14 | int n, m, q; 15 | while(cin >> n >> m, n|m) { 16 | memset(S, 0, (m+n)*sizeof(int)); 17 | 18 | for(int i=0; i> T[i-j+m][S[i-j+m]++]; 21 | 22 | cin >> q; 23 | while(q--) { 24 | int L, U; 25 | cin >> L >> U; 26 | int maxx = 0; 27 | for(int i=0;i 5 | #include 6 | #include 7 | #define MAX 1010 8 | using namespace std; 9 | 10 | int X[MAX], Y[MAX]; 11 | long T[MAX][MAX]; 12 | int C[MAX]; 13 | 14 | inline long sqr(long v) { return v*v; } 15 | 16 | int main(){ 17 | int n; 18 | while(scanf("%d", &n), n) { 19 | memset(C, 0, sizeof(C)); 20 | 21 | for(int i=0; i 5 | #include 6 | using namespace std; 7 | 8 | int duration(char c) { 9 | switch(c) { 10 | case 'W': return 64; 11 | case 'H': return 32; 12 | case 'Q': return 16; 13 | case 'E': return 8; 14 | case 'S': return 4; 15 | case 'T': return 2; 16 | case 'X': return 1; 17 | } 18 | } 19 | 20 | int main() { 21 | string s; 22 | while(cin >> s, s!="*") { 23 | int d=0, r=0; 24 | for(int i=1; i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int T[10001][1001]; 10 | int N[10001]; 11 | 12 | inline long abs(long n) { return n>0?n:-n;} 13 | 14 | int main() { 15 | int n, tmp; 16 | while(cin >> n, n) { 17 | memset(T, 0, n*sizeof(T[0])); 18 | for(int i=0; i> N[i]; 20 | for(int j=0; j> tmp; 22 | T[i][tmp]++; 23 | } 24 | for(int j=1;j<=1000;j++) 25 | T[i][j] += T[i][j-1]; 26 | } 27 | 28 | long minn = INT_MAX; 29 | for(int t=0;t<=1000;t++) { 30 | long sum=0; 31 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 205 10 | using namespace std; 11 | 12 | map E; 13 | int emp(string& s) { 14 | if (E.find(s) != E.end()) 15 | return E[s]; 16 | else 17 | return E[s] = E.size()-1; 18 | } 19 | 20 | bool L[MAX], L2[MAX]; 21 | vector G[MAX]; 22 | int n; 23 | 24 | int dfs(int v) { 25 | int acum = 0, illu = 0; 26 | for(int i=0;i 0 && illu < G[v].size()) 31 | L[v] = true; 32 | return acum + L[v]; 33 | } 34 | 35 | int main() 36 | { 37 | while(cin >> n, n) { 38 | memset(G, 0, sizeof(G)); 39 | memset(L, 0, sizeof(L)); 40 | E.clear(); 41 | string a, b; 42 | cin >> a; 43 | emp(a); 44 | for(int i=1;i> a >> b; 46 | G[emp(b)].push_back(emp(a)); 47 | } 48 | 49 | int total = dfs(0); 50 | 51 | memcpy(L2, L, sizeof(L)); 52 | bool unique = true; 53 | for(int i=0; i 5 | #include 6 | #include 7 | #define MAX 5001 8 | using namespace std; 9 | 10 | int T[MAX][MAX]; 11 | 12 | int main() { 13 | int t; cin >> t; t=0; 14 | string s; 15 | while(cin >> s) { 16 | int sz = s.size(); 17 | int maxx = 0; 18 | for(int i=1; i<=sz; i++) { 19 | for(int j=1; j<=sz; j++) { 20 | if (s[i-1] == s[j-1] && i!=j) 21 | maxx = max(maxx, T[i][j] = T[i-1][j-1]+1); 22 | else 23 | T[i][j] = 0; 24 | } 25 | } 26 | 27 | cout << maxx << endl; 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /uva/12300.cpp: -------------------------------------------------------------------------------- 1 | //12300 2 | //Smallest Regular Polygon 3 | //Math;Geometry 4 | #include 5 | #include 6 | #include 7 | #define PI 3.141592653589793238462 8 | using namespace std; 9 | 10 | double cot(double angle) { 11 | return cos(angle)/sin(angle); 12 | } 13 | 14 | int main(){ 15 | int x1, y1, x2, y2, n; 16 | while(cin >> x1 >> y1 >> x2 >> y2 >> n, x1 | y1 | x2 | y2 | n) { 17 | double d = sqrt(pow(x2-x1, 2.0)+pow(y2-y1, 2.0)); 18 | int k = n/2; 19 | double s = sin(PI/n)/sin(PI*k/n)*d; 20 | double A = 0.25*n*s*s*cot(PI/n); 21 | setprecision(6); 22 | cout << fixed << A << endl; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /uva/1231.cpp: -------------------------------------------------------------------------------- 1 | //1231 2 | //ACORN 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #define MAX 2001 8 | using namespace std; 9 | 10 | int S[MAX][MAX], T[MAX][MAX], M[MAX]; 11 | char skip; 12 | int main() { 13 | int cases; cin >> cases; 14 | while(cases--) { 15 | memset(S, 0, sizeof(S)); 16 | memset(M, 0, sizeof(M)); 17 | int t, h, f; 18 | cin >> t >> h >> f; 19 | 20 | for(int i=0; i> k; 22 | while(k--) { 23 | cin >> a; 24 | S[a][i]++; 25 | } 26 | } 27 | 28 | for(int i=h; i>=0; i--) { 29 | for(int j=0; j 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | struct Node { 10 | int a, b, h; 11 | bool leaf; 12 | Node() {} 13 | Node(int a, int b, int h, bool leaf=true) : a(a), b(b), h(h), leaf(leaf) {} 14 | }; 15 | 16 | Node H[5000005]; 17 | inline int left(int i) { return 2*i; } 18 | inline int right(int i) { return 2*i+1; } 19 | 20 | inline void cut(int v, int x) { 21 | H[left(v)] = Node(H[v].a, x, H[v].h); 22 | H[right(v)] = Node(x, H[v].b, H[v].h); 23 | H[v].leaf = false; 24 | } 25 | 26 | int dfs(int v, int a, int b, int h) { 27 | a = max(a, H[v].a); 28 | b = min(b, H[v].b); 29 | if (b<=a) return 0; 30 | 31 | if (!H[v].leaf) 32 | return dfs(left(v), a, b, h) + dfs(right(v), a, b, h); 33 | 34 | if (H[v].h > h) return 0; 35 | if (H[v].a < a) return cut(v, a), dfs(v, a, b, h); 36 | if (b < H[v].b) return cut(v, b), dfs(v, a, b, h); 37 | 38 | H[v].h = h; 39 | return b-a; 40 | } 41 | 42 | int main() { 43 | int n, t; cin >> t; t=0; 44 | while(cin >> n, n) { 45 | H[1] = Node(0, 100000, 0); 46 | 47 | int sum = 0; 48 | while(n--) { 49 | int a, b, h; 50 | cin >> a >> b >> h; 51 | sum += dfs(1, a, b, h); 52 | } 53 | cout << sum << endl; 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /uva/1233.cpp: -------------------------------------------------------------------------------- 1 | //1233 2 | //USHER 3 | //Graphs;Shortest Path;Floyd-Warshall 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 501 10 | 11 | using namespace std; 12 | 13 | int P[MAX]; 14 | int G[MAX][MAX]; 15 | int n, m; 16 | 17 | int main() { 18 | int tt; cin >> tt; 19 | while(tt--) { 20 | int b, p, q; cin >> b >> p >> q; 21 | for(int i=0; i> P[i]; 23 | 24 | memset(G, 0x1f, sizeof(G)); 25 | 26 | G[0][0] = 0; 27 | for(int i=1; i<=p; i++) { 28 | int k; cin >> k; 29 | G[i][i] = 0; 30 | for(int j=0; j> x >> y; 32 | G[i][y] = min(G[i][y], x); 33 | } 34 | } 35 | 36 | for(int k=0; k<=p; k++) 37 | for(int i=0; i<=p; i++) 38 | for(int j=0; j<=p; j++) 39 | G[i][j] = min(G[i][j], G[i][k] + G[k][j]); 40 | 41 | int minn = 1<<30; 42 | for(int i=0; i= b) break; 50 | current--; 51 | answer++; 52 | } 53 | 54 | cout << answer << endl; 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /uva/1235.cpp: -------------------------------------------------------------------------------- 1 | //1235 2 | //Anti Brute Force Lock 3 | //Graphs;Minimum Spanning Tree;Prim 4 | #include 5 | #include 6 | #include 7 | #define MAX 501 8 | 9 | using namespace std; 10 | 11 | int abs(int a) { 12 | return a>0?a:-a; 13 | } 14 | 15 | int d(int a, int b) { 16 | int result = 0; 17 | for(int i=0; i<4; i++) { 18 | int aa=a%10, bb=b%10; 19 | result += min(abs(aa-bb), 10-abs(aa-bb)); 20 | a/=10; b/=10; 21 | } 22 | return result; 23 | } 24 | 25 | int K[MAX], G[MAX][MAX], n; 26 | bool V[MAX]; 27 | int D[MAX]; 28 | 29 | int updateD(int i) { 30 | D[i] = 0; 31 | for(int j=0; j> t; 40 | while(cin >> n) { 41 | memset(V, 0, sizeof(V)); 42 | memset(D, 0x3F, sizeof(D)); 43 | 44 | for(int i=0; i> K[i]; 46 | 47 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 10001 11 | using namespace std; 12 | 13 | int V[MAX], L[MAX], P[MAX], n, gpe; 14 | vector G[MAX]; 15 | 16 | inline int findset(int v) { 17 | if (P[v] != -1 && P[v] != v) 18 | return P[v] = findset(P[v]); 19 | return v; 20 | } 21 | 22 | inline int unionset(int x, int y) { 23 | int a = findset(x), b = findset(y); 24 | if (a V[v]) 38 | unionset(v, w); 39 | } else if(w != u) { 40 | L[v] = min(L[v], V[w]); 41 | } 42 | } 43 | } 44 | 45 | int main() { 46 | int m, q; 47 | while(cin >> n >> m >> q, n|m|q) { 48 | memset(G, 0, sizeof(vector)*(n+1)); 49 | memset(V, 0, sizeof(int)*(n+1)); 50 | memset(L, 0, sizeof(int)*(n+1)); 51 | memset(P, -1, sizeof(int)*(n+1)); 52 | gpe = 0; 53 | 54 | for(int i=0; i> a >> b; 57 | G[a].push_back(b); 58 | G[b].push_back(a); 59 | } 60 | 61 | for(int i=0; i> a >> b; 68 | cout << (findset(a)==findset(b) ? "Y" : "N") << endl; 69 | } 70 | cout << "-" << endl; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /uva/1239.cpp: -------------------------------------------------------------------------------- 1 | //1239 2 | //Greatest K-Palindrome Substring 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1005 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | 13 | int main() { 14 | int t; cin >> t; t=0; 15 | string P; 16 | int k; 17 | while(cin >> P >> k) { 18 | int p = P.size(); 19 | 20 | int maxx=0; 21 | for(int i=p; i>=1; i--) { 22 | for(int j=i; j<=p; j++) { 23 | T[i][j] = T[i+1][j-1] + (P[i-1] == P[j-1] ? 0 : 1); 24 | 25 | if (T[i][j] <= k) 26 | maxx = max(maxx, j-i+1); 27 | } 28 | } 29 | 30 | cout << maxx << endl; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /uva/1246.cpp: -------------------------------------------------------------------------------- 1 | //1246 2 | //Find Terrorists 3 | //Math;Sieve 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | bool P[100]; 10 | int T[10000001]; 11 | vector W; 12 | 13 | long long real_mod(long long a, long long b) { 14 | long long c = a%b; 15 | if (c<0) c+=b; 16 | return c; 17 | } 18 | 19 | int main() { 20 | int n, k; 21 | 22 | memset(P, true, sizeof(P)); 23 | P[0] = P[1] = false; 24 | for(int i=2; i<100; i++) { 25 | if (P[i]) { 26 | W.push_back(i); 27 | for(int j=i*i; j>=0 && j<100; j+=i) 28 | P[j] = false; 29 | } 30 | } 31 | 32 | int t; cin >> t; t=0; 33 | int a, b; 34 | while(cin >> a >> b) { 35 | memset(T, 0, sizeof(int)*(b-a+1)); 36 | 37 | if (a==0) { T[0]-=2; T[1] -= 1; } 38 | if (a==1) { T[0]-=1; } 39 | 40 | for(long long i=2; i*i<=b; i++) { 41 | for(long long j=max(real_mod(i*i+i-a, i), i*i+i-a); j<=(b-a); j+=i) { 42 | T[j]+=2; 43 | } 44 | int tmp = i*i-a; 45 | if (tmp >= 0 && tmp <= (b-a)) 46 | T[tmp]++; 47 | } 48 | 49 | int cnt=0; 50 | for(int i=0; i<=(b-a);i++) { 51 | if (P[T[i]+2]) { 52 | cout << (cnt++?" ":"") << i+a; 53 | } 54 | } 55 | if (!cnt) cout << -1; 56 | cout << endl; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /uva/1247.cpp: -------------------------------------------------------------------------------- 1 | //1247 2 | //Interstar Transport 3 | //Graphs;Shortest Path;Dijkstra 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 30 11 | 12 | using namespace std; 13 | 14 | struct Edge { 15 | int u, v, c; 16 | Edge(int u, int v, int c) : u(u), v(v), c(c) {} 17 | inline bool operator < (const Edge& that) const { return c > that.c; } 18 | }; 19 | 20 | int G[MAX][MAX]; 21 | int V[MAX]; 22 | int D[MAX]; 23 | int n, m; 24 | 25 | void show(int t) { 26 | if (D[t] != t) { 27 | show(D[t]); 28 | cout << " "; 29 | } 30 | 31 | cout << (char)(t+'A'); 32 | } 33 | 34 | int shortest(int a, int b) { 35 | memset(V, 0x3f, sizeof(V)); 36 | priority_queue Q; 37 | Q.push(Edge(a, a, 0)); 38 | 39 | while(!Q.empty()) { 40 | Edge item = Q.top(); Q.pop(); 41 | if (item.c >= V[item.v]) continue; 42 | V[item.v] = item.c; 43 | D[item.v] = item.u; 44 | 45 | for(int j=0; j> n >> m) { 58 | memset(G, 0, sizeof(G)); 59 | 60 | for(int i=0; i> a >> b >> c; 63 | G[a-'A'][b-'A'] = G[b-'A'][a-'A'] = c; 64 | } 65 | 66 | int k; cin >> k; 67 | while(k--) { 68 | char a, b; cin >> a >> b; 69 | shortest(a-'A', b-'A'); 70 | } 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /uva/12482.cpp: -------------------------------------------------------------------------------- 1 | //12482 2 | //Short Story Competition 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int n, li, c; 11 | while(cin >> n >> li >> c) { 12 | string s; 13 | int pag = 1; 14 | int car = 0; 15 | for(int i=0; i 0) car++; 17 | 18 | cin >> s; 19 | if (car + s.size() > c) { 20 | pag++; 21 | car = s.size(); 22 | } else { 23 | car += s.size(); 24 | } 25 | //cout << s<< " " << pag << " " << car << endl; 26 | } 27 | cout << ceil(pag/(double)li) << endl; 28 | 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /uva/12484.cpp: -------------------------------------------------------------------------------- 1 | //12484 2 | //Cards 3 | //Dynamic Programming;Minimax 4 | #include 5 | #include 6 | #include 7 | #define ull long long 8 | using namespace std; 9 | 10 | ull T[10006], Q[10006], M[10006]; 11 | 12 | 13 | int main() { 14 | int n; 15 | while(cin >> n) { 16 | for(int i=1; i<=n; i++) { 17 | cin >> M[i]; 18 | M[i]+=M[i-1]; 19 | } 20 | 21 | memset(T, 0, sizeof(ull)*n); 22 | 23 | for(int i=1; i<=n; i++) { 24 | for(int j=0; j<=n-i; j++) 25 | Q[j] = M[j+i]-M[j] - min(T[j], T[j+1]); 26 | 27 | swap(T, Q); 28 | } 29 | 30 | cout << T[0] << endl; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /uva/12485.cpp: -------------------------------------------------------------------------------- 1 | //12485 2 | //Perfect Choir 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int T[10005]; 10 | 11 | int main() { 12 | int n; 13 | while(cin >> n) { 14 | memset(T, 0, sizeof(T)); 15 | int total = 0; 16 | for(int i=0; i> a; 18 | total += a; 19 | T[i] = a; 20 | } 21 | 22 | if (total % n != 0) { 23 | cout << -1 << endl; 24 | continue; 25 | } 26 | 27 | int media = total / n; 28 | int maior = 0; 29 | for(int i=0; i media) 31 | maior += T[i] - media; 32 | } 33 | 34 | cout << maior +1 << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /uva/12488.cpp: -------------------------------------------------------------------------------- 1 | //12488 2 | //Start Grid 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int L[30], C[30]; 10 | 11 | int abs(int n) { 12 | if (n < 0) return -n; 13 | return n; 14 | } 15 | 16 | int main() { 17 | int n; 18 | while(cin >> n) { 19 | for(int i=0; i> L[i]; 21 | 22 | for(int i=0; i> C[i]; 24 | 25 | int total = 0; 26 | for(int i=0; ii; j--) { 29 | if (L[j]==cara) { 30 | int t = L[j]; 31 | L[j] = L[j-1]; 32 | L[j-1] = t; 33 | total++; 34 | } 35 | } 36 | } 37 | 38 | cout << total << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /uva/12493.cpp: -------------------------------------------------------------------------------- 1 | //12493 2 | //Stars 3 | //Math;Prime Factorization;Euler's Totient 4 | #include 5 | #include 6 | #define PP 100000 7 | #define ull unsigned long long 8 | using namespace std; 9 | 10 | bool P[PP]; 11 | 12 | int main() { 13 | for(long long i=2; i*i> n) { 22 | ull tot = 1; 23 | //cout << ">>" << n << endl; 24 | for(ull i=2; i*i<=n && n>1; i++) { 25 | if (P[i]) continue; 26 | ull q=0; 27 | 28 | while(n%i==0) { 29 | n/=i; 30 | q++; 31 | } 32 | 33 | //if (q>0) 34 | //cout << i << " " << q << endl; 35 | if (q>0) 36 | tot *= (i-1) * (ull)pow(i, q-1); 37 | } 38 | if (n>1) 39 | tot *= n-1; 40 | 41 | cout << tot/2 << endl; 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /uva/12506.cpp: -------------------------------------------------------------------------------- 1 | //12506 2 | //Shortest Names 3 | //Misc;String Matching;Trie 4 | #include 5 | #include 6 | #define MAXS 1000010 7 | using namespace std; 8 | 9 | struct Trie { 10 | int G[MAXS][26]; 11 | int S[MAXS]; 12 | int stateCount; 13 | 14 | Trie() { 15 | clear(); 16 | } 17 | 18 | void clear() { 19 | stateCount = 0; 20 | clear(stateCount++); 21 | } 22 | 23 | int clear(int state) { 24 | memset(G[state], -1, sizeof G[state]); 25 | S[state] = 0; 26 | return state; 27 | } 28 | 29 | void add(string &s) { 30 | int state = 0; 31 | for(int i=0; i> tt; 63 | while(tt--) { 64 | T.clear(); 65 | 66 | int n; cin >> n; 67 | for(int i=0; i> s; 69 | T.add(s); 70 | } 71 | 72 | cout << dfs(0) << endl; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /uva/1251.cpp: -------------------------------------------------------------------------------- 1 | //1251 2 | //Repeated Substitution with Sed 3 | //Graphs;Shortest Path;BFS 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1000 9 | using namespace std; 10 | 11 | struct Item { 12 | string s; 13 | int c; 14 | Item(string s, int c) : s(s), c(c) {} 15 | }; 16 | 17 | string replace(string str, string from, string to) { 18 | if(from.empty()) 19 | return str; 20 | int start_pos = 0; 21 | while((start_pos = str.find(from, start_pos)) != string::npos) { 22 | str.replace(start_pos, from.length(), to); 23 | start_pos += to.length(); 24 | } 25 | return str; 26 | } 27 | 28 | int n; 29 | string A[MAX], B[MAX]; 30 | 31 | int main() { 32 | while(cin >> n, n) { 33 | for(int i=0; i> A[i] >> B[i]; 35 | string a, b; 36 | cin >> a >> b; 37 | 38 | queue Q; 39 | set S; 40 | Q.push(Item(a, 0)); 41 | 42 | int answer = -1; 43 | while(!Q.empty()) { 44 | Item e = Q.front(); Q.pop(); 45 | if (e.s == b) { 46 | answer = e.c; 47 | break; 48 | } 49 | 50 | if (S.find(e.s) != S.end()) continue; 51 | S.insert(e.s); 52 | 53 | for(int i=0; i 10) continue; 56 | Q.push(Item(s, e.c+1)); 57 | } 58 | } 59 | 60 | cout << answer << endl; 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /uva/1263.cpp: -------------------------------------------------------------------------------- 1 | //1263 2 | //Mines 3 | //Graphs;Topological Sorting 4 | #include 5 | #include 6 | #define MAX 2001 7 | using namespace std; 8 | 9 | int n; 10 | bool V[MAX], G[MAX][MAX]; 11 | int X[MAX], Y[MAX], D[MAX], O[MAX], npv; 12 | 13 | inline int abs(int a) { 14 | return a>0?a:-a; 15 | } 16 | 17 | void dfs(int v, bool sort){ 18 | V[v] = true; 19 | for(int i = 1; i <= n; i++) 20 | if (G[v][i] && !V[i]) 21 | dfs(i, sort); 22 | if (sort) 23 | O[++npv] = v; 24 | } 25 | 26 | int main() { 27 | int t; cin >> t; t=0; 28 | while(cin >> n) { 29 | memset(G, 0, sizeof(G)); 30 | for(int i=1; i<=n; i++) { 31 | cin >> X[i] >> Y[i] >> D[i]; 32 | } 33 | for(int i=1; i<=n; i++) { 34 | for(int j=1; j<=n; j++) { 35 | int r = D[i]/2; 36 | if (abs(X[j]-X[i])<=r && abs(Y[j]-Y[i]) <=r && i!=j) 37 | G[i][j] = true; 38 | } 39 | } 40 | 41 | npv = 0; 42 | memset(V, 0, sizeof(V)); 43 | memset(O, 0, sizeof(O)); 44 | 45 | for(int i = 1; i <= n; i++) 46 | if(!V[i]) dfs(i, true); 47 | 48 | memset(V, 0, sizeof(V)); 49 | 50 | int comp = 0; 51 | for(int i = n; i > 0; i--) 52 | if(!V[O[i]]) { 53 | comp++; 54 | dfs(O[i], false); 55 | } 56 | 57 | cout << comp << endl; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /uva/136.cpp: -------------------------------------------------------------------------------- 1 | //136 2 | //Ugly Numbers 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #define ull unsigned long long 7 | using namespace std; 8 | 9 | struct Number { 10 | ull n, p; 11 | Number(ull n, ull p) : n(n), p(p) {} 12 | inline const bool operator < (const Number& that) const { 13 | return n > that.n; 14 | } 15 | }; 16 | 17 | priority_queue Q; 18 | int main() { 19 | Q.push(Number(1,1)); 20 | for(int i=1; i<1500; i++) { 21 | Number last = Q.top(); Q.pop(); 22 | if (last.p <= 2) Q.push(Number(last.n*2, 2)); 23 | if (last.p <= 3) Q.push(Number(last.n*3, 3)); 24 | if (last.p <= 5) Q.push(Number(last.n*5, 5)); 25 | } 26 | 27 | cout << "The 1500'th ugly number is " << Q.top().n << "." << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /uva/160.cpp: -------------------------------------------------------------------------------- 1 | //160 2 | //Factors and Factorials 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int W[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }, wn = 25; 9 | int T[101][25]; 10 | 11 | int main() { 12 | for(int i=2; i<=100; i++) { 13 | int p = i; 14 | for(int j=0; j1; j++) { 18 | while(p%W[j]==0) { 19 | p/=W[j]; 20 | T[i][j]++; 21 | } 22 | } 23 | } 24 | 25 | int n; 26 | while(cin >> n, n) { 27 | cout << setw(3) << right << n << "! ="; 28 | int count = 0; 29 | for(int i=0; i 15) { 32 | count = 1; 33 | cout << endl << " "; 34 | } 35 | cout << setw(3) << right << T[n][i]; 36 | } 37 | cout << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /uva/245.cpp: -------------------------------------------------------------------------------- 1 | //245 2 | //Uncompress 3 | //Misc;Linked List 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #define MAX 1000 12 | using namespace std; 13 | 14 | list W; 15 | stringstream sin; 16 | int curnum=0; 17 | bool word=false, number=false; 18 | 19 | void finishWord() { 20 | W.push_back(sin.str()); 21 | sin.str(""); 22 | word = false; 23 | } 24 | 25 | void finishNumber() { 26 | list::iterator it = W.end(); 27 | while(curnum--) 28 | it--; 29 | 30 | cout << *it; 31 | W.push_back(*it); 32 | W.erase(it); 33 | 34 | curnum = 0; 35 | number = false; 36 | } 37 | 38 | int main() { 39 | string s; 40 | 41 | while(getline(cin, s), s!="0") { 42 | for(int i=0; i= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { 45 | sin << c; 46 | word = true; 47 | } else if (word) finishWord(); 48 | 49 | if (c >= '0' && c <= '9') { 50 | curnum *= 10; curnum += c-'0'; 51 | number = true; 52 | } else if (number) finishNumber(); 53 | 54 | if (!number) 55 | cout << c; 56 | } 57 | if (word) finishWord(); 58 | if (number) finishNumber(); 59 | 60 | cout << endl; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /uva/315.cpp: -------------------------------------------------------------------------------- 1 | //315 2 | //Network 3 | //Graphs;Finding Articulation Points 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 101 9 | using namespace std; 10 | int G[MAX][MAX], V[MAX], L[MAX], P[MAX], n, gpe; 11 | 12 | void dfs(int u, int v) { 13 | V[v] = L[v] = ++gpe; 14 | for(int i = 1; i <= n; i++) { 15 | if(G[v][i]) { 16 | if(!V[i]){ 17 | dfs(v, i); 18 | L[v] = min(L[v], L[i]); 19 | if(L[i] >= V[v]) P[v]++; 20 | } else if(i != u) { 21 | L[v] = min(L[v], V[i]); 22 | } 23 | } 24 | } 25 | } 26 | 27 | int main() { 28 | while(cin >> n, n) { 29 | memset(G, 0, sizeof(G)); 30 | memset(V, 0, sizeof(V)); 31 | memset(L, 0, sizeof(L)); 32 | memset(P, 0, sizeof(P)); 33 | gpe = 0; 34 | 35 | int a, b; string s; 36 | while(getline(cin, s), s != "0") { 37 | stringstream sin(s); 38 | sin >> a; 39 | while(sin >> b) { 40 | G[a][b] = G[b][a] = 1; 41 | } 42 | } 43 | dfs(1, 1); P[1]--; 44 | int cnt = 0; 45 | for(int i=1; i<=n; i++) 46 | if (P[i]) cnt++; 47 | 48 | cout << cnt << endl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /uva/424.cpp: -------------------------------------------------------------------------------- 1 | //424 2 | //Integer Inquiry 3 | //Math;Big Integer 4 | #define MAX 110 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int T[MAX]; 10 | int n=0; 11 | 12 | void add(string& s) { 13 | int c=0; 14 | int m = s.size(); 15 | for(int i=0; i> s, s!="0") 34 | add(s); 35 | 36 | for(int i=n-1; i>=0; i--) 37 | cout << T[i]; 38 | cout << endl; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /uva/458.cpp: -------------------------------------------------------------------------------- 1 | //458 2 | //The Decoder 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | for(string s; cin>>s;) { 10 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #define MAX 10005 9 | using namespace std; 10 | 11 | int S[MAX], T[MAX]; 12 | char skip; 13 | int main() { 14 | int n,t,m,cases; 15 | cin >> cases; 16 | while(cases--) { 17 | cin >> n >> t >> m; 18 | memset(T, 0x3F, sizeof(T)); 19 | 20 | for(int i=1; i<=n; i++) { 21 | cin >> S[i]; 22 | if (i> skip; 23 | } 24 | 25 | int k=0; 26 | T[0] = 0; 27 | 28 | for(int i=1;i<=n;i++) { 29 | for(int j=k; j>=0; j--) { 30 | int add = 0; 31 | if ((T[j]%t)+S[i] > t) add = t-T[j]%t; 32 | if (T[j]+S[i]+add <= T[j+1]) { 33 | T[j+1] = T[j]+S[i]+add; 34 | k=max(k, j+1); 35 | } 36 | } 37 | } 38 | 39 | int answer = 0; 40 | for(int i=0; i<=k && T[i] <= m*t; i++) 41 | answer = i; 42 | 43 | cout << answer << endl; 44 | if (cases) cout << endl; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /uva/494.cpp: -------------------------------------------------------------------------------- 1 | //494 2 | //Kindergarten Counting Game 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | string s; 10 | while(getline(cin, s)) { 11 | bool inside = false; 12 | int words = 0; 13 | for(int i=0; i= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') { 15 | inside = true; 16 | } else if (inside) { 17 | inside = false; 18 | words++; 19 | } 20 | } 21 | if (inside) words++; 22 | cout << words << endl; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /uva/573.cpp: -------------------------------------------------------------------------------- 1 | //573 2 | //The Snail 3 | //Misc;Ad hoc 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int h,u,d,f; 9 | while(cin >> h >> u >> d >> f, h|u|d|f) { 10 | double current = 0, speed=u; 11 | 12 | for (int i=1;;i++) { 13 | current += speed; 14 | speed = max(0.0, speed-f/100.0*u); 15 | if (current > h) { 16 | cout << "success on day " << i << endl; 17 | break; 18 | } 19 | current -= d; 20 | if (current < 0) { 21 | cout << "failure on day " << i << endl; 22 | break; 23 | } 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /uva/579.cpp: -------------------------------------------------------------------------------- 1 | //579 2 | //ClockHands 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int x, y; char c; 11 | while(cin >> x >> c >> y, x|y) { 12 | double a = x*30+(y/2.0); 13 | double b = y*6.0; 14 | double r = abs(a-b); 15 | if (r > 180.0) 16 | r = 360.0-r; 17 | 18 | cout << fixed << setprecision(3) << r << endl; 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /uva/591.cpp: -------------------------------------------------------------------------------- 1 | //591 2 | //Box of Bricks 3 | //Misc;Ad hoc 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int T[100]; 10 | 11 | int main() { 12 | int n, t=0; 13 | while(cin >> n, n) { 14 | int s=0; 15 | for(int i=0; i> T[i]; 17 | s += T[i]; 18 | } 19 | s/=n; 20 | 21 | int r=0; 22 | for(int i=0; i 5 | #include 6 | #include 7 | #define MAX 1001 8 | using namespace std; 9 | int G[MAX][MAX], V[MAX], L[MAX], n, m, gpe; 10 | 11 | void dfs(int u, int v) { 12 | V[v] = L[v] = ++gpe; 13 | for(int i = 1; i <= n; i++) { 14 | if(G[v][i]) { 15 | if(!V[i]){ 16 | dfs(v, i); 17 | L[v] = min(L[v], L[i]); 18 | if(L[i] <= V[v]) { 19 | G[i][v] = 0; 20 | } 21 | } else if(i != u) { 22 | L[v] = min(L[v], V[i]); 23 | G[i][v] = 0; 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main() { 30 | int tt = 0; 31 | while(cin >> n >> m, n|m) { 32 | memset(G, 0, sizeof(G)); 33 | memset(V, 0, sizeof(V)); 34 | memset(L, 0, sizeof(L)); 35 | gpe = 0; 36 | 37 | cout << ++tt << endl << endl; 38 | 39 | for(int i=0; i> a >> b; 41 | G[a][b] = G[b][a] = 1; 42 | } 43 | 44 | for(int i=1; i<=n; i++) 45 | if (!V[i]) 46 | dfs(i, i); 47 | 48 | for(int i=1; i<=n; i++) 49 | for(int j=1; j<=n; j++) 50 | if (G[i][j]) 51 | cout << i << " " << j < 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | struct Item { 11 | int p, x; 12 | string v; 13 | char m; 14 | 15 | Item() { } 16 | Item(int p, string v, int x, char m) : p(p), v(v), x(x), m(m) { } 17 | }; 18 | 19 | map M; 20 | Item Q[400000]; 21 | int qq=0; 22 | 23 | void add(int p, string v, int i, int j, char m) { 24 | if (not (i%3 == j%3 ^ i/3 == j/3) || j<0 || j>=9) return; 25 | swap(v[i], v[j]); 26 | if (M.find(v) != M.end()) return; 27 | Q[M[v]=qq++] = Item(p, v, j, m); 28 | } 29 | 30 | int main() { 31 | Q[M["123456780"]=qq++] = Item(-1, "123456780", 8, 'z'); 32 | 33 | for(int i=0; i> tt; 44 | while(tt--) { 45 | stringstream ss; 46 | for(int i=0; i<9; i++) { 47 | string s; cin >> s; 48 | if (s=="x") s="0"; 49 | ss << s; 50 | } 51 | 52 | string s = ss.str(); 53 | if (M.find(s) == M.end()) { 54 | cout << "unsolvable" << endl; 55 | } else { 56 | Item item = Q[M[s]]; 57 | 58 | while(item.p != -1) { 59 | cout << item.m; 60 | item = Q[item.p]; 61 | } 62 | cout << endl; 63 | } 64 | if (tt) cout << endl; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /uva/719.cpp: -------------------------------------------------------------------------------- 1 | //719 2 | //Glass Beads 3 | //Misc;String Matching;Suffix Array;Circular 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 10050 10 | using namespace std; 11 | 12 | int RA[MAX], tempRA[MAX]; 13 | int SA[MAX], tempSA[MAX]; 14 | int C[MAX]; 15 | 16 | void suffix_sort(int n, int k) { 17 | memset(C, 0, sizeof C); 18 | 19 | for (int i = 0; i < n; i++) 20 | C[RA[(i + k)%n]]++; 21 | 22 | int sum = 0; 23 | for (int i = 0; i < max(256, n); i++) { 24 | int t = C[i]; 25 | C[i] = sum; 26 | sum += t; 27 | } 28 | 29 | for (int i = 0; i < n; i++) 30 | tempSA[C[RA[(SA[i] + k)%n]]++] = SA[i]; 31 | 32 | memcpy(SA, tempSA, n*sizeof(int)); 33 | } 34 | 35 | void suffix_array(string &s) { 36 | int n = s.size(); 37 | 38 | for (int i = 0; i < n; i++) 39 | RA[i] = s[i]; 40 | 41 | for (int i = 0; i < n; i++) 42 | SA[i] = i; 43 | 44 | for (int k = 1; k < n; k *= 2) { 45 | suffix_sort(n, k); 46 | suffix_sort(n, 0); 47 | 48 | int r = tempRA[SA[0]] = 0; 49 | for (int i = 1; i < n; i++) { 50 | int s1 = SA[i], s2 = SA[i-1]; 51 | bool equal = true; 52 | equal &= RA[s1] == RA[s2]; 53 | equal &= RA[(s1+k)%n] == RA[(s2+k)%n]; 54 | 55 | tempRA[SA[i]] = equal ? r : ++r; 56 | } 57 | 58 | memcpy(RA, tempRA, n*sizeof(int)); 59 | } 60 | } 61 | 62 | int main() { 63 | int tt; cin >> tt; 64 | while(tt--) { 65 | string s; cin >> s; 66 | suffix_array(s); 67 | cout << SA[0]+1 << endl; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /uva/796.cpp: -------------------------------------------------------------------------------- 1 | //796 2 | //Critical Links 3 | //Graphs;Finding Bridges 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 101 11 | using namespace std; 12 | int G[MAX][MAX], V[MAX], L[MAX], n, gpe; 13 | 14 | struct Ponte { 15 | int a, b; 16 | Ponte() { } 17 | Ponte(int a, int b) : a(min(a, b)), b(max(a, b)) {} 18 | }; 19 | bool comp(const Ponte& a, const Ponte& b) { return a.a < b.a || (a.a==b.a && a.b < b.b); } 20 | vector P; 21 | 22 | void dfs(int u, int v) { 23 | V[v] = L[v] = ++gpe; 24 | for(int i = 0; i < n; i++) { 25 | if(G[v][i]) { 26 | if(!V[i]){ 27 | dfs(v, i); 28 | L[v] = min(L[v], L[i]); 29 | if(L[i] > V[v]) P.push_back(Ponte(v, i)); 30 | } else if(i != u) { 31 | L[v] = min(L[v], V[i]); 32 | } 33 | } 34 | } 35 | } 36 | 37 | int main() { 38 | while(cin >> n) { 39 | memset(G, 0, sizeof(G)); 40 | memset(V, 0, sizeof(V)); 41 | memset(L, 0, sizeof(L)); 42 | P.clear(); 43 | gpe = 0; 44 | 45 | int a, an, b; char skip; 46 | for(int i=0; i> a >> skip >> an >> skip; 48 | while(an--) { 49 | cin >> b; G[b][a] = G[a][b] = 1; 50 | } 51 | } 52 | 53 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define MAX 1006 11 | using namespace std; 12 | 13 | int G[MAX][MAX], n; 14 | int F[MAX][MAX]; 15 | bool V[MAX]; 16 | 17 | int send(int s, int t, int minn) { 18 | V[s] = true; 19 | 20 | if (s==t) return minn; 21 | 22 | for(int i=1; i<=n; i++) { 23 | int capacity = G[s][i]-F[s][i]; 24 | if (!V[i] && G[s][i]-F[s][i] > 0) { 25 | if (int sent = send(i, t, min(minn, capacity))) { 26 | F[s][i] += sent; 27 | F[i][s] -= sent; 28 | return sent; 29 | } 30 | } 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | int main() { 37 | int tt=0; 38 | while(cin >> n, n) { 39 | memset(G, 0, sizeof(G)); 40 | memset(F, 0, sizeof(F)); 41 | memset(V, 0, sizeof(V)); 42 | 43 | tt++; 44 | int s, t, c; 45 | cin >> s >> t >> c; 46 | for(int i=0;i> a >> b >> f; 49 | G[a][b] = G[b][a] += f; 50 | } 51 | 52 | int total = 0; 53 | while(int sent = send(s, t, INT_MAX)) { 54 | total += sent; 55 | memset(V, 0, sizeof(V)); 56 | } 57 | 58 | cout << "Network " << tt << endl; 59 | cout << "The bandwidth is " << total << "." << endl; 60 | cout << endl; 61 | } 62 | 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /uva/825.cpp: -------------------------------------------------------------------------------- 1 | //825 2 | //Walking on the Safe Side 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 250 9 | using namespace std; 10 | 11 | int T[MAX][MAX]; 12 | int B[MAX][MAX]; 13 | 14 | int main() { 15 | int t; cin >> t; 16 | for(int tt=1; tt<=t; tt++) { 17 | int r, c; cin >> r >> c; 18 | 19 | string s; 20 | getline(cin, s); 21 | for(int i=0; i> a; 26 | while(sin >> b) { 27 | B[a-1][b-1] = tt; 28 | } 29 | } 30 | 31 | for(int i=0; i0) s+= T[i-1][j]; 35 | if (j>0) s+= T[i][j-1]; 36 | T[i][j] = (B[i][j] != tt ? s : 0); 37 | } 38 | } 39 | 40 | if (tt>1) cout << endl; 41 | cout << T[r-1][c-1] << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /uva/902.cpp: -------------------------------------------------------------------------------- 1 | //902 2 | //Password Search 3 | //Misc;STL map 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | map T; 10 | 11 | int main() { 12 | int n; string s; 13 | while(cin >> n >> s) { 14 | T.clear(); 15 | 16 | int maxx=0, maxv=0; 17 | for(int i=0; i<=s.size()-n; i++){ 18 | int v = ++T[s.substr(i, n)]; 19 | if (v > maxv) { 20 | maxv = v; 21 | maxx = i; 22 | } 23 | } 24 | cout << s.substr(maxx, n) << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /uva/907.cpp: -------------------------------------------------------------------------------- 1 | //907 2 | //Winterim Backpacking Trip 3 | //Dynamic Programming;Integer partition 4 | #define MAX 602 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int T[MAX][MAX], S[MAX], n, k; 11 | 12 | int main() { 13 | while(cin >> n >> k) { 14 | n++; k++; 15 | memset(T, 0, sizeof(T)); 16 | 17 | S[0] = 0; 18 | for(int i=1; i<=n; i++) { 19 | cin >> S[i]; 20 | } 21 | 22 | for(int i=1; i<=n; i++) 23 | T[i][1] = T[i-1][1]+S[i]; 24 | 25 | for(int i=1; i<=k; i++) 26 | T[1][i] = S[1]; 27 | 28 | for(int i=2; i<=n; i++) { 29 | for(int j=2; j<=k; j++) { 30 | T[i][j] = INT_MAX; 31 | for(int x=1; x 5 | #include 6 | #include 7 | #include 8 | #define MAX 1001 9 | using namespace std; 10 | 11 | struct Edge { 12 | int x, y, c; 13 | Edge(int x, int y, int c) : x(x), y(y), c(c) {}; 14 | inline bool operator <(const Edge& a) const { 15 | return c > a.c; 16 | } 17 | }; 18 | 19 | int G[MAX][MAX], V[MAX][MAX]; 20 | int n, m; 21 | priority_queue Q; 22 | 23 | void try_q(int x, int y, int c) { 24 | if (x <= 0 || x > n || y<=0 || y > m || c+G[x][y] >= V[x][y]) return; 25 | Q.push(Edge(x, y, c+G[x][y])); 26 | } 27 | 28 | 29 | int main() { 30 | int T; cin >> T; 31 | for(int tt=1; tt<=T; tt++) { 32 | memset(V, 0x7f, sizeof(V)); 33 | Q = priority_queue(); 34 | 35 | cin >> n >> m; 36 | for(int i=1; i<=n; i++) 37 | for(int j=1; j<=m; j++) 38 | cin >> G[i][j]; 39 | 40 | Q.push(Edge(1, 1, G[1][1])); 41 | 42 | while(!Q.empty()) { 43 | Edge e = Q.top(); Q.pop(); 44 | if (V[e.x][e.y] <= e.c) continue; 45 | 46 | V[e.x][e.y] = e.c; 47 | 48 | if (e.x == n && e.y == m) break; 49 | 50 | try_q(e.x-1, e.y, e.c); 51 | try_q(e.x+1, e.y, e.c); 52 | try_q(e.x, e.y-1, e.c); 53 | try_q(e.x, e.y+1, e.c); 54 | } 55 | 56 | cout << V[n][m] << endl; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /uva/986.cpp: -------------------------------------------------------------------------------- 1 | //986 2 | //How Many? 3 | //Dynamic Programming;Ad hoc 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int T[50][50][50][2]; 9 | 10 | int main() { 11 | int n, r, h; 12 | 13 | while(cin >> n >> r >> h) { 14 | memset(T, 0, sizeof(T)); 15 | T[0][0][0][0] = 1; 16 | 17 | for(int i=1; i<=2*n; i++) { 18 | for(int j=0; j<=2*n; j++) { 19 | for(int k=0; k<=2*n; k++) { 20 | if (j<2*n) { 21 | T[i][j][k][0] += T[i-1][j+1][k][0]; 22 | 23 | if (j+1==h && k>0) 24 | T[i][j][k][0] += T[i-1][j+1][k-1][1]; 25 | else if (j+1!=h) 26 | T[i][j][k][0] += T[i-1][j+1][k][1]; 27 | } 28 | 29 | if (j>0) { 30 | T[i][j][k][1] += T[i-1][j-1][k][0] + T[i-1][j-1][k][1]; 31 | } 32 | 33 | } 34 | } 35 | } 36 | 37 | cout << T[n*2][0][r][0] << endl; 38 | 39 | } 40 | } 41 | --------------------------------------------------------------------------------