POJ 1113 Wall 凸包面积 poj 2187 Beauty Contest
题目连接http://poj.org/problem?id=1113
1113
#include <stdio.h> #include<algorithm> #include <math.h> using namespace std; int q[50050]; int f; struct node { int x,y; }a[50050]; int cmp(struct node p1,struct node p2) { int x1 = p1.x-a[0].x; int x2 = p2.x-a[0].x; int y1 = p1.y-a[0].y; int y2 = p2.y-a[0].y; if(x1*y2 == x2*y1) return x1*x1+y1*y1 < x2*x2+y2*y2; return x1*y2 > x2*y1; } double dis(int i,int j) { double x,y; x = a[i].x-a[j].x; y = a[i].y-a[j].y; return sqrt(x*x+y*y); } int is_left(int i,int j,int k) { int x1 = a[j].x-a[i].x; int x2 = a[k].x-a[j].x; int y1 = a[j].y-a[i].y; int y2 = a[k].y-a[j].y; return x1*y2 >= x2*y1; } int main() { int n,i,j,temp,r; while(~scanf("%d %d",&n,&r)) { temp = 0; for(i = 0;i < n;i++) { scanf("%d %d",&a[i].x,&a[i].y); if(a[i].x > a[temp].x && a[temp].y <= a[i].y) temp =i; } if(i) { struct node t; t = a[temp]; a[temp] = a[0]; a[0] = t; } sort(a+1,a+n,cmp); f = 0; q[f++] = 0; q[f++] = 1; q[f++] = 2; for(i = 3;i < n;) { if(f < 2 || is_left(q[f-2],q[f-1],i)) q[f++] = i++; else f--; } q[f] = q[0]; double ans = 0; for(i = 0;i < f;i++) { ans += dis(q[i],q[i+1]); } ans += 2*r*3.141592653; printf("%.0f\n",ans); } return 0; }
2187
#include <stdio.h> #include<algorithm> using namespace std; int q[50050]; int f; struct node { int x,y; }a[50050]; int cmp(node b,node c) { int x1 = b.x-a[0].x; int x2 = c.x-a[0].x; int y1 = b.y-a[0].y; int y2 = c.y-a[0].y; if(x1*y2 == x2*y1) return x1*x1+y1*y1 < x2*x2+y2*y2; return x1*y2>x2*y1; } int is_left(int i,int j,int k) { int x1 = a[j].x-a[i].x; int x2 = a[k].x-a[j].x; int y1 = a[j].y-a[i].y; int y2 = a[k].y-a[j].y; if(x1*y2>x2*y1) return 1; return 0; } int dis(int i,int j) { return (a[j].x-a[i].x)*(a[j].x-a[i].x)+(a[j].y-a[i].y)*(a[j].y-a[i].y); } int main() { int n,i,j,ans,temp; while(~scanf("%d",&n)) { temp = 0; for(i = 0;i < n;i++) { scanf("%d %d",&a[i].x,&a[i].y); if(a[temp].x > a[i].x && a[temp].y >= a[i].y) temp = i; } if(temp) { struct node t; t = a[0]; a[0] = a[temp]; a[temp] = t; } sort(a+1,a+n,cmp); f = 0; q[f++] = 0; q[f++] = 1; q[f++] = 2; for(i = 3;i < n;) { if(f < 2||is_left(q[f-2],q[f-1],i)) { q[f++] = i++; } else f--; } q[f] = q[0]; ans = 0; for(i = 0;i < f;i++) { for(j = i+1;j < f;j++) if(ans < dis(q[i],q[j])) ans = dis(q[i],q[j]); } printf("%d\n",ans); } return 0; }
#include <stdio.h> #include <string.h> #define N 5050 int dp[2][N]; int main() { int n,i,j; char str[N]; while(~scanf("%d",&n)) { getchar(); gets(str); memset(dp,0,sizeof(dp)); for(i = n-1;i >= 0;i--) { for(j = i+1;j < n;j++) { if(str[i] == str[j]) dp[i][j] = dp[(i+1)][j-1]; else dp[i][j] = (dp[(i+1)][j] < dp[(i)][j-1]?dp[i+1][j]:dp[i][j-1])+1; } } printf("%d\n",dp[0][n-1]); } return 0; }