POJ 1852 Ants

最初やったら何故かG++でcompileされててさらにTLEくらって訳が分からなかった。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
  int c,n,l,a;
  cin >> c;
  while(c--){
    int minT=0, maxT=0;
    vector<int> v;
    cin >> l >> n;
    for(int i = 0; i < n; i++){
      cin >> a;
      v.push_back(a);
    }
    for(int i = 0; i < n; i++){
      minT = max(minT, min(v[i], l - v[i]));
      maxT = max(maxT, max(v[i], l - v[i]));
    }
    cout << minT << ' ' << maxT << endl;
  }
}

POJ 1046 Color Me Less

問題文要約
そのまま実装。int型でsqrtしなくてもオーバーフローしないかなーとか思ってたけどAcceptできたのでよしとしよう。

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

class color{
public:
  int r,g,b,d;
};

bool LessColor(const color& l, const color& r){ return l.d < r.d;}

int main(){
  vector<color> v(16);
  color a;
  for(int i = 0; i < 16; i++) 
    scanf("%d%d%d",&v[i].r,&v[i].g,&v[i].b);
  
  while(scanf("%d%d%d",&a.r,&a.g,&a.b),(a.r!=-1&&a.g!=-1&&a.b!=-1)){
    for(int i = 0; i < 16; i++){
      v[i].d = (a.r-v[i].r)*(a.r-v[i].r)+(a.g-v[i].g)*(a.g-v[i].g)+(a.b-v[i].b)*(a.b-v[i].b);
    }
    sort(v.begin(),v.end(),LessColor);
    printf("(%d,%d,%d) maps to (%d,%d,%d)\n",a.r,a.g,a.b,v[0].r,v[0].g,v[0].b);
  }
}

POJ 1005 I Think I Need a Houseboat

問題文要約
図のような島があり、1年で50平方マイルずつ中心から半円で侵食していく。座標が与えられるので、その座標の位置が侵食される年数を出力せよ。

#include <cmath>
#include <cstdio>
using namespace std;

const double PI = acos(-1);

int main(){
  int n,year;
  double a,b;
  scanf("%d",&n);
  for(int i = 0; i < n; i++){
    scanf("%lf%lf",&a,&b);
    year = ceil((a * a + b * b) * PI / (50 * 2));
    printf("Property %d: This property will begin eroding in year %d.\n",i+1,year);
  }
  puts("END OF OUTPUT.");
}