ProgrammingContestChallengeBook

POJ 1852 Ants

POJ 2386 [Lake Counting](#Lake Counting)

POJ 1979 [Red and Black](#Red and Black)

AOJ 0118 [Property Distribution](#Property Distribution)

AOJ 0333 Ball

POJ 3009 [Curling 2.0](#Curling 2.0)

AOJ 0558 Cheese

POJ 3669 [Meteor Shower](#Meteor Shower)

AOJ 0121 [Seven Puzzle](#Seven Puzzle)

POJ 2718 [Smallest Difference](#Smallest Difference)

POJ 3187 [Backward Digit Sums](#Backward Digit Sums)

POJ 3050 Hopscotch

AOJ 0525 Osenbei

POJ 3617 [Best Cow Line](#Best Cow Line)

POJ 3069 [Saruman's Army](#Saruman's Army)

POJ 3253 [Fence Repair](#Fence Repair)

POJ 2376 [Cleaning Shifts](#Cleaning Shifts)

POJ 1328 [Radar Installation](#Radar Installation)

POJ 3190 [Stall Reservations](#Stall Reservations)

POJ 2393 [Yogurt Factory](#Yogurt Factory)

POJ 1017 Packets

POJ 3040 Allowance

POJ 1862 Stripies

POJ 3262 [Protecting the Flowers](#Protecting the Flowers)

POJ 3176 [Cow Bowling](#Cow Bowling)

POJ 2229 Sumsets

POJ 2385 [Apple Catching](#Apple Catching)

POJ 3616 [Milking Time](#Milking Time)

POJ 3280 [Cheapest Palindrome](#Cheapest Palindrome)

POJ 1742 Coins

POJ 3046 [Ant Counting](#Ant Counting)

POJ 3181 [Dollar Dayz](#Dollar Dayz)

POJ 1065 [Wooden Sticks](#Wooden Sticks)

POJ 1631 [Bridging signals](#Bridging signals)

POJ 3666 [Making the Grade](#Making the Grade)

POJ 2392 [Space Elevator](#Space Elevator)

POJ 2184 [Cow Exhibition](#Cow Exhibition)

POJ 2431 Expedition

POJ 3614 Sunscreen

POJ 2010 [Moo University - Financial Aid](#Moo University - Financial Aid)

POJ 1182 食物链

POJ 2236 [Wireless Network](#Wireless Network)

POJ 1703 [Find them, Catch them](#Find them, Catch them)

AOJ 2170 [Marked Ancestor](#Marked Ancestor)

AOJ 0189 [Convenient Location](#Convenient Location)

POJ 2139 [Six Degrees of Cowvin Bacon](#Six Degrees of Cowvin Bacon)

POJ 3259 Wormholes

POJ 3268 [Silver Cow Party](#Silver Cow Party)

AOJ 2249 [Road Construction](#Road Construction)

AOJ 2200 [Mr.Rito Post Office](#Mr.Rito Post Office)

POJ 1258 Agri-Net

POJ 2377 [Bad Cowtractors](#Bad Cowtractors)

AOJ 2224 [Save your cat](#Save your cat)

POJ 2395 [Out of Hay](#Out of Hay)

POJ 3255 Roadblocks

POJ 3723 Conscription

POJ 3169 Layout

UVa 10006 [Carmichael Numbers](#Carmichael Numbers)

GCJ 2008 Round 1A A [Minimum Scalar Product](#Minimum Scalar Product)

GCJ 2009 Round 2 A [Crazy Rows](#Crazy Rows)

GCJ 2009 Round 1C C [Bribe the *ers(!)](#Bribe the *ers)

GCJ 2008 APAC Semifinal C Millionaire(!)

AOJ 0005 [GCD and LCM](#GCD and LCM)

POJ 2429 [GCD & LCM Inverse](#GCD & LCM Inverse)

POJ 1930 [Dead Fraction(!)](#Dead Fraction)

AOJ 0009 [Prime Number](#Prime Number)

POJ 3126 [Prime Path](#Prime Path)

POJ 3421 [X-factor Chains](#X-factor Chains)

POJ 3292 [Semi-prime H-numbers(!)](#Semi-prime H-numbers)

POJ 3641 [Pseudoprime numbers](#Pseudoprime numbers)

POJ 1995 [Raising Modulo Numbers](#Raising Modulo Numbers)

POJ 1064 [Cable master](#Cable master)

POJ 2456 [Aggressive cows](#Aggressive cows)

POJ 3061 Subsequence

POJ 3320 [Jessica's Reading Problem](#Jessica's Reading Problem)

POJ 3276 [Face The Right Way](#Face The Right Way)

POJ 3279 Fliptile

POJ 3684 [Physics Experiment](#Physics Experiment)

POJ 2785 [4 Values whose Sum is 0](#4 Values whose Sum is 0)

POJ 3258 [River Hopscotch](#River Hopscotch)

POJ 3273 [Monthly Expense](#Monthly Expense)

POJ 3104 Drying

POJ 3045 [Cow Acrobats](#Cow Acrobats)

POJ 2976 [Dropping tests](#Dropping tests)

POJ 3111 [K Best](#K Best)

POJ 3579 Median

POJ 3685 Matrix

POJ 2010 [Moo University - Financial Aid](#Moo University - Financial Aid Binary-Search)

POJ 3662 [Telephone Lines](#Telephone Lines)

POJ 1759 Garland

POJ 3484 Showstopper(!)

POJ 2566 [Bound Found](#Bound Found)

POJ 2739 [Sum of Consecutive Prime Numbers](#Sum of Consecutive Prime Numbers)

POJ 2100 [Graveyard Design](#Graveyard Design)

POJ 3185 [The Water Bowls](#The Water Bowls)

POJ 1222 [Extended Lights Out](#Extended Lights Out)

POJ 2674 [Linear world](#Linear world)

POJ 3977 Subset

POJ 2549 Sumsets

AOJ 0531 [Paint Color](#Paint Color)

POJ 3041 Asteroids

[POJ 1852](http://poj.org/problem?id=1852) Ants
```cpp
#include
#include
using namespace std;
int main(){
int t;
scanf("%d", &t);
while(t--){
int l, n;
scanf("%d %d", &l, &n);
int ansmin = 0, ansmax = 0;
while(n--){
int pos;
scanf("%d", &pos);
ansmin = max(ansmin, min(pos, l - pos));
ansmax = max(ansmax, max(pos, l - pos));
}
printf("%d %d\n", ansmin, ansmax);
}
return 0;
}

<span id = "Lack Counting"></span>
[POJ 2386](http://poj.org/problem?id=2386) Lake Counting
```cpp
#include <iostream>
using namespace std;
const int N = 111;
char maze[N][N];
int n, m, ans;
void dfs(int x, int y){
maze[x][y] = '.';
for(int cx = -1; cx <= 1; ++cx)
for(int cy = -1; cy <= 1; ++cy){
int nx = x + cx, ny = y + cy;
if(0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] == 'W')
dfs(nx, ny);
}
}
int main(){
while(cin >> n >> m){
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
cin >> maze[i][j];
ans = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(maze[i][j] == 'W'){
++ans;
dfs(i, j);
}
cout << ans << endl;
}
return 0;
}

POJ 1979 Red and Black

#include <iostream>
using namespace std;
const int N = 22;
int w, h, ans, sx, sy;
char maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y){
++ans;
maze[x][y] = '#';
for(int i = 0; i < 4; ++i){
int nx = x + cx[i], ny = y + cy[i];
if(0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.')
dfs(nx, ny);
}
}
int main(){
while(cin >> w >> h, (w && h)){
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j){
cin >> maze[i][j];
if(maze[i][j] == '@'){
sx = i; sy = j;
maze[sx][sy] = '.';
}
}
ans = 0;
dfs(sx, sy);
cout << ans << endl;
}
return 0;
}

AOJ 0118 Property Distribution

#include <iostream>
using namespace std;
const int N = 111;
int w, h, ans;
char ch, maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y){
maze[x][y] = '-';
for(int i = 0; i < 4; ++i){
int nx = x + cx[i], ny = y + cy[i];
if(0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch)
dfs(nx, ny);
}
}
int main(){
while(cin >> h >> w, (h && w)){
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
cin >> maze[i][j];
ans = 0;
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j){
if(maze[i][j] != '-'){
ch = maze[i][j];
++ans;
dfs(i, j);
}
}
cout << ans << endl;
}
return 0;
}

AOJ 0333 Ball

#include <iostream>
#include <algorithm>
using namespace std;
int ball[11];
bool flag;
bool dfs(int index, int left, int right){
if(index == 10) return true;
if(ball[index] > left && dfs(index + 1, ball[index], right)) return true;
if(ball[index] > right && dfs(index + 1, left, ball[index])) return true;
return false; }
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
int left = 0, right = 0;
cin >> n;
while(n--){
for(int i = 0; i < 10; ++i) cin >> ball[i];
flag = dfs(0, 0, 0);
cout << (flag ? "YES" : "NO") << endl;
}
return 0;
}

POJ 3009 Curling 2.0

#include <iostream>
#include <algorithm>
using namespace std; const int N = 22;
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
int w, h, ans, sx, sy, ex, ey;
int maze[N][N]; void dfs(int px, int py, int step){
if(step > 10) return ;
for(int i = 0; i < 4; ++i){
int nx = px + cx[i], ny = py + cy[i];
if(nx == ex && ny == ey){
ans = min(ans, step + 1);
continue;
}
if(nx < 0 || h <= nx || ny < 0 || w <= ny || maze[nx][ny] == 1) continue;
while(true){
nx += cx[i], ny += cy[i];
if(nx < 0 || h <= nx || ny < 0 || w <= ny) break;
if(!maze[nx][ny]) continue;
if(nx == ex && ny == ey){
ans = min(ans, step + 1);
return ;
}
maze[nx][ny] = 0;
dfs(nx - cx[i], ny - cy[i], step + 1);
maze[nx][ny] = 1;
break;
}
}
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(cin >> w >> h, (w && h)){
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j){
cin >> maze[i][j];
if(maze[i][j] == 2){
sx = i;
sy = j;
maze[i][j] = 0;
}
if(maze[i][j] == 3){
ex = i;
ey = j;
maze[i][j] = 1;
}
}
ans = 0x3f3f3f3f;
dfs(sx, sy, 0);
cout << (ans > 10 ? -1 : ans) << endl;
}
return 0;
}

AOJ 0558 Cheese

#include <queue>
#include <cstring>
#include <iostream>
using namespace std; const int N = 1111;
const int cx[] = {0, 0, 1, -1}, cy[] = {1, -1, 0, 0};
char maze[N][N];
int step[N][N];
int h, w, n, sx, sy, ans; int bfs(int cheese){
memset(step, -1, sizeof(step));
queue<pair<int, int> > que;
que.push(make_pair(sx, sy));
step[sx][sy] = 0;
while(!que.empty()){
int x = que.front().first, y = que.front().second;
que.pop();
for(int i = 0; i < 4; ++i){
int nx = x + cx[i], ny = y + cy[i];
if(nx < 0 || h <= nx || ny < 0 || w <= ny || maze[nx][ny] == 'X' || step[nx][ny] != -1) continue;
step[nx][ny] = step[x][y] + 1;
if(maze[nx][ny] - '0' == cheese){
sx = nx;
sy = ny;
return step[nx][ny];
}
que.push(make_pair(nx, ny));
}
}
return 0;
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(cin >> h >> w >> n){
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j){
cin >> maze[i][j];
if(maze[i][j] == 'S'){
sx = i;
sy = j;
}
}
ans = 0;
for(int i = 1; i <= n; ++i)
ans += bfs(i);
cout << ans << endl;
}
return 0;
}

POJ 3669 Meteor Shower

#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1111;
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
int maze[N][N], step[N][N];
int n, sx, sy; int bfs(){
queue<pair<int, int> > que;
que.push(make_pair(0, 0));
while(!que.empty()){
int x = que.front().first, y = que.front().second;
que.pop();
if(step[x][y] >= maze[x][y]) continue;
if(maze[x][y] == 0x3f3f3f3f) return step[x][y];
for(int i = 0; i < 4; ++i){
int nx = x + cx[i], ny = y + cy[i];
if(nx < 0 || ny < 0 || step[nx][ny] != -1) continue;
que.push(make_pair(nx, ny));
step[nx][ny] = step[x][y] + 1;
}
}
return -1;
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(cin >> n){
memset(maze, 0x3f, sizeof(maze));
while(n--){
int x, y, t;
cin >> x >> y >> t;
maze[x][y] = min(maze[x][y], t);
for(int i = 0; i < 4; ++i){
int nx = x + cx[i], ny = y + cy[i];
if(nx < 0 || ny < 0) continue;
maze[nx][ny] = min(maze[nx][ny], t);
}
}
memset(step, -1, sizeof(step));
step[0][0] = 0;
cout << bfs() << endl;
}
return 0;
}

AOJ 0121 Seven Puzzle

#include <map>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int cd[] = {-1, 1, 4, -4};
map<string, int> dp; void dfs(){
dp["01234567"] = 0;
queue<string> que;
que.push("01234567");
while(!que.empty()){
string str = que.front();
que.pop();
int zpos = 0;
for(int i = 0; str[i]; ++i)
if(str[i] == '0'){
zpos = i;
break;
}
for(int i = 0; i < 4; ++i){
int npos = zpos + cd[i];
if(npos < 0 || 7 < npos || (zpos == 3 && cd[i] == 1) || (zpos == 4 && cd[i] == -1)) continue;
string nstr = str;
swap(nstr[zpos], nstr[npos]);
if(dp.find(nstr) == dp.end()){
dp[nstr] = dp[str] + 1;
que.push(nstr);
}
}
}
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
dfs();
string str;
while(getline(cin, str)){
str.erase(remove(str.begin(), str.end(), ' '), str.end());
cout << dp[str] << endl;
}
return 0;
}

POJ 2718 Smallest Difference

#include <string>
#include <iostream>
#include <algorithm>
using namespace std; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
cin.ignore();
while(t--){
string str;
getline(cin, str);
str.erase(remove(str.begin(), str.end(), ' '), str.end());
sort(str.begin(), str.end());
int mid = str.size() >> 1;
int ans = 0x3f3f3f3f;
do{
if(str.size() > 2 && (str[0] == '0' || str[mid] == '0')) continue;
int fnum = 0, anum = 0;
for(int i = 0; i < mid; ++i) fnum = fnum * 10 + str[i] - '0';
for(int i = mid; i < str.size(); ++i) anum = anum * 10 + str[i] - '0';
ans = min(ans, abs(fnum - anum));
}while(next_permutation(str.begin(), str.end()));
cout << ans << endl;
}
return 0;
}

POJ 3187 Backward Digit Sums

#include <iostream>
#include <algorithm>
using namespace std;
int PT[11][11]; // Pascal's Triangle
int seq[11];
void init(){
PT[0][0] = 1;
for(int i = 1; i < 11; ++i){
PT[i][0] = 1;
for(int j = 1; j < i; ++j)
PT[i][j] = PT[i-1][j] + PT[i-1][j-1];
PT[i][i] = 1;
}
} int main(){
init();
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, sum;
while(cin >> n >> sum){
for(int i = 1; i <= n; ++i) seq[i - 1] = i;
do{
long long tmp = 0;
for(int i = 0; i < n; ++i)
tmp += seq[i] * PT[n-1][i];
if(tmp == sum){
for(int i = 0; i < n; ++i){
if(i) cout << " ";
cout << seq[i];
}
cout << endl;
break;
}
}while(next_permutation(seq, seq + n));
}
return 0;
}

POJ 3050 Hopscotch

#include <set>
#include <iostream>
using namespace std; int grid[5][5];
set<int> ans;
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1}; void dfs(int px, int py, int step, int sum){
if(step == 5){
ans.insert(sum);
return ;
}
for(int i = 0; i < 4; ++i){
int nx = px + cx[i], ny = py + cy[i];
if(nx < 0 || 4 < nx || ny < 0 || 4 < ny) continue;
dfs(nx, ny, step + 1, sum * 10 + grid[nx][ny]);
}
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
cin >> grid[i][j];
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
dfs(i, j, 0, grid[i][j]);
cout << ans.size() << endl;
return 0;
}

AOJ 0525 Osenbei

#include <bitset>
#include <iostream>
using namespace std; bitset<10000> maze[10]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int r, c;
while(cin >> r >> c, (r && c)){ for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j){
bool tmp;
cin >> tmp;
maze[i][j] = tmp;
} int time = 1 << r;
int ans = 0;
for(int i = 0; i < time; ++i){ for(int j = 0; j < r; ++j)
if(i & (1 << j))
maze[j].flip(); int result = 0;
for(int C = 0; C < c; ++C){
int cntpercol = 0;
for(int R = 0; R < r; ++R)
if(maze[R][C])
++cntpercol;
result += max(cntpercol, r - cntpercol);
}
ans = max(ans, result); for(int j = 0; j < r; ++j)
if(i & (1 << j))
maze[j].flip();
}
cout << ans << endl;
}
return 0;
}

POJ 3617 Best Cow Line

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
string s = "";
int n;
while(cin >> n){
cin.ignore();
for(int i = 0; i < n; ++i){
char ch;
cin >> ch;
s += ch;
}
string ans = "";
for(int i = 0; i < n; ++i){
string tmp = s;
reverse(tmp.begin(), tmp.end());
if(s < tmp){
ans += s[0];
s = s.substr(1);
}
else{
ans += s[s.size() - 1];
s.erase(s.end()-1, s.end());
}
}
int cnt = 0;
for(int i = 0; i < n; ++i){
if(cnt == 80){
cout << endl;
cnt = 0;
}
cout << ans[i];
++cnt;
}
cout << endl;
}
return 0;
}

POJ 3069 Saruman's Army

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; vector<int> arr;
int r, n; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int r, n;
while(cin >> r >> n, ~r){
arr.clear();
for(int i = 0; i < n; ++i){
int tmp;
cin >> tmp;
arr.push_back(tmp);
}
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
int ans = 0;
for(vector<int>::iterator i = arr.begin(); i != arr.end(); ){
i = upper_bound(i, arr.end(), *i + r) - 1;
++ans;
i = upper_bound(i, arr.end(), *i + r);
}
cout << ans << endl;
}
return 0;
}

POJ 3253 Fence Repair

#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
priority_queue<ll, vector<ll>, greater<ll> > q;
int n;
while(cin >> n){
while(!q.empty()) q.pop();
for(int i = 0; i < n; ++i){
ll tmp;
cin >> tmp;
q.push(tmp);
}
ll ans = 0;
while(q.size() > 1){
ll t = q.top();
q.pop();
t += q.top();
q.pop();
q.push(t);
ans += t;
}
cout << ans << endl;
}
return 0;
}

POJ 2376 Cleaning Shifts

#include <iostream>
#include <algorithm>
using namespace std; const int N = 25252;
struct cow{
int begin, ending;
};
cow p[N];
int n, t, ans, begintime, endtime; bool cmp(cow a, cow b){ return a.begin == b.begin ? a.ending < b.ending : a.begin < b.begin; } int main(){
while(cin >> n >> t){
for(int i = 0; i < n; ++i) cin >> p[i].begin >> p[i].ending;
sort(p, p + n, cmp);
if(p[0].begin > 1){
cout << -1 << endl;
continue;
}
ans = begintime = endtime = 1;
for(int i = 0; i < n; ++i){
if(p[i].begin <= begintime) endtime = max(endtime, p[i].ending);
else{
++ans;
begintime = endtime + 1;
if(p[i].begin <= begintime) endtime = max(endtime, p[i].ending);
else{
ans = -1;
break;
}
}
if(endtime >= t) break;
}
if(endtime < t) ans = -1;
cout << ans << endl;
}
return 0;
}

POJ 1328 Radar Installation

#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 1111; struct coordinate{ double x, y; };
struct interval{ double begin, ending; }; coordinate island[N];
interval cover[N]; bool cmp(interval a, interval b){ return a.begin < b.begin; } int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, d, t = 1;
while(cin >> n >> d, n && d){
cout << "Case " << t++ << ": ";
bool flag = true;
for(int i = 0; i < n; ++i){
cin >> island[i].x >> island[i].y;
if(island[i].y > d) flag = false;
}
if(!flag) cout << -1 << endl;
else{
int ans = 0;
for(int i = 0; i < n; ++i){
double len = sqrt(d * d - island[i].y * island[i].y);
cover[i].begin = island[i].x - len;
cover[i].ending = island[i].x + len;
}
sort(cover, cover + n, cmp);
double pos = -numeric_limits<double>::max();
for(int i = 0; i < n; ++i){
if(pos < cover[i].begin){
++ans;
pos = cover[i].ending;
}
else if(pos > cover[i].ending)
pos = cover[i].ending;
}
cout << ans << endl;
}
}
return 0;
}

POJ 3190 Stall Reservations

#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; struct Cow{
int index;
int begin, ending;
bool operator< (const Cow& other) const {
return begin < other.begin;
}
};
struct Stall{
int id;
int ending;
bool operator< (const Stall& other) const {
return ending > other.ending;
}
Stall(){}
Stall(int _id, int _ending): id(_id), ending(_ending) {}
}; const int N = 55555;
Cow cow[N];
int result[N];
priority_queue<Stall> que; void put_cow(int i, bool new_stall){
Stall s;
if(new_stall) s.id = que.size() + 1;
else{
s.id = que.top().id;
que.pop();
}
result[cow[i].index] = s.id;
s.ending = cow[i].ending;
que.push(s);
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
while(cin >> n){
while(!que.empty()) que.pop();
for(int i = 0; i < n; ++i){
cow[i].index = i;
cin >> cow[i].begin >> cow[i].ending;
}
sort(cow, cow+n);
put_cow(0, true);
for(int i = 1; i < n; ++i)
put_cow(i, cow[i].begin <= que.top().ending);
cout << que.size() << endl;
for(int i = 0; i < n; ++i) cout << result[i] << endl;
}
return 0;
}

POJ 2393 Yogurt Factory

#include <iostream>
#include <algorithm>
using namespace std; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, scost;
while(cin >> n >> scost){
long long ans = 0;
int lowest = 0x3f3f3f3f;
while(n--){
int cost, puc;
cin >> cost >> puc;
lowest = min(lowest+scost, cost);
ans += lowest * puc;
}
cout << ans << endl;
}
return 0;
}

POJ 1017 Packets

#include <iostream>
using namespace std; int puc[6]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(true){
bool flag = false;
for(int i = 0; i < 6; ++i){
cin >> puc[i];
if(puc[i]) flag = true;
}
if(!flag) break;
int ans = puc[5] + puc[4] + puc[3];
puc[0] = max(0, puc[0] - puc[4] * 11);
if(puc[1] > puc[3] * 5) puc[1] -= puc[3] * 5;
else puc[0] = max(0, puc[0] - puc[3] * 20 + puc[1] * 4), puc[1] = 0;
ans += (puc[2] + 3) / 4;
puc[2] %= 4;
if(puc[2]){
if(puc[1] >= 7 - 2 * puc[2]){
puc[1] -= 7 - 2 * puc[2];
puc[0] = max(0, puc[0] - (8 - puc[2]));
}
else{
puc[0] = max(0, puc[0] - (36 - 9 * puc[2] - 4 * puc[1]));
puc[1] = 0;
}
}
ans += (puc[1] + 8) / 9;
puc[1] %= 9;
if(puc[1]) puc[0] = max(0, puc[0] - (36 - 4 * puc[1]));
ans += (puc[0] + 35) / 36;
cout << ans << endl;
}
return 0;
}

POJ 3040 Allowance

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; struct Coin{
int value, amount;
bool operator < (const Coin& other) const {
return value < other.value;
}
}; const int N = 22;
int n, c;
Coin coin[N];
int need[N]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(cin >> n >> c){
for(int i = 0; i < n; ++i) cin >> coin[i].value >> coin[i].amount;
sort(coin, coin + n);
long long ans = 0;
for(int i = 0; i < n; ++i)
if(coin[i].value >= c){
ans += coin[i].amount;
coin[i].amount = 0;
}
while(true){
memset(need, 0, sizeof(need));
int l = c;
for(int i = n-1; ~i; --i){
need[i] = min(l / coin[i].value, coin[i].amount);
l -= coin[i].value * need[i];
}
if(l > 0){
for(int i = 0; i < n; ++i){
if(coin[i].amount && coin[i].value >= l){
++need[i];
l = 0;
break;
}
}
}
if(l > 0) break;
int tmp = 0x3f3f3f3f;
for(int i = 0; i < n; ++i)
if(need[i])
tmp = min(tmp, coin[i].amount / need[i]);
ans += tmp;
for(int i = 0; i < n; ++i)
if(need[i])
coin[i].amount -= need[i] * tmp;
}
cout << ans << endl;
}
return 0;
}

POJ 1862 Stripies

#include <queue>
#include <cmath>
#include <cstdio>
using namespace std; int main(){
int n;
while(~scanf("%d", &n)){
priority_queue<double> que;
while(n--){
double k;
scanf("%lf", &k);
que.push(k);
}
while(que.size() > 1){
double a1 = que.top(); que.pop();
double a2 = que.top(); que.pop();
que.push(2 * sqrt(a1 * a2));
}
printf("%.3f\n", que.top());
que.pop();
}
return 0;
}

POJ 3262 Protecting the Flowers

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; struct Cow{
ll distance, damage;
bool operator < (const Cow& other) const {
return distance * other.damage < other.distance * damage;
}
}; const int N = 111111;
Cow cow[N];
int n; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
while(cin >> n){
ll total = 0;
for(int i = 0; i < n; ++i){
cin >> cow[i].distance >> cow[i].damage;
total += cow[i].damage;
}
sort(cow, cow + n);
ll ans = 0;
for(int i = 0; i < n; ++i){
total -= cow[i].damage;
ans += total * cow[i].distance * 2;
}
cout << ans << endl;
}
return 0;
}

POJ 3176 Cow Bowling

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 365;
int origin[N][N], dp[N][N]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
while(cin >> n){
for(int i = 0; i < n; ++i)
for(int j = 0; j <= i; ++j)
cin >> origin[i][j];
memset(dp, 0, sizeof(dp));
dp[0][0] = origin[0][0];
for(int i = 0; i < n; ++i)
for(int j = 0; j <= i; ++j){
dp[i+1][j] = max(dp[i+1][j], dp[i][j] + origin[i+1][j]);
dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + origin[i+1][j+1]);
}
int ans = -1;
for(int i = 0; i < n; ++i) ans = max(ans, dp[n-1][i]);
cout << ans << endl;
}
return 0;
}

POJ 2229 Sumsets

#include <iostream>
using namespace std; int dp[1111111] = {0, 1}; int main(){
int n;
for(int i = 2; i < 1000001; ++i)
if(i & 1) dp[i] = dp[i-1];
else dp[i] = (dp[i-1] + dp[i>>1]) % 1000000000;
while(cin >> n) cout << dp[n] << endl;
return 0;
}

POJ 2385 Apple Catching

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int arr[1111];
int dp[2][33][1111]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int t, w;
while(cin >> t >> w){
for(int i = 0; i < t; ++i){
int t;
cin >> t;
arr[i] = t - 1;
}
memset(dp, 0, sizeof(dp));
if(arr[0]) dp[1][1][0] = 1;
else dp[0][0][0] = 1;
for(int i = 0; i <= w; ++i)
for(int j = 0; j < t; ++j)
for(int k = 0; k < 2; ++k){
dp[k][i][j+1] = max(dp[k][i][j+1], dp[k][i][j] + (k == arr[j+1]));
dp[k^1][i+1][j+1] = max(dp[k^1][i+1][j+1], dp[k][i][j] + ((k^1) == arr[j+1]));
}
cout << max(*max_element(dp[0][w], dp[0][w]+t), *max_element(dp[1][w], dp[1][w]+t)) << endl;
}
return 0;
}

POJ 3616 Milking Time

#include <iostream>
#include <algorithm>
using namespace std; struct interval{
int stime, etime, efficiency;
bool operator < (const interval& o) const {
return stime < o.stime;
}
}; const int N = 1111;
int dp[N];
interval p[N]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m, r;
while(cin >> n >> m >> r){
for(int i = 0; i < m; ++i){
cin >> p[i].stime >> p[i].etime >> p[i].efficiency;
p[i].etime += r;
}
sort(p, p + m);
for(int i = 0; i < m; ++i){
dp[i] = p[i].efficiency;
for(int j = 0; j < i; ++j){
if(p[i].stime >= p[j].etime)
dp[i] = max(dp[i], dp[j] + p[i].efficiency);
}
}
cout << *max_element(dp, dp + m) << endl;
}
return 0;
}

POJ 3280 Cheapest Palindrome

#include <iostream>
#include <algorithm>
using namespace std; const int N = 2222;
int dp[N][N], cost[N]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
string s;
while(cin >> n >> m){
cin >> s;
for(int i = 0; i < n; ++i){
cin.ignore();
char ch;
int x, y;
cin >> ch >> x >> y;
cost[ch] = min(x, y);
}
for(int k = 1; k < m; ++k){
for(int i = 0, j = k; j < m; ++i, ++j){
dp[i][j] = 0x3f3f3f3f;
if(s[i] == s[j]) dp[i][j] = min(dp[i][j], dp[i+1][j-1]);
else dp[i][j] = min(dp[i][j], min(dp[i+1][j] + cost[s[i]], dp[i][j-1] + cost[s[j]]));
}
}
cout << dp[0][m-1] << endl;
}
return 0;
}

POJ 1742 Coins

#include <cstring>
#include <iostream>
using namespace std; const int N = 101, M = 100001;
int cost[N], num[N], dp[M]; int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
while(cin >> n >> m, n){
for(int i = 0; i < n; ++i) cin >> cost[i];
for(int i = 0; i < n; ++i) cin >> num[i];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j <= m; ++j){
if(dp[j] >= 0) dp[j] = num[i];
else if(j < cost[i] || dp[j-cost[i]] < 0) dp[j] = -1;
else dp[j] = dp[j-cost[i]] - 1;
}
}
int ans = 0;
for(int i = 1; i <= m; ++i) ans += !(dp[i] == -1);
cout << ans << endl;
}
return 0;
}

POJ 3046 Ant Counting

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 1111;
const int MOD = 1000000; int num[N], dp[2][100005]; int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t, a, s, b;
while(cin >> t >> a >> s >> b){
memset(num, 0, sizeof(num));
for(int i = 0;i < a; ++i){
int index;
cin >> index;
++num[index];
}
memset(dp, 0, sizeof(dp));
dp[0][0] = dp[1][0] = 1;
for(int i = 1; i <= t; ++i){
int m = i & 1, n = (i-1) & 1;
for(int j = 1; j < 100003; ++j){
dp[m][j] = dp[m][j-1] + dp[n][j];
if(j > num[i]) dp[m][j] = dp[m][j] - dp[n][j-1-num[i]] + MOD;
while(dp[m][j] >= MOD) dp[m][j] -= MOD;
}
}
int ans = 0; t &= 1;
for(int i = s; i <= b; ++i){
ans += dp[t][i];
while(ans >= MOD) ans -= MOD;
}
cout << ans << endl;
}
return 0;
}

POJ 3181 Dollar Dayz

import java.util.*;
import java.math.*;
public class Main{ static final int N = 1111;
static final int K = 111; static BigInteger dp[][] = new BigInteger[K][N]; static void init(){
for(int i = 0; i < N; ++i) dp[0][i] = BigInteger.ZERO;
for(int i = 0; i < K; ++i) dp[i][0] = BigInteger.ONE;
for(int i = 1; i < K; ++i)
for(int j = 1; j < N; ++j){
dp[i][j] = dp[i-1][j];
if(j >= i) dp[i][j] = dp[i][j].add(dp[i][j-i]);
}
} public static void main(String[] args){
Scanner scan = new Scanner(System.in);
init();
while(scan.hasNext()){
int n = scan.nextInt();
int k = scan.nextInt();
System.out.println(dp[k][n]);
}
scan.close();
}
}

POJ 1065 Wooden Sticks

#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std; typedef pair<int, int> stick;
const int N = 5555;
stick p[N];
int dp[N]; int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
for(int i = 0; i < n; ++i) cin >> p[i].first >> p[i].second;
sort(p, p + n);
memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; ++i)
*lower_bound(dp, dp + n, p[i].second, greater<int>()) = p[i].second;
cout << lower_bound(dp, dp + n, -1, greater<int>()) - dp << endl;
}
return 0;
}

POJ 1631 Bridging signals

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 44444;
const int INF = 0x3f3f3f3f;
int arr[N], dp[N]; int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
memset(dp, INF, sizeof(dp));
for(int i = 0; i < n; ++i) *lower_bound(dp, dp+n, arr[i]) = arr[i];
printf("%d\n", lower_bound(dp, dp+n, INF) - dp);
}
return 0;
}

POJ 3666 Making the Grade

#include <cstdio>
#include <limits>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const int N = 2016;
const ll INF = numeric_limits<ll>::max();
ll arr[N], srt[N], dp[N][N]; ll _abs(ll a){ return a < 0 ? -a : a; }
int main(){
int n;
while(~scanf("%d", &n)){
for(int i = 1; i <= n; ++i){
scanf("%lld", &arr[i]);
srt[i] = arr[i];
}
sort(srt + 1, srt + n + 1);
for(int i = 1; i <= n; ++i){
ll mini = INF;
for(int j = 1; j <= n; ++j){
mini = min(mini, dp[i-1][j]);
dp[i][j] = _abs(arr[i] - srt[j]) + mini;
}
}
ll ans = INF;
for(int i = 1; i <= n; ++i) ans = min(ans, dp[n][i]);
printf("%lld\n", ans);
}
return 0;
}

POJ 2392 Space Elevator

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 40404;
struct block{
int h, l, n;
const bool operator < (const block& o) const {
return l < o.l;
}
};
block arr[404];
int dp[2][40404];
pair<int, int> que[40404]; int main(){
int n;
while(~scanf("%d", &n)){
for(int i = 0; i < n; ++i) scanf("%d %d %d", &arr[i].h, &arr[i].l, &arr[i].n);
sort(arr, arr + n);
int cur = 0, pre = 1;
for(int i = 0; i < n; ++i){
swap(cur, pre);
int v = arr[i].h, w = arr[i].h, V = arr[i].l, c = arr[i].n;
for(int mod = 0; mod < w; ++mod){
int l = 0, r = 0;
for(int j = 0; mod + j * w <= V; ++j){
while(r > l && dp[pre][mod+j*w] - j*v > que[r-1].first) --r;
que[r++] = make_pair(dp[pre][mod+j*w]-j*v, j);
if(r > l && j - que[l].second > c) ++l;
dp[cur][mod+j*w] = que[l].first + j*v;
}
}
}
int ans = *max_element(dp[cur], dp[cur]+arr[n-1].l+1);
printf("%d\n", ans);
}
return 0;
}

POJ 2184 Cow Exhibition

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 111, M = 233333;
const int INF = 0x3f3f3f3f;
struct{ int s, f; } cow[N];
int dp[M]; int main(){
int n;
while(~scanf("%d", &n)){
for(int i = 0; i < n; ++i) scanf("%d %d", &cow[i].s, &cow[i].f);
for(int i = 0; i < M; ++i) dp[i] = -INF;
dp[100000] = 0;
for(int i = 0; i < n; ++i){
if(cow[i].s < 0 && cow[i].f < 0) continue;
if(cow[i].s > 0){
for(int j = 200000; j >= cow[i].s; --j)
if(dp[j - cow[i].s] > -INF)
dp[j] = max(dp[j], dp[j - cow[i].s] + cow[i].f);
}
else{
for(int j = cow[i].s; j <= 200000 + cow[i].s; ++j)
if(dp[j - cow[i].s] > -INF)
dp[j] = max(dp[j], dp[j - cow[i].s] + cow[i].f);
}
}
int ans = 0;
for(int i = 100000; i <= 200000; ++i)
if(dp[i] >= 0)
ans = max(ans, dp[i] + i - 100000);
printf("%d\n", ans);
}
return 0;
}

POJ 2431 Expedition

#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 11111;
int n, l, p;
struct shop {
int dis, fuel;
shop(){}
shop(int dd, int ff): dis(dd), fuel(ff) {}
const bool operator < (const shop& o) const {
return dis < o.dis;
}
} arr[N];
priority_queue<int> que; int main(){
while(~scanf("%d", &n)){
while(!que.empty()) que.pop();
for(int i = 0; i < n; ++i) scanf("%d %d", &arr[i].dis, &arr[i].fuel);
scanf("%d %d", &l, &p);
for(int i = 0; i < n; ++i) arr[i].dis = l - arr[i].dis;
arr[n] = shop(l, 0);
sort(arr, arr + n + 1);
int ans = 0, dis = 0;
for(int i = 0; i <= n; ++i){
dis = arr[i].dis - dis;
while(p < dis){
if(que.empty()){
ans = -1;
break;
}
p += que.top();
que.pop();
++ans;
}
if(ans == -1) break;
que.push(arr[i].fuel);
p -= dis;
dis = arr[i].dis;
}
printf("%d\n", ans);
}
return 0;
}

POJ 3614 Sunscreen

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 25555;
int c, l;
struct Cow {
int minr, maxr;
const bool operator < (const Cow& other) const {
return minr == other.minr ? maxr < other.maxr : minr < other.minr;
}
} cow[N];
struct Lotion {
int r, cover;
const bool operator < (const Lotion& other) const {
return r == other.r ? cover < other.cover : r < other.r;
}
} lotion[N]; int main(){
while(~scanf("%d %d", &c, &l)){
for(int i = 0; i < c; ++i) scanf("%d %d", &cow[i].minr, &cow[i].maxr);
for(int i = 0; i < l; ++i) scanf("%d %d", &lotion[i].r, &lotion[i].cover);
sort(cow, cow + c);
sort(lotion, lotion + l);
priority_queue<int, vector<int>, greater<int> > q;
int ans = 0;
for(int i = 0, index = 0; i < l; ++i){
while(index < c && cow[index].minr <= lotion[i].r){
q.push(cow[index].maxr);
++index;
}
while(!q.empty() && lotion[i].cover){
int minr = q.top(); q.pop();
if(minr >= lotion[i].r){
++ans;
--lotion[i].cover;
}
}
}
printf("%d\n", ans);
}
return 0;
}

POJ 2010 Moo University - Financial Aid

#include <queue>
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std; const int MAXN = 111111;
const int INF = 0x3f3f3f3f;
struct Cow {
int score, aid;
const bool operator < (const Cow& o) const {
return score == o.score ? aid < o.aid : score < o.score;
}
} cow[MAXN];
int N, C, F, lower[MAXN], upper[MAXN]; int main (){
while(~scanf("%d %d %d", &N, &C, &F)){
for(int i = 0; i < C; ++i) scanf("%d %d", &cow[i].score, &cow[i].aid);
sort(cow, cow + C);
int median = N >> 1;
priority_queue<int> q;
for(int i = 0, total = 0; i < C; ++i){
lower[i] = q.size() == median ? total : INF;
q.push(cow[i].aid);
total += cow[i].aid;
if(q.size() > median){
total -= q.top();
q.pop();
}
}
while(!q.empty()) q.pop();
for(int i = C - 1, total = 0; i >= 0; --i){
upper[i] = q.size() == median ? total : INF;
q.push(cow[i].aid);
total += cow[i].aid;
if(q.size() > median){
total -= q.top();
q.pop();
}
}
int ans = -1;
for(int i = C - 1; i >= 0; --i){
if(lower[i] + cow[i].aid + upper[i] <= F){
ans = cow[i].score;
break;
}
}
printf("%d\n", ans);
}
}

POJ 1182 食物链

#include <cstdio>
const int N = 233333; class UnionFind {
public:
int father[N];
void init(int n){ for(int i = 0; i < n; ++i) father[i] = i; }
int find(int n){ return n == father[n] ? n : father[n] = find(father[n]); }
bool same(int x, int y){ return find(x) == find(y); }
void unite(int x, int y){
x = find(x); y = find(y);
if(x != y) father[x] = y;
}
};
int n, k; void AC(){
UnionFind p = UnionFind();
p.init(n * 3);
int type, x, y, ans = 0;
while(k--){
scanf("%d %d %d", &type, &x, &y);
--x; --y;
if(x < 0 || n <= x || y < 0 || n <= y){
++ans;
continue;
}
if(type == 1){
if(p.same(x, y + n) || p.same(x, y + 2 * n)) ++ans;
else{
p.unite(x, y);
p.unite(x + n, y + n);
p.unite(x + 2 * n, y + 2 * n);
}
}
else{
if(p.same(x, y) || p.same(x, y + 2 * n)) ++ans;
else{
p.unite(x, y + n);
p.unite(x + n, y + 2 * n);
p.unite(x + 2 * n, y);
}
}
}
printf("%d\n", ans);
} int main(){
scanf("%d %d", &n, &k);
AC();
return 0;
}

POJ 2236 Wireless Network

#include <cstdio>
#include <cstring>
const int N = 1111;
const int INF = 0x3f3f3f3f; class UnionFind {
public:
int father[N];
void init(int n){ for(int i = 0; i <= n; ++i) father[i] = i; }
int find(int n){ return n == father[n] ? n : father[n] = find(father[n]); }
bool same(int x, int y){ return find(x) == find(y); }
void unite(int x, int y){
x = find(x); y = find(y);
if(x != y) father[x] = y;
}
};
int n, d;
int x[N], y[N], dis[N][N];
bool repaired[N]; int distance(int x1, int y1, int x2, int y2){ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } int main(){
UnionFind p = UnionFind();
memset(repaired, false, sizeof(repaired));
memset(dis, INF, sizeof(dis));
scanf("%d %d", &n, &d);
d *= d;
for(int i = 1; i <= n; ++i){
scanf("%d %d", &x[i], &y[i]);
for(int j = 1; j < i; ++j)
dis[i][j] = dis[j][i] = distance(x[i], y[i], x[j], y[j]);
}
int xx, yy;
char ch;
p.init(n);
getchar();
while(~scanf("%c", &ch)){
if(ch == 'O'){
scanf("%d", &xx);
repaired[xx] = true;
for(int i = 1; i <= n; ++i)
if(repaired[i] && dis[xx][i] <= d)
p.unite(p.father[i], xx);
}
else{
scanf("%d %d", &xx, &yy);
printf("%s\n", p.same(xx, yy) ? "SUCCESS" : "FAIL");
}
getchar();
}
return 0;
}

POJ 1703 Find them, Catch them

#include <cstdio>
const int N = 233333; class UnionFind {
public:
int father[N];
void init(int n){ for(int i = 0; i <= n; ++i) father[i] = i; }
int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); }
int same(int x, int y){ return find(x) == find(y); }
void unite(int x, int y){
x = find(x); y = find(y);
if(x != y) father[x] = y;
}
};
int T, n, m; int main(){
scanf("%d", &T);
while(T--){
scanf("%d %d", &n, &m);
UnionFind p = UnionFind();
p.init(n<<1);
char ch;
int x, y;
while(m--){
getchar();
scanf("%c %d %d", &ch, &x, &y);
if(ch == 'A'){
if(p.same(x, y)) puts("In the same gang.");
else if(p.same(x, y + n)) puts("In different gangs.");
else puts("Not sure yet.");
}
else{
p.unite(x, y + n);
p.unite(x + n, y);
}
}
}
return 0;
}

AOJ 2170 Marked Ancestor

#include <stack>
#include <queue>
#include <cstdio>
#include <vector>
using namespace std; const int N = 111111; int father[N], _rank[N];
void init(int n){ for(int i = 0; i < n; ++i){ father[i] = i; _rank[i] = 0; } }
int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); }
int same(int x, int y){ return find(x) == find(y); }
void unite(int x, int y){
x = find(x); y = find(y);
if(x != y){
_rank[x] < _rank[y] ? father[x] = y : father[y] = x;
_rank[x] == _rank[y] ? ++_rank[x] : 0;
}
} int n, q, parent[N], ancestor[N];
bool marked[N];
vector<int> son[N];
stack<char> operation;
stack<int> target; void bfs(int index, int iancestor){
queue<int> qi, qa;
qi.push(index); qa.push(iancestor);
while(!qi.empty()){
index = qi.front(); qi.pop();
iancestor = qa.front(); qa.pop();
if(marked[index]) iancestor = index;
ancestor[index] = iancestor;
for(vector<int>::iterator i = son[index].begin(); i != son[index].end(); ++i){
qi.push(*i);
qa.push(iancestor);
}
}
} int main(){
while(scanf("%d %d", &n, &q), (n || q)){
for(int i = 0; i < n; ++i){
son[i].clear();
marked[i] = false;
}
marked[0] = true;
for(int i = 1; i < n; ++i){
scanf("%d", &parent[i]);
--parent[i];
son[parent[i]].push_back(i);
}
while(q--){
char ch;
int x;
scanf("%*c%c %d", &ch, &x);
--x;
if(ch == 'M'){
if(marked[x]) continue;
marked[x] = true;
}
operation.push(ch);
target.push(x);
}
bfs(0, 0);
init(n);
for(int i = 0; i < n; ++i) unite(i, ancestor[i]);
long long ans = 0;
while(!operation.empty()){
char o = operation.top(); operation.pop();
int t = target.top(); target.pop();
if(o == 'Q') ans += ancestor[find(t)] + 1;
else{
int p = ancestor[find(parent[t])];
unite(t, parent[t]);
ancestor[find(t)] = p;
}
}
printf("%lld\n", ans);
}
return 0;
}

AOJ 0189 Convenient Location

#include <bits/stdc++.h>
using namespace std; int path[11][11]; int main(){
int n;
while(cin >> n, n){
memset(path, 0x3f3f3f3f, sizeof(path));
for(int i = 0; i < 10; ++i) path[i][i] = 0;
while(n--){
int a, b, c;
cin >> a >> b >> c;
path[a][b] = min(path[a][b], c);
path[b][a] = min(path[b][a], c);
}
for(int k = 0; k < 10; ++k)
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
path[i][j] = min(path[i][j], path[i][k] + path[k][j]);
int town = -1, total = 0x3f3f3f3f;
for(int i = 0; i < 10; ++i){
int tmp = 0;
bool flag = false;
for(int j = 0; j < 10; ++j){
if(path[i][j] != 0x3f3f3f3f && path[i][j] != 0) flag = true;
tmp += path[i][j] == 0x3f3f3f3f ? 0 : path[i][j];
}
if(flag && tmp < total){
town = i;
total = tmp;
}
}
cout << town << " " << total << endl;
}
}

POJ 2139 Six Degrees of Cowvin Bacon

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 305;
int relat[N][N], arr[N]; int main(){
int n, m;
while(~scanf("%d %d", &n, &m)){
memset(relat, 0x3f3f3f3f, sizeof(relat));
for(int i = 1; i <= n; ++i) relat[i][i] = 0;
while(m--){
int k;
scanf("%d", &k);
for(int i = 0; i < k; ++i){
scanf("%d", &arr[i]);
for(int j = 0; j < i; ++j)
relat[arr[i]][arr[j]] = relat[arr[j]][arr[i]] = 1;
}
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
relat[i][j] = min(relat[i][j], relat[i][k] + relat[k][j]);
memset(arr, 0, sizeof(arr));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
arr[i] += relat[i][j];
int ans = 0x3f3f3f3f;
for(int i = 1; i <= n; ++i)
ans = min(ans, arr[i]);
printf("%d\n", int(ans * 100 / double(n-1)));
}
return 0;
}

POJ 3259 Wormholes

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct Edge {
int from, to, cost;
Edge() {}
Edge(int _from, int _to, int _cost): from(_from), to(_to), cost(_cost) {}
} edge[5555]; int dist[555]; int main(){
int t;
scanf("%d", &t);
while(t--){
int n, m, w;
scanf("%d %d %d", &n, &m, &w);
int index = 0;
while(m--){
int s, e, k;
scanf("%d %d %d", &s, &e, &k);
edge[index].from = s;
edge[index].to = e;
edge[index++].cost = k;
edge[index].from = e;
edge[index].to = s;
edge[index++].cost = k;
}
while(w--){
int s, e, k;
scanf("%d %d %d", &s, &e, &k);
edge[index].from = s;
edge[index].to = e;
edge[index++].cost = -k;
}
bool flag = false;
memset(dist, 0, sizeof(dist));
for(int i = 0; i < n; ++i){
for(int j = 0; j < index; ++j){
Edge e = edge[j];
if(dist[e.to] > dist[e.from] + e.cost){
dist[e.to] = dist[e.from] + e.cost;
if(i == n - 1){
flag = true;
break;
}
}
}
if(flag) break;
}
puts(flag ? "YES" : "NO");
}
return 0;
}

POJ 3268 Silver Cow Party

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std; const int N = 111111;
int n, m, x;
int dist[N], rdist[N];
bool inque[N];
vector<pair<int, int> > g[N], rg[N];
queue<int> q; void spfa(int d[], int src, vector<pair<int, int> > fg[]){
while(!q.empty()) q.pop();
memset(inque, false, sizeof(inque));
d[src] = 0;
q.push(src);
inque[src] = true;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = 0; i < fg[u].size(); ++i){
pair<int, int> p = fg[u][i];
if(d[p.first] > d[u] + p.second){
d[p.first] = d[u] + p.second;
if(!inque[p.first]){
inque[p.first] = true;
q.push(p.first);
}
}
}
inque[u] = false;
}
} int main(){
while(~scanf("%d %d %d", &n, &m, &x)){
for(int i = 0; i < m; ++i){
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
g[a].push_back(make_pair(b, t));
rg[b].push_back(make_pair(a, t));
}
memset(dist, 0x3f, sizeof(dist));
memset(rdist, 0x3f, sizeof(rdist));
spfa(dist, x, g);
spfa(rdist, x, rg);
int ans = 0;
for(int i = 1; i <= n; ++i)
if(dist[i] != 0x3f3f3f3f && rdist[i] != 0x3f3f3f3f)
ans = max(ans, dist[i] + rdist[i]);
printf("%d\n", ans);
}
return 0;
}

AOJ 2249 Road Construction

Solution 1:

#include <bits/stdc++.h>
using namespace std; const int N = 12345; struct edge {
int to, len, cost;
edge(){}
edge(int tt, int ll, int cc): to(tt), len(ll), cost(cc){}
};
vector<edge> g[N];
int dist[N], n, m;
typedef pair<int, int> P; void dijkstra(int src){
priority_queue<P, vector<P>, greater<P> > q;
memset(dist, 0x3f, sizeof(dist));
dist[src] = 0;
q.push(P(0, src));
while(!q.empty()){
P p = q.top(); q.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < g[v].size(); ++i){
edge e = g[v][i];
if(dist[e.to] > dist[v] + e.len){
dist[e.to] = dist[v] + e.len;
q.push(P(dist[e.to], e.to));
}
}
}
} int main(){
while(scanf("%d %d", &n, &m), n || m){
for(int i = 0; i <= n; ++i) g[i].clear();
while(m--){
int a, b, l, c;
scanf("%d %d %d %d", &a, &b, &l, &c);
g[a].push_back(edge(b, l, c));
g[b].push_back(edge(a, l, c));
}
dijkstra(1);
int ans = 0;
for(int i = 2; i <= n; ++i){
int minc = 0x3f3f3f3f;
for(int j = 0; j < g[i].size(); ++j)
if(dist[g[i][j].to] + g[i][j].len == dist[i] && g[i][j].cost < minc)
minc = g[i][j].cost;
ans += minc;
}
printf("%d\n", ans);
}
return 0;
}

Solution 2:

#include <bits/stdc++.h>
using namespace std; const int N = 12345;
struct edge {
int to, len, cost;
edge(){}
edge(int tt, int ll, int cc): to(tt), len(ll), cost(cc) {}
const bool operator > (const edge& o) const {
return len == o.len ? cost > o.cost : len > o.len;
}
};
vector<edge> g[N];
int n, m;
bool visited[N]; int dijkstra(int src){
memset(visited, false, sizeof(visited));
priority_queue<edge, vector<edge>, greater<edge> > q;
q.push(edge(src, 0, 0));
int ans = 0;
while(!q.empty()){
edge e = q.top(); q.pop();
int v = e.to;
if(visited[v]) continue;
visited[v] = true;
ans += e.cost;
for(int i = 0; i < g[v].size(); ++i)
q.push(edge(g[v][i].to, e.len + g[v][i].len, g[v][i].cost));
}
return ans;
} int main(){
while(scanf("%d %d", &n, &m), n || m){
for(int i = 1; i <= n; ++i) g[i].clear();
while(m--){
int a, b, l, c;
scanf("%d %d %d %d", &a, &b, &l, &c);
g[a].push_back(edge(b, l, c));
g[b].push_back(edge(a, l, c));
}
printf("%d\n", dijkstra(1));
}
return 0;
}

AOJ 2200 Mr.Rito Post Office

#include <bits/stdc++.h>
using namespace std; const int N = 233, M = 1111, INF = 111111111;
int n, m, r, dl[N][N], ds[N][N], dp[M][N], t[M]; int main(){
while(scanf("%d %d", &n, &m), n || m){
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
dl[i][j] = ds[i][j] = i == j ? 0 : INF;
while(m--){
int x, y, l; char o;
scanf("%d %d %d %c", &x, &y, &l, &o);
--x; --y;
if(o == 'L') dl[x][y] = dl[y][x] = min(dl[y][x], l);
else ds[x][y] = ds[y][x] = min(ds[y][x], l);
}
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j){
dl[i][j] = min(dl[i][j], dl[i][k] + dl[k][j]);
ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j]);
}
scanf("%d", &r);
for(int i = 0; i < r; ++i){ scanf("%d", &t[i]); --t[i]; }
for(int i = 0; i < r; ++i)
for(int j = 0; j < n; ++j)
dp[i][j] = INF;
for(int i = 0; i < n; ++i)
dp[0][i] = ds[t[0]][i] + dl[i][t[0]];
for(int i = 1; i < r; ++i)
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
dp[i][k] = j == k ?
min(dp[i][j], dp[i-1][j] + dl[t[i-1]][t[i]]) :
min(dp[i][k], dp[i-1][j] + dl[t[i-1]][j] + ds[j][k] + dl[k][t[i]]);
printf("%d\n", *min_element(dp[r-1], dp[r-1] + n));
}
return 0;
}

POJ 1258 Agri-Net

#include <queue>
#include <cstdio>
using namespace std; typedef pair<int, int> pii;
typedef pair<int, pii> P;
priority_queue<P, vector<P>, greater<P> > q;
int n, father[111]; int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); }
void kruskal(){
for(int i = 0; i <= n; ++i) father[i] = i;
int ans = 0;
while(!q.empty()){
P p = q.top(); q.pop();
if(find(p.second.first) == find(p.second.second)) continue;
father[find(p.second.first)] = find(p.second.second);
ans += p.first;
}
printf("%d\n", ans);
}
int main(){
while(~scanf("%d", &n)){
while(!q.empty()) q.pop();
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j){
int x;
scanf("%d", &x);
q.push(P(x, pii(i, j)));
}
kruskal();
}
return 0;
}

POJ 2377 Bad Cowtractors

#include <queue>
#include <cstdio>
using namespace std; typedef pair<int, int> pii;
typedef pair<int, pii> P;
priority_queue<P> q;
int n, m, father[1111]; int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); } void kruskal(){
for(int i = 0; i <= n; ++i) father[i] = i;
int ans = 0, cnt = 0;
while(!q.empty()){
if(cnt == n-1) break;
P p = q.top(); q.pop();
if(find(p.second.first) == find(p.second.second)) continue;
++cnt;
ans += p.first;
father[find(p.second.first)] = find(p.second.second);
}
printf("%d\n", cnt == n-1 ? ans : -1);
} int main(){
while(~scanf("%d %d", &n, &m)){
while(!q.empty()) q.pop();
while(m--){
int x, y, l;
scanf("%d %d %d", &x, &y, &l);
q.push(P(l, pii(x, y)));
}
kruskal();
}
return 0;
}

AOJ 2224 Save your cat

#include <queue>
#include <cmath>
#include <cstdio>
using namespace std; const int N = 11111;
typedef pair<int, int> pii;
typedef pair<double, pair<int, int> > P; pii pile[N];
priority_queue<P> q;
int n, m, father[N]; int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); } void kruskal(){
double ans = 0.0;
while(!q.empty()){
P p = q.top(); q.pop();
if(find(p.second.first) == find(p.second.second)){
ans += p.first;
continue;
}
father[find(p.second.first)] = find(p.second.second);
}
printf("%.3f\n", ans);
} int main(){
while(~scanf("%d %d", &n, &m)){
for(int i = 1; i <= n; ++i) father[i] = i;
while(!q.empty()) q.pop();
for(int i = 1; i <= n; ++i) scanf("%d %d", &pile[i].first, &pile[i].second);
while(m--){
int x, y;
scanf("%d %d", &x, &y);
int dx = pile[x].first - pile[y].first, dy = pile[x].second - pile[y].second;
q.push(P(sqrt(double(dx*dx + dy*dy)), pii(x, y)));
}
kruskal();
}
return 0;
}

POJ 2395 Out of Hay

#include <queue>
#include <cstdio>
using namespace std; typedef pair<int, int> pii;
typedef pair<int, pii> P;
priority_queue<P, vector<P>, greater<P> > q;
int n, m, father[2333]; int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); } void kruskal(){
int ans = -1;
while(!q.empty()){
P p = q.top(); q.pop();
if(find(p.second.first) == find(p.second.second)) continue;
ans = max(ans, p.first);
father[find(p.second.first)] = find(p.second.second);
}
printf("%d\n", ans);
} int main(){
while(~scanf("%d %d", &n, &m)){
while(!q.empty()) q.pop();
for(int i = 1; i <= n; ++i) father[i] = i;
while(m--){
int x, y, l;
scanf("%d %d %d", &x, &y, &l);
q.push(P(l, pii(x, y)));
}
kruskal();
}
return 0;
}

POJ 3255 Roadblocks

#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 5555, INF = 0x3f3f3f3f;
struct edge{ int to, cost; };
vector<edge> g[N];
typedef pair<int, int> P;
priority_queue<P> q;
int n, r, dis[N], dis2[N]; void dijkstra(){
for(int i = 0; i <= n; ++i) dis[i] = dis2[i] = INF;
dis[1] = 0;
q.push(P(0, 1));
while(!q.empty()){
P p = q.top(); q.pop();
if(dis2[p.second] < p.first) continue;
int v = p.second;
for(int i = 0; i < g[v].size(); ++i){
edge e = g[v][i];
int d = p.first + e.cost;
if(dis[e.to] > d){
swap(dis[e.to], d);
q.push(P(dis[e.to], e.to));
}
if(dis2[e.to] > d && dis[e.to] < d){
dis2[e.to] = d;
q.push(P(dis2[e.to], e.to));
}
}
}
for(int i = 1; i <= n; ++i) printf("%d %d\n", dis[i], dis2[i]);
} int main(){
while(~scanf("%d %d", &n, &r)){
for(int i = 0; i <= n; ++i) g[i].clear();
while(!q.empty()) q.pop();
while(r--){
int x, y, l;
scanf("%d %d %d", &x, &y, &l);
g[x].push_back(edge{y, l});
g[y].push_back(edge{x, l});
}
dijkstra();
}
return 0;
}

POJ 3723 Conscription

#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 11111;
int n, m, r, father[N<<1];
typedef pair<int, int> pii;
typedef pair<int, pii> P;
priority_queue<P> q; int find(int x){ return x == father[x] ? x : father[x] = find(father[x]); } void kruskal(){
int ans = 0;
while(!q.empty()){
P p = q.top(); q.pop();
if(find(p.second.first) == find(p.second.second)) continue;
ans += p.first;
father[find(p.second.first)] = find(p.second.second);
}
printf("%d\n", 10000 * (n + m) - ans);
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d %d", &n, &m, &r);
while(!q.empty()) q.pop();
for(int i = 0, limit = n + m; i < limit; ++i) father[i] = i;
while(r--){
int x, y, l;
scanf("%d %d %d", &x, &y, &l);
q.push(P(l, pii(x, y + n)));
}
kruskal();
}
return 0;
}

POJ 3169 Layout

#include <cstdio>
#include <vector>
#include <cstring>
using namespace std; const int N = 11111, INF = 0x3f3f3f3f;
int n, ml, md, d[N];
struct edge { int from, to, cost; };
vector<edge> g; void bellmen_ford(){
memset(d, INF, sizeof(d));
d[1] = 0;
bool flag = false;
for(int i = 0; i < n; ++i){
for(vector<edge>::iterator it = g.begin(); it != g.end(); ++it){
edge e = *it;
if(d[e.to] > d[e.from] + e.cost){
d[e.to] = d[e.from] + e.cost;
if(i == n-1) flag = true;
}
}
}
if(flag) puts("-1");
else if(d[n] == INF) puts("-2");
else printf("%d\n", d[n]);
} int main(){
while(~scanf("%d %d %d", &n, &ml, &md)){
g.clear();
for(int i = n; i; --i) g.push_back(edge{i, i-1, 0});
while(ml--){
int al, bl, ll;
scanf("%d %d %d", &al, &bl, &ll);
g.push_back(edge{al, bl, ll});
}
while(md--){
int ad, bd, ld;
scanf("%d %d %d", &ad, &bd, &ld);
g.push_back(edge{bd, ad, -ld});
}
bellmen_ford();
}
return 0;
}

UVa 10006 Carmichael Numbers

#include <bits/stdc++.h>
using namespace std; const int N = 66666;
bool isprime[N];
int n; void init(){
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
for(int i = 2, limit = sqrt(N); i <= limit; ++i){
if(isprime[i]){
for(int j = i; i * j < N; ++j)
isprime[i * j] = false;
}
}
} typedef long long ll;
bool pow_mod(ll x, ll n, ll mod){
ll ans = 1, t = x;
while(n){
if(n & 1) ans = ans * x % mod;
x = x * x % mod;
n >>= 1;
}
return ans != t;
} int main(){
init();
while(cin >> n, n){
if(isprime[n]) printf("%d is normal.\n", n);
else{
bool flag = true;
for(int i = 2; i < n; ++i) if(pow_mod(i, n, n)){ flag = false; break; }
if(flag) printf("The number %d is a Carmichael number.\n", n);
else printf("%d is normal.\n", n);
} }
return 0;
}

GCJ 2008 Round 1A A Minimum Scalar Product

#include <iostream>
#include <algorithm>
using namespace std; const int N = 1111;
typedef long long ll; ll t, n, a[N], b[N]; int main(){
cin >> t;
for(int x = 1; x <= t; ++x){
cout << "Case #" << x << ": ";
cin >> n;
for(int i = 0; i < n; ++i) cin >> a[i];
for(int i = 0; i < n; ++i) cin >> b[i];
sort(a, a + n); sort(b, b + n);
ll ans = 0;
for(int i = 0; i < n; ++i) ans += a[i] * b[n-i-1];
cout << ans << endl;
}
return 0;
}

GCJ 2009 Round 2 A Crazy Rows

#include <iostream>
#include <algorithm>
using namespace std; const int N = 44;
int a[N], t, n; int main(){
cin >> t;
for(int x = 1; x <= t; ++x){
cout << "Case #" << x << ": ";
int ans = 0;
cin >> n;
for(int i = 0; i < n; ++i){
a[i] = -1;
for(int j = 0; j < n; ++j){
char k;
cin >> k;
if(k-'0') a[i] = j;
}
}
for(int i = 0; i < n; ++i){
int pos = -1;
for(int j = i; j < n; ++j) if(a[j] <= i){ pos = j; break; }
for(int j = pos; j > i; --j){
swap(a[j], a[j-1]);
++ans;
}
}
cout << ans << endl;
}
}

GCJ 2009 Round 1C C Bribe the *ers


GCJ 2008 APAC Semifinal C Millionaire


AOJ 0005 GCD and LCM

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
int main(){
ll a, b;
while(cin >> a >> b) cout << gcd(a, b) << " " << lcm(a, b) << endl;
return 0;
}

POJ 2429 GCD & LCM Inverse

#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll; ll mul_mod(ll a, ll b, ll mod){
ll ans = 0, exp = a;
while(exp >= mod) exp -= mod;
while(b){
if(b & 1){
ans += exp;
if(ans >= mod) ans -= mod;
}
exp <<= 1;
if(exp >= mod) exp -= mod;
b >>= 1;
}
return ans;
}
ll pow_mod(ll x, ll n, ll mod){
ll ans = 1, exp = x;
while(exp >= mod) exp -= mod;
while(n){
if(n & 1) ans = mul_mod(ans, exp, mod);
exp = mul_mod(exp, exp, mod);
n >>= 1;
}
return ans;
} const int MP = 233333;
vector<int> primes;
vector<bool> isprime;
void init_primes(){
isprime = vector<bool>(MP, true);
isprime[0] = isprime[1] = false;
for(int i = 2; i < MP; ++i){
if(isprime[i]) primes.push_back(i);
for(int j = 0; j < primes.size() && i * primes[j] < MP; ++j){
isprime[i * primes[j]] = false;
if(i % primes[j] == 0) break;
}
}
}
bool miller_rabin(ll n){
if(n < 2) return false;
if(n == 2) return true;
if(!(n & 1)) return false;
ll p = n - 1;
int k = 0;
while(!(p & 1)){
++k;
p >>= 1;
}
for(int i = 0; i < 20; ++i){
ll a = rand() % (n - 1) + 1;
ll x = pow_mod(a, p, n);
if(x == 1) continue;
bool flag = false;
for(int j = 0; j < k; ++j){
if(x == n - 1){
flag = true;
break;
}
x = mul_mod(x, x, n);
}
if(flag) continue;
return false;
}
return true;
}
bool is_prime(ll n){
if(n < MP) return isprime[n];
else return miller_rabin(n);
} ll pollard_rho(ll n, int a){
ll x = 2, y = 2, d = 1;
while(d == 1){
x = mul_mod(x, x, n) + a;
y = mul_mod(y, y, n) + a;
y = mul_mod(y, y, n) + a;
d = __gcd((x >= y ? x - y : y - x), n);
}
if(d == n) return pollard_rho(n, a + 1);
return d;
}
void factorize(ll n, map<ll, int>& factors){
if(is_prime(n)) ++factors[n];
else{
for(int i = 0; i < primes.size(); ++i)
while(!(n % primes[i])){
++factors[primes[i]];
n /= primes[i];
}
if(n != 1){
if(is_prime(n)) ++factors[n];
else{
ll d = pollard_rho(n, 1);
factorize(d, factors);
factorize(n / d, factors);
}
}
}
} int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll a, b;
init_primes();
while(cin >> a >> b){
ll c = b / a;
map<ll, int> factors;
factorize(c, factors);
vector<ll> p_factors;
for(map<ll, int>::iterator it = factors.begin(); it != factors.end(); ++it){
ll tmp = 1;
while(it->second--) tmp *= it->first;
p_factors.push_back(tmp);
}
ll x = 1, y = c, sum = 1e18;
for(int i = 0; i < (1 << p_factors.size()); ++i){
ll tx = 1;
for(int j = 0; j < p_factors.size(); ++j)
if(i & (1 << j))
tx *= p_factors[j];
ll ty = c / tx;
if(tx < ty && tx + ty < sum){
x = tx;
y = ty;
sum = x + y;
}
}
cout << x * a << " " << y * a << endl;
}
return 0;
}

POJ 1930 Dead Fraction


AOJ 0009 Prime Number

#include <bits/stdc++.h>
using namespace std; const int N = 1111111;
int primes[N], total;
bool isprime[N]; void init_primes(){
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
total = 0;
for(int i = 2; i < N; ++i){
if(isprime[i]) primes[total++] = i;
for(int j = 0; j < total && i * primes[j] < N; ++j){
isprime[i * primes[j]] = false;
if(i % primes[j] == 0) break;
}
}
} int main(){
init_primes();
int n;
while(cin >> n) cout << upper_bound(primes, primes + total, n) - primes << endl;
return 0;
}

POJ 3126 Prime Path

#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std; const int N = 11111;
int primes[N], total;
bool isprime[N]; void init_primes(){
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
total = 0;
for(int i = 2; i < N; ++i){
if(isprime[i]) primes[total++] = i;
for(int j = 0; j < total && i * primes[j] < N; ++j){
isprime[i * primes[j]] = false;
if(i % primes[j] == 0) break;
}
}
} int main(){
init_primes();
int t;
cin >> t;
while(t--){
int a, b;
cin >> a >> b;
queue<int> q;
q.push(a);
vector<int> step = vector<int>(N, -1);
step[a] = 0;
while(!q.empty()){
int num = q.front(); q.pop();
if(num == b){
cout << step[num] << endl;
break;
}
for(int i = 0; i < 4; ++i){
int digit[] = {num/1000, (num%1000)/100, (num%100)/10, num%10};
for(int j = 0; j < 10; ++j){
digit[i] = j;
int newn = digit[0]*1000 + digit[1]*100 + digit[2]*10 + digit[3];
if(newn < 1000 || !isprime[newn] || ~step[newn]) continue;
q.push(newn);
step[newn] = step[num] + 1;
}
}
}
}
return 0;
}

POJ 3421 X-factor Chains

#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
typedef long long ll;
vector<int> factorize(int n){
vector<int> ans;
for(int i = 2; i * i <= n; ++i){
int tmp = 0;
while(n % i == 0){
++tmp;
n /= i;
}
ans.push_back(tmp);
}
if(n != 1) ans.push_back(1);
return ans;
}
ll factorial(int n){
ll ans = 1;
while(n) ans *= n--;
return ans;
} int main(){
int n;
while(cin >> n){
vector<int> p_factors = factorize(n);
int length = accumulate(p_factors.begin(), p_factors.end(), 0);
ll num = factorial(length);
for(vector<int>::iterator it = p_factors.begin(); it != p_factors.end(); ++it)
num /= factorial(*it);
cout << length << " " << num << endl;
}
return 0;
}

POJ 3292 Semi-prime H-numbers


POJ 3641 Pseudoprime numbers

#include <iostream>
using namespace std; typedef long long ll;
ll mul_mod(ll a, ll b, ll mod){
ll ans = 0, exp = a % mod;
while(b){
if(b & 1){
ans += exp;
while(ans >= mod) ans -= mod;
}
exp <<= 1;
while(exp >= mod) exp -= mod;
b >>= 1;
}
return ans;
}
ll pow_mod(ll x, ll n, ll mod){
ll ans = 1, exp = x % mod;
while(n){
if(n & 1) ans = mul_mod(ans, exp, mod);
exp = mul_mod(exp, exp, mod);
n >>= 1;
}
return ans;
}
bool isprime(ll n){
for(int i = 2; i * i <= n; ++i)
if(n % i == 0)
return false;
return true;
}
int main(){
ll p, a;
while(cin >> p >> a, p || a){
if(!isprime(p) && pow_mod(a, p, p) == a) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}

POJ 1995 Raising Modulo Numbers

#include <numeric>
#include <iostream>
using namespace std; typedef long long ll;
ll mul_mod(ll a, ll b, ll mod){
ll ans = 0, exp = a % mod;
while(b){
if(b & 1){
ans += exp;
while(ans >= mod) ans -= mod;
}
exp <<= 1;
while(exp >= mod) exp -= mod;
b >>= 1;
}
return ans;
}
ll pow_mod(ll x, ll n, ll mod){
ll ans = 1, exp = x % mod;
while(n){
if(n & 1) ans = mul_mod(ans, exp, mod);
exp = mul_mod(exp, exp, mod);
n >>= 1;
}
return ans;
}
const int N = 50000;
int ans[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
int mod, h;
cin >> mod >> h;
for(int i = 0; i < h; ++i){
int a, b;
cin >> a >> b;
ans[i] = pow_mod(a, b, mod);
}
cout << accumulate(ans, ans + h, 0) % mod << endl;
}
return 0;
}

POJ 1064 Cable master

#include <cmath>
#include <cstdio>
using namespace std; const int N = 12345;
int n, k;
double len[N]; int main(){
while(~scanf("%d %d", &n, &k)){
for(int i = 0; i < n; ++i) scanf("%lf", &len[i]);
double l = 0, r = 1<<30;
for(int i = 0; i < 100; ++i){
double mid = (l + r) / 2.0;
int t = 0;
for(int j = 0; j < n; ++j) t += int(len[j] / mid);
if(t >= k) l = mid;
else r = mid;
}
printf("%.2f\n", floor(r * 100) / 100);
}
return 0;
}

POJ 2456 Aggressive cows

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 100002;
int n, c, x[N]; bool check(int dis){
int k = 0;
for(int i = 1; i < c; ++i){
int pos = k + 1;
while(pos < n && x[pos] - x[k] < dis) ++pos;
if(pos == n) return false;
k = pos;
}
return true;
} int main(){
while(~scanf("%d %d", &n, &c)){
for(int i = 0; i < n; ++i) scanf("%d", &x[i]);
sort(x, x + n);
int l = 0, r = 0x3f3f3f3f;
for(int i = 0; i < 100; ++i){
int mid = (l + r) >> 1;
if(check(mid)) l = mid;
else r = mid;
}
printf("%d\n", l);
}
return 0;
}

POJ 3061 Subsequence

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 100002;
int n, s, arr[N]; int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &s);
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
int ans = 0x3f3f3f3f, l = 0, r = 0, sum = 0;
while(true){
while(r < n && sum < s) sum += arr[r++];
if(sum < s) break;
ans = min(ans, r - l);
sum -= arr[l++];
}
if(ans > n) ans = 0;
printf("%d\n", ans);
}
return 0;
}

POJ 3320 Jessica's Reading Problem

#include <set>
#include <map>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 1000002;
int n, arr[N];
set<int> s;
map<int, int> m; int main(){
while(~scanf("%d", &n)){
s.clear(); m.clear();
for(int i = 0; i < n; ++i){
scanf("%d", &arr[i]);
s.insert(arr[i]);
}
int k = s.size(), l = 0, r = 0, cnt = 0, ans = n;
while(true){
while(r < n && cnt < k) if(m[arr[r++]]++ == 0) ++cnt;
if(cnt < k) break;
ans = min(ans, r - l);
if(--m[arr[l++]] == 0) --cnt;
}
printf("%d\n", ans);
}
return 0;
}

POJ 3276 Face The Right Way

#include <cstdio>
#include <cstring> const int N = 5555;
int n, dir[N], flip[N]; int main(){
while(~scanf("%d", &n)){
for(int i = 0; i < n; ++i){
char ch;
scanf(" %c", &ch);
if(ch == 'F') dir[i] = 0;
else dir[i] = 1;
}
int ansop = n, ansk = 1;
for(int k = 1; k <= n; ++k){
memset(flip, 0, sizeof(flip));
int op = 0, sum = 0;
for(int i = 0; i + k - 1 < n; ++i){
if((dir[i] + sum) & 1){
++op;
flip[i] = 1;
}
sum += flip[i];
if(i - k + 1 >= 0) sum -= flip[i-k+1];
}
for(int i = n-k+1; i < n; ++i){
if((dir[i] + sum) & 1){
op = -1;
break;
}
if(i - k + 1 >= 0) sum -= flip[i-k+1];
}
if(op >= 0 && ansop > op){
ansop = op;
ansk = k;
}
}
printf("%d %d\n", ansk, ansop);
}
}

POJ 3279 Fliptile

#include <cstdio>
#include <cstring> const int N = 20, INF = 0x3f3f3f3f;
const int dx[] = {0, 0, 0, -1}, dy[] = {1, -1, 0, 0};
int n, m, ans[N][N], flip[N][N], origin[N][N]; bool check(int x, int y){
int cnt = origin[x][y];
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m)
cnt += flip[nx][ny];
}
return cnt & 1;
} int solve(){
for(int i = 1; i < n; ++i)
for(int j = 0; j < m; ++j)
if(check(i - 1, j))
flip[i][j] = 1;
for(int j = 0; j < m; ++j)
if(check(n - 1, j))
return 0;
int cnt = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
cnt += flip[i][j];
return cnt;
} int main(){
while(~scanf("%d %d", &n, &m)){
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
scanf("%d", &origin[i][j]);
int step = INF;
for(int i = 0; i < (1 << m); ++i){
memset(flip, 0, sizeof(flip));
for(int j = 0; j < m; ++j)
flip[0][j] = (i >> j) & 1;
int cnt = solve();
if(cnt != 0 && cnt < step){
step = cnt;
memcpy(ans, flip, sizeof(flip));
}
}
if(step == INF) puts("IMPOSSIBLE");
else{
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
printf("%d%c", ans[i][j], " \n"[j == m - 1]);
}
}
return 0;
}

POJ 3684 Physics Experiment

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; const double g = 10.0;
const int MAX_N = 111;
int N, H, R, T;
double ans[MAX_N]; double calc(int T){
if(T < 0) return H;
double t = sqrt(2 * H / g), d;
int k = (int)(T / t);
if(k & 1)
d = k * t + t - T;
else
d = T - k * t;
return H - g * d * d / 2;
} void solve(){
for(int i = 0; i < N; ++i) ans[i] = calc(T - i);
sort(ans, ans + N);
for(int i = 0; i < N; ++i)
printf("%.2f%c", ans[i] + 2 * R * i / 100.0, i == N - 1 ? '\n' : ' ');
} int main(){
int x;
scanf("%d", &x);
while(x--){
scanf("%d %d %d %d", &N, &H, &R, &T);
solve();
}
return 0;
}

POJ 2785 4 Values whose Sum is 0

#include <iostream>
#include <algorithm>
using namespace std; const int N = 4444;
int n, a[N], b[N], c[N], d[N], e[N*N], f[N*N]; int main(){
ios::sync_with_stdio(false); cin.tie(0);
while(cin >> n){
for(int i = 0; i < n; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i];
int it = 0;
for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) e[it] = a[i] + b[j], f[it++] = c[i] + d[j];
sort(f, f + it);
int ans = 0;
for(int i = 0; i < it; ++i)
ans += upper_bound(f, f + it, -e[i]) - lower_bound(f, f + it, -e[i]);
cout << ans << endl;
}
return 0;
}

POJ 3258 River Hopscotch

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 55555;
int l, n, m, rock[N]; bool check(int x){
int last = 0, rm = 0;
for(int i = 0; i < n; ++i){
int cur = last + 1;
while(cur < n && rock[cur] - rock[last] < x) ++rm, ++cur;
last = cur;
}
if(rm > m) return false;
return true;
} int main(){
while(~scanf("%d %d %d", &l, &n, &m)){
for(int i = 0; i < n; ++i) scanf("%d", &rock[i]);
rock[n++] = 0; rock[n++] = l;
sort(rock, rock + n);
int lb = 0, rb = l + 1;
while(rb - lb > 1){
int mid = (lb + rb) >> 1;
if(check(mid)) lb = mid;
else rb = mid;
}
printf("%d\n", lb);
}
return 0;
}

POJ 3273 Monthly Expense

#include <cstdio>
#include <numeric>
#include <algorithm>
using namespace std; const int N = 123456;
int n, m, arr[N]; bool check(int x){
int seg = 1, sum = 0;
for(int i = 0; i < n; ++i){
sum += arr[i];
if(sum > x){
sum = arr[i];
++seg;
}
}
if(seg > m) return true;
return false;
} int main(){
while(~scanf("%d %d", &n, &m)){
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
int lb = *max_element(arr, arr + n), rb = accumulate(arr, arr + n, 0);
while(rb > lb){
int mid = (lb + rb) >> 1;
if(check(mid)) lb = mid + 1;
else rb = mid;
}
printf("%d\n", lb);
}
return 0;
}

POJ 3104 Drying

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 111111;
int n, k, arr[N]; int main(){
while(~scanf("%d", &n)){
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
scanf("%d", &k);
if(k == 1){
printf("%d\n", *max_element(arr, arr + n));
continue;
}
int lb = 0, rb = *max_element(arr, arr + n);
while(rb > lb){
int mid = (lb + rb) >> 1;
long long tot = 0;
for(int i = 0; i < n; ++i){
int t = arr[i] - mid;
if(t <= 0) continue;
tot += ceil(double(t) / (k - 1));
}
if(tot > mid) lb = mid + 1;
else rb = mid;
}
printf("%d\n", rb);
}
return 0;
}

POJ 3045 Cow Acrobats

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 55555;
struct Cow{
int w, s;
const bool operator < (const Cow& o) const {
return w + s > o.w + o.s;
}
}cow[N]; int main(){
int n;
while(~scanf("%d", &n)){
int tot = 0;
for(int i = 0; i < n; ++i){
scanf("%d %d", &cow[i].w, &cow[i].s);
tot += cow[i].w;
}
sort(cow, cow + n);
int risk = -0x3f3f3f3f;
for(int i = 0; i < n; ++i){
tot -= cow[i].w;
risk = max(risk, tot - cow[i].s);
}
printf("%d\n", risk);
}
return 0;
}

POJ 2976 Dropping tests

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 1111;
const double eps = 1e-6;
int n, k;
double x;
struct test {
int a, b;
const bool operator < (const test& o) const {
return a - b * x > o.a - o.b * x;
}
} arr[N]; bool check(){
sort(arr, arr + n);
double atot = 0, btot = 0;
for(int i = 0; i < n - k; ++i) atot += arr[i].a, btot += arr[i].b;
return atot / btot > x;
} int main(){
while(scanf("%d %d", &n, &k), n || k){
for(int i = 0; i < n; ++i) scanf("%d", &arr[i].a);
for(int i = 0; i < n; ++i) scanf("%d", &arr[i].b);
double lb = 0, rb = 1;
while(fabs(rb - lb) > eps){
x = (lb + rb) / 2.0;
if(check()) lb = x;
else rb = x;
}
printf("%.0f\n", lb * 100);
}
return 0;
}

POJ 3111 K Best

#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; const int N = 111111;
const double eps = 1e-6;
int n, k;
double x;
struct jewels {
int w, v, i;
const bool operator < (const jewels& o) const {
return v - x * w > o.v - x * o.w;
}
} arr[N]; bool check(){
sort(arr, arr + n);
double vtot = 0, wtot = 0;
for(int i = 0; i < k; ++i) vtot += arr[i].v, wtot += arr[i].w;
return vtot / wtot > x;
} int main(){
while(~scanf("%d %d", &n, &k)){
for(int i = 0; i < n; ++i) scanf("%d %d", &arr[i].v, &arr[i].w), arr[i].i = i + 1;
double lb = 0, rb = 1e15;
while(fabs(rb - lb) > eps){
x = (lb + rb) / 2.0;
if(check()) lb = x;
else rb = x;
}
vector<int> ans;
for(int i = 0; i < k; ++i) ans.push_back(arr[i].i);
sort(ans.begin(), ans.end());
for(int i= 0; i < ans.size(); ++i) printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
}
return 0;
}

POJ 3579 Median

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 111111;
int n, arr[N], c_n_2; bool check(int x){
int cnt = 0;
for(int i = 0; i < n; ++i) cnt += arr + n - lower_bound(arr+i+1, arr+n, x + arr[i]);
return cnt <= (c_n_2 >> 1);
} int main(){
while(~scanf("%d", &n)){
c_n_2 = n * (n - 1) >> 1;
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
sort(arr, arr + n);
int lb = 0, rb = 0x3f3f3f3f, ans = 0;
while(rb - lb > 1){
int mid = (lb + rb) >> 1;
if(check(mid)) rb = mid;
else lb = ans = mid;
}
printf("%d\n", ans);
}
return 0;
}

POJ 3685 Matrix

#include <iostream>
using namespace std; typedef long long ll; ll x, n, m; ll calc(ll x, ll y){ return x * x + 100000 * x + y * y - 100000 * y + x * y; } bool check(ll x){
ll cnt = 0;
for(int j = 1; j <= n; ++j){
int lb = 0, rb = n + 1;
while(rb - lb > 1){
int mid = (lb + rb) >> 1;
if(calc(mid, j) < x) lb = mid;
else rb = mid;
}
cnt += lb;
}
return cnt < m;
} int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> x;
while(x--){
cin >> n >> m;
ll lb = -100000 * n, rb = n * n + 100000 * n + n * n + n * n;
while(rb - lb > 1){
ll mid = (lb + rb) >> 1;
if(check(mid)) lb = mid;
else rb = mid;
}
cout << lb << endl;
}
return 0;
}

POJ 2010 Moo University - Financial Aid

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 111111;
int n, c, f;
struct Cow{
int score, aid, rank;
}cow_a[N], cow_s[N]; bool cmp_s(const Cow& a, const Cow& b){ return a.score < b.score; }
bool cmp_a(const Cow& a, const Cow& b){ return a.aid < b.aid; } int main(){
ios::sync_with_stdio(false);
cin.tie(0); while(cin >> n >> c >> f){
for(int i = 0; i < c; ++i) cin >> cow_s[i].score >> cow_s[i].aid;
sort(cow_s, cow_s + c, cmp_s);
for(int i = 0; i < c; ++i) cow_s[i].rank = i;
memcpy(cow_a, cow_s, sizeof(Cow) * c);
sort(cow_a, cow_a + c, cmp_a); int l = 0, r = c, ans = -1, half = n >> 1; while(r - l > 1){
int mid = (l + r) >> 1; int left = 0, right = 0, tot = cow_s[mid].aid;
for(int i = 0; i < c; ++i){
if(cow_a[i].rank < mid && tot + cow_a[i].aid <= f && left < half){
++left;
tot += cow_a[i].aid;
}
else if(cow_a[i].rank > mid && tot + cow_a[i].aid <= f && right < half){
++right;
tot += cow_a[i].aid;
}
} if(left < half && right < half){ ans = -1; break; }
else if(left < half) l = mid;
else if(right < half) r = mid;
else{
ans = cow_s[mid].score;
l = mid;
}
} cout << ans << endl;
} return 0;
}

POJ 3662 Telephone Lines

#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef pair<int, int> P;
const int N = 11111;
vector<P> g[N];
int n, p, k, d[N]; int dijkstra(int s, int x){
priority_queue<P, vector<P>, greater<P> > q;
q.push(P(s, 0));
memset(d, 0x3f3f3f3f, sizeof(d));
d[s] = 0;
while(!q.empty()){
P p = q.top(); q.pop();
int v = p.first;
if(d[v] < p.second) continue;
for(int i = 0; i < g[v].size(); ++i){
P t = g[v][i];
int k = d[v] + (t.second >= x ? 1 : 0);
if(d[t.first] > k){
d[t.first] = k;
q.push(P(t.first, k));
}
}
}
return d[n-1];
} int main(){
ios::sync_with_stdio(false);
cin.tie(0); while(cin >> n >> p >> k){
for(int i = 0; i < p; ++i){
int f, t, v;
cin >> f >> t >> v;
--f; --t;
g[f].push_back(P(t, v));
g[t].push_back(P(f, v));
} int l = 0, r = 0x3f3f3f3f;
while(r - l > 1){
int mid = (l + r) >> 1;
if(dijkstra(0, mid) > k) l = mid;
else r = mid;
}
cout << (l > 1000000 ? -1 : l) << endl;
}
return 0;
}

POJ 1759 Garland

#include <iomanip>
#include <cstring>
#include <iostream>
using namespace std; int n; double a, h[1111], ans; bool check(double x){
h[1] = x;
for(int i = 2; i < n; ++i){
h[i] = 2 * h[i - 1] + 2 - h[i - 2];
if(h[i] < 0) return false;
}
ans = h[n - 1];
return true;
} int main(){
while(cin >> n >> a){
memset(h, 0, sizeof(h));
h[0] = a;
double l = 0, r = 0x3f3f3f3f;
for(int i = 0; i < 1000; ++i){
double mid = (l + r) / 2.0;
if(check(mid)) r = mid;
else l = mid;
}
cout << fixed << setprecision(2) << ans << endl;
}
return 0;
}

POJ 2739 Sum of Consecutive Prime Numbers

#include <cstring>
#include <iostream>
using namespace std; const int N = 11111;
int n, prime[N], tot; bool isprime[N]; void init(){
tot = 0;
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
for(int i = 2; i < N; ++i){
if(isprime[i]) prime[tot++] = i;
for(int j = 0; j < tot && i * prime[j] < N; ++j){
isprime[i * prime[j]] = false;
if(i % prime[j] == 0) break;
}
}
} int main(){
init();
while(cin >> n, n){
int l = 0, r = 0, sum = 0, ans = 0;
while(true){
while(sum < n && r < tot) sum += prime[r++];
if(sum < n) break;
if(sum == n) ++ans;
sum -= prime[l++];
}
cout << ans << endl;
}
return 0;
}

POJ 2100 Graveyard Design

#include <vector>
#include <cstdio>
using namespace std; typedef long long ll; vector<pair<ll, ll> > vt; int main(){
ll n;
while(~scanf("%I64d", &n)){
vt.clear();
ll l = 1, r = 1, sum = 0;
while(true){
while(sum < n){
sum += r * r;
++r;
}
if((r-1) * (r-1) > n) break;
if(sum == n) vt.push_back(make_pair(l, r));
sum -= l * l;
++l;
}
printf("%d\n", vt.size());
for(int i = 0; i < vt.size(); ++i){
pair<ll, ll> p = vt[i];
printf("%I64d", p.second - p.first);
while(p.first < p.second) printf(" %I64d", p.first++);
puts("");
}
}
return 0;
}

POJ 2566 Bound Found

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int N = 101010;
const int INF = 0x3f3f3f3f; int n, t, k, arr[N], ans, ansl, ansr;
struct book{
int presum, index;
book(){};
book(int pp, int ii): presum(pp), index(ii) {};
const bool operator < (const book& o) const {
return presum < o.presum;
}
} pre[N]; int main(){
while(scanf("%d %d", &n, &t)){
pre[0] = book(0, 0);
if(!n && !t) break;
for(int i = 1; i <= n; ++i){
scanf("%d", &arr[i]);
pre[i] = book(pre[i - 1].presum + arr[i], i);
}
sort(pre, pre + n + 1);
while(t--){
scanf("%d", &k);
int l = 0, r = 1, sum = INF, MIN = INF; ans = INF;
while(r <= n){
sum = pre[r].presum - pre[l].presum;
if(abs(sum - k) < MIN){
MIN = abs(sum - k);
ans = sum;
ansl = min(pre[l].index, pre[r].index);
ansr = max(pre[l].index, pre[r].index);
}
if(sum <= k) ++r;
else ++l;
if(l == r) ++r;
}
printf("%d %d %d\n", ans, ansl + 1, ansr);
}
}
return 0;
}

POJ 3185 The Water Bowls

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 22; int dir[N], flip[N]; int calc(int start){
memset(flip, 0, sizeof(flip));
dir[0] = start;
int ans = 0, sum = 0;
for(int i = 0; i < 20; ++i){
if((dir[i] + sum) & 1){
++ans;
flip[i] = 1;
}
sum += flip[i];
if(i - 2 >= 0) sum -= flip[i - 2];
}
if((dir[20] + sum) & 1) return 0x3f3f3f3f;
return ans;
} int main(){
for(int i = 1; i <= 20; ++i) scanf("%d", &dir[i]);
printf("%d\n", min(calc(0), calc(1)));
return 0;
}

POJ 1222 Extended Lights Out

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int n = 5, m = 6, cx[] = {-1, 0, 0, 0, 1}, cy[] = {0, -1, 0, 1, 0}; int t, tile[11][11], opt[11][11], flip[11][11]; int get(int x, int y){
int c = tile[x][y];
for(int i = 0; i < 5; ++i){
int xx = x + cx[i], yy = y + cy[i];
if(0 <= xx && xx < n && 0 <= yy && yy < m)
c += flip[xx][yy];
}
return c & 1;
} int calc(){
for(int i = 1; i < n; ++i)
for(int j = 0; j < m; ++j)
if(get(i - 1, j))
flip[i][j] = 1;
for(int j = 0; j < m; ++j)
if(get(n - 1, j))
return -1;
int res = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
res += flip[i][j];
return res;
} void solve(){
int res = -1;
for(int i = 0; i < 1 << m; ++i){
memset(flip, 0, sizeof(flip));
for(int j = 0; j < m; ++j)
flip[0][m-j-1] = i >> j & 1;
int num = calc();
if(num >= 0 && (res < 0 || res > num)){
res = num;
memcpy(opt, flip, sizeof(flip));
}
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
printf("%d%c", opt[i][j], j == m - 1 ? '\n' : ' ');
} int main(){
scanf("%d", &t);
for(int x = 1; x <= t; ++x){
printf("PUZZLE #%d\n", x);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
scanf("%d", &tile[i][j]);
solve();
}
return 0;
}

POJ 2674 Linear world

#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; const int N = 33333;
const double eps = 1e-6;
struct inhabitant{
double pos;
string name;
const bool operator < (const inhabitant& o) const {
return fabs(pos) < fabs(o.pos);
}
} arr[N];
int n;
double l, v; int main(){
while(scanf("%d", &n), n){
scanf("%lf %lf", &l, &v);
for(int i = 0; i < n; ++i){
char dir, name[333];
scanf(" %c %lf %s", &dir, &arr[i].pos, name);
arr[i].name = string(name);
if(dir == 'n' || dir == 'N') arr[i].pos = -arr[i].pos;
}
sort(arr, arr + n);
double maxd = 0.0; int id = 0; bool right = true;
for(int i = 0; i < n; ++i){
double nd = (arr[i].pos < 0.0 ? 0 : l) - arr[i].pos;
if(nd > maxd){
maxd = nd;
id = i;
right = arr[i].pos > 0.0;
}
}
int cnt = 0;
if(right){
for(int i = id; i < n; ++i) cnt += arr[i].pos < 0.0;
id += cnt;
}
else{
for(int i = id; i >= 0; --i) cnt += arr[i].pos > 0.0;
id -= cnt;
}
printf("%13.2f %s\n", (int)(maxd / v * 100) / 100.0, arr[id].name.c_str());
}
return 0;
}

POJ 3977 Subset

#include <map>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long ll;
const int N = 44; ll _abs(const ll& x){ return x >= 0 ? x : -x; } int n; ll arr[N]; int main(){
while(scanf("%d", &n), n){
for(int i = 0; i < n; ++i) scanf("%lld", &arr[i]);
map<ll, int> ma;
pair<ll, int> ans(_abs(arr[0]), 1);
for(int i = 0; i < (1 << (n >> 1)); ++i){
ll sum = 0; int num = 0;
for(int j = 0; j < (n >> 1); ++j)
if((i >> j) & 1){
sum += arr[j];
++num;
}
if(num == 0) continue;
ans = min(ans, make_pair(_abs(sum), num));
map<ll, int>::iterator it = ma.find(sum);
if(it == ma.end()) ma[sum] = num;
else it->second = min(it->second, num);
}
for(int i = 0; i < (1 << (n - (n >> 1))); ++i){
ll sum = 0; int num = 0;
for(int j = 0; j < (n - (n >> 1)); ++j)
if((i >> j) & 1){
sum += arr[j + (n >> 1)];
++num;
}
if(num == 0) continue;
ans = min(ans, make_pair(_abs(sum), num));
map<ll, int>::iterator it = ma.lower_bound(-sum);
if(it != ma.end()) ans = min(ans, make_pair(_abs(sum + it->first), num + it->second));
if(it != ma.begin()) --it, ans = min(ans, make_pair(_abs(sum + it->first), num + it->second));
}
printf("%lld %d\n", ans.first, ans.second);
}
return 0;
}

POJ 2549 Sumsets

#include <map>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 1111, INF = 0x3f3f3f3f;
int n, arr[N]; struct book{
int val, i, j;
book(){};
book(int vv, int ii, int jj): val(vv), i(ii), j(jj) {};
const bool operator < (const book& o) const {
return val < o.val;
}
const bool operator > (const book& o) const {
return val > o.val;
}
const bool operator != (const book& o) const {
return i != o.i && j != o.j && i != o.j && j != o.i;
}
}; int main(){
while(scanf("%d", &n), n){
int ans = -INF;
for(int i = 0; i < n; ++i) scanf("%d", &arr[i]);
vector<book> left, right;
for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j) left.push_back(book(arr[i] + arr[j], i, j));
for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j){
right.push_back(book(arr[i] - arr[j], i, j));
right.push_back(book(arr[j] - arr[i], j, i));
}
sort(left.begin(), left.end());
sort(right.begin(), right.end());
for(vector<book>::iterator it = left.begin(); it != left.end(); ++it){
vector<book>::iterator itl = lower_bound(right.begin(), right.end(), *it);
vector<book>::iterator itr = upper_bound(itl, right.end(), *it);
while(itl != itr){
if(*it != *itl) ans = max(ans, arr[itl->j] + it->val);
++itl;
}
}
if(ans == -INF) puts("no solution");
else printf("%d\n", ans);
}
return 0;
}

AOJ 0531 Paint Color

#include <bits/stdc++.h>
using namespace std; const int N = 1002;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int h, w, n, X1[N], X2[N], Y1[N], Y2[N], fild[N<<1][N<<1]; int compress(int* x1, int* x2, int W){
vector<int> xs;
for(int i = 0; i < n; ++i) xs.push_back(x1[i]), xs.push_back(x2[i]);
xs.push_back(0); xs.push_back(W);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for(int i = 0; i < n; ++i){
x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
return xs.size() - 1;
} void imos(){
memset(fild, 0, sizeof(fild));
for(int i = 0; i < n; ++i){
++fild[Y1[i]][X1[i]];
--fild[Y1[i]][X2[i]];
++fild[Y2[i]][X2[i]];
--fild[Y2[i]][X1[i]];
}
for(int i = 0; i < h; ++i) for(int j = 1; j < w; ++j) fild[i][j] += fild[i][j-1];
for(int i = 1; i < h; ++i) for(int j = 0; j < w; ++j) fild[i][j] += fild[i-1][j];
} int bfs(){
int ans = 0;
for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j){
if(fild[i][j]) continue;
++ans;
queue<pair<int, int> > q;
q.push(make_pair(i, j));
fild[i][j] = 1;
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
for(int d = 0; d < 4; ++d){
int xx = x + dx[d], yy = y + dy[d];
if(xx < 0 || h <= xx || yy < 0 || w <= yy || fild[xx][yy]) continue;
q.push(make_pair(xx, yy));
fild[xx][yy] = 1;
}
}
}
return ans;
} int main(){
while(~scanf("%d %d", &w, &h)){
if(w == 0 && h == 0) break;
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%d %d %d %d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
w = compress(X1, X2, w);
h = compress(Y1, Y2, h);
imos();
printf("%d\n", bfs());
}
return 0;
}

POJ 3041 Asteroids

#include <cstdio>
#include <vector>
#include <cstring>
using namespace std; const int N = 100010;
int n, k;
vector<int> G[N<<1];
int match[N];
bool used[N]; bool dfs(int v){
used[v] = true;
for(int i = 0; i < G[v].size(); ++i){
int u = G[v][i], w = match[u];
if(w < 0 || (!used[w] && dfs(w))){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int bipartite_matching(){
int ans = 0;
memset(match, -1, sizeof(match));
for(int i = 0; i < n; ++i){
if(match[i] < 0){
memset(used, false, sizeof(used));
if(dfs(i)) ++ans;
}
}
return ans;
} int main(){
scanf("%d %d", &n, &k);
while(k--){
int x, y;
scanf("%d %d", &x, &y);
G[x-1].push_back(y+n-1);
G[y+n-1].push_back(x-1);
}
n <<= 1;
printf("%d\n", bipartite_matching());
return 0;
}
上一篇:P3810 【模板】三维偏序(陌上花开)(CDQ分治)


下一篇:软件设计模式之路-----解释器模式