【milking cows】 /* ID: wangqia6 TASK: milk2 LANG: C++ */ #include <fstream> using namespace std; long st[5005],en[5005]; void swapq(long &a,long &b) { long tmp; tmp = a; a = b; b = tmp; } void quicksort(int s,int t) { int l = s,r = t; long key = st[s]; while (l < r) { while (st[l] < key) l++; while (st[r] > key) r--; if (l<=r) { swapq(st[l],st[r]); swapq(en[l],en[r]); l++; r--; } } if (s < r) quicksort(s,r); if (l < t) quicksort(l,t); } int main() { ifstream infile; ofstream outfile; infile.open("milk2.in"); outfile.open("milk2.out"); int n; infile >> n; for (int i = 0; i < n; i++) infile >> st[i] >> en[i]; quicksort(0,n-1); long have = en[0] - st[0], no = 0,leftp = st[0],rightp = en[0]; int i = 0,j; while (i < n) { j = i + 1; while ((j < n) && (st[j] <= rightp)) { if (en[j] > rightp) rightp = en[j]; j++; } if (rightp - leftp > have) have = rightp - leftp; if ((j < n) && (st[j] - rightp > no)) no = st[j] - rightp; leftp = st[j]; rightp = en[j]; i = j; } outfile << have << ' ' << no << endl; infile.close(); outfile.close(); return 0; } 【dual palindromes】 /* ID: wangqia6 TASK: dualpal LANG: C++ */ #include <fstream> using namespace std; int ok(long x,int base) { int p = 0,arr[16]; while (x > 0) { arr[p] = x % base; x /= base; p++; } p--; for (int i = 0; i <= p / 2; i++) if (arr[i] != arr[p - i]) return 0; return 1; } int check(long x) { int sum = 0; for (int base = 2; base <= 10; base++) { if (ok(x,base)) sum++; if (sum == 2) return 1; } return 0; } int main() { ifstream infile; ofstream outfile; infile.open("dualpal.in"); outfile.open("dualpal.out"); int n,sum = 0; long x; infile >> n >> x; while (sum != n) { x++; if (check(x)) { sum++; outfile << x << endl; } } infile.close(); outfile.close(); return 0; }