A题:Mountain Climbing http://acm.fzu.edu.cn/problem.php?pid=2160
比赛时一直在看其他题,没有仔细想这道题。
做这道题之前,首先要知道:
已知三个点的坐标: A(x1,y1), B(x2,y2), C(x3,y3)1.若(x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) > 0,则C在线段AB的上方;
2.若(x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) < 0,则C在线段AB的下方;
3.若(x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) == 0,则C在线段AB上。
这样,我们就可以从后往前遍历,依次找从当前点开始能够和距离当前点最远的那座山连接绳索,因为后边的已经求出来了,所以当前点的时间为找到的最远点的的时间加1。
还有一个要注意的地方:题目中山的高度为10^8,在相乘时会超int,所以应该用64位整数。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
struct node
{
__int64 x;
__int64 y;
}a[N];
int next[N], dp[N];
bool judge(node a0, node a1, node a2)
{
__int64 k = (a0.x - a2.x) * (a1.y - a2.y) - (a0.y - a2.y) * (a1.x - a2.x);
if(k < 0) return 1;
return 0;
}
int main()
{
int n, i, T, cas = 0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i = 1; i <= n; i++)
scanf("%I64d%I64d",&a[i].x, &a[i].y);
memset(next, 0, sizeof(next));
dp[n] = 0;
dp[n-1] = 1;
next[n-1] = n;
for(i = n - 2; i > 0; i--)
{
int tmp = i + 1;
while(tmp != n && judge(a[next[tmp]], a[tmp], a[i]))
{
tmp = next[tmp];
}
next[i] = tmp;
dp[i] = dp[tmp] + 1;
}
printf("Case#%d: ",++cas);
for(i = 1; i < n; i++)
printf("%d ", dp[i]);
printf("%d\n", dp[n]);
}
return 0;
}
D题:多米诺骨牌 http://acm.fzu.edu.cn/problem.php?pid=2163
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
struct node
{
int x; //位置坐标
int h; //高度
int id; //编号
int maxr; //最右边端点
int ans; //能推倒的个数
}a[N];
bool comp1(node a1, node a2)
{
return a1.x < a2.x;
}
bool comp2(node a1, node a2)
{
return a1.id < a2.id;
}
int main()
{
int n, i, j;
while(~scanf("%d",&n))
{
for(i = 0; i < n; i++)
{
scanf("%d%d",&a[i].x, &a[i].h);
a[i].id = i;
a[i].ans = 1;
a[i].maxr = a[i].x + a[i].h - 1;
}
sort(a, a+n, comp1);
for(i = n - 2; i >= 0; i--)
{
j = i + 1;
int r = a[i].maxr;
while(1)
{
if(j >= n || a[j].x > r) break;
a[i].ans += a[j].ans;
r = max(a[j].maxr, r); //更新第i个骨牌能推倒的骨牌的最右边端点
j += a[j].ans; //连续的a[j].ans个都能推倒,所以可以直接往后跳
}
a[i].maxr = r;
}
sort(a, a+n, comp2);
for(i = 0; i < n - 1; i++)
printf("%d ",a[i].ans);
printf("%d\n",a[n-1].ans);
}
return 0;
}