51nod 1100 斜率最大

博客探讨了51nod的1100题,寻找平面上N个点中斜率最大的直线。博主首先尝试暴力解法,然后通过排序优化,最后发现斜率最大的两点总是相邻的。博客分享了学习过程,包括代码实现和数学规律的运用,旨在帮助读者理解并解决问题。

 

平面上有N个点,任意2个点确定一条直线,求出所有这些直线中,斜率最大的那条直线所通过的两个点。

(点的编号为1-N,如果有多条直线斜率相等,则输出所有结果,按照点的X轴坐标排序,正序输出。数据中所有点的X轴坐标均不相等,且点坐标为随机。)

Input

第1行,一个数N,N为点的数量。(2 <= N <= 10000)
第2 - N + 1行:具体N个点的坐标,X Y均为整数(-10^9 <= X,Y <= 10^9)

Output

每行2个数,中间用空格分隔。分别是起点编号和终点编号(起点的X轴坐标 < 终点的X轴坐标)

Input示例

5
1 2
6 8
4 4
5 4
2 3

Output示例

4 2

 

 

 

  学校老司机建议写下自己当时思路,有利于以后复习,一开始还没怎么在意,今天看两周前的题目居然忘了怎么做,吓得我马上开了个博客,主要是学习过程,非大神,看官们轻喷

  

  首先看了这道题第一感觉是暴力(菜鸡只能暴力), 最暴力的方法就是两个for循环,比较斜率,估计会爆所以跳过

         1  :将点首先按x坐标排序,有利于保存答案和优化

         2  :计算过程保存一个最高y点,在x增大的情况下y如果一样大,肯定斜率更低。

         3  :保存的话用数组保存两个点的x值,因为题目x都不相同,题目要求输出原来的数组的顺序点,所以开个数组保存原顺序,然后算出答案从中匹配

       附上代码:

    

#include<iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <stack>
#include<cstdio>
using namespace std;
   long long int n,i,j,ansnum=0,ymax=0,temp,shunxu[10005],ax=0,ay=0;
   double xielv=0,txielv;
 struct dian
    {
     long long   int  x,y;
    }d[10005],ans[10005];
int cmp(dian a,dian b)
{
    if(a.x<b.x)
        return 1;
    return 0;
}
int main()
{

   scanf("%lld",&n);
   for(i=0;i<n;i++)
   {
    scanf("%lld%lld",&d[i].x,&d[i].y);
    shunxu[i]=d[i].x;
   }
   sort(d,d+n,cmp);
   temp=n-1;
   for(i=0;i<temp;i++)
   {
       ymax=d[i].y;
       for(j=i+1;j<=temp;j++)
       {
           if(d[j].y<=ymax)
            continue;
            if(d[j].y<=d[i].y)
                continue;
            txielv=(double(d[j].y-d[i].y))/(double(d[j].x-d[i].x));
           if(txielv>xielv)
           {
               ymax=d[j].y;
               xielv=txielv;
               ans[0].x=d[i].x;
               ans[0].y=d[j].x;
               ansnum=1;
           }
           else if(txielv==xielv)
           {
               ans[ansnum].x=d[i].x;
               ans[ansnum].y=d[j].x;
           }
       }
   }
     for(i=0;i<ansnum;i++)
     {
         for(j=0;j<n;j++)
         {
             if(shunxu[j]==ans[i].x)
                ax=j+1;
             else if(shunxu[j]==ans[i].y)
                ay=j+1;
         }
         printf("%lld %lld\n",ax,ay);
     }
    return 0;
}

 

 

 

然后发现别人的代码都是20ms以内

发现有个数学规律,排序后斜率最大的两个点肯定是相邻的,用反证法更容易验证,那我写那么多干吗唉

附上代码:

 

#include<iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <stack>
#include<cstdio>
#include <set>
#include<queue>
using namespace std;
   long long int n,i,j,ansnum=0,ymax=0,temp,shunxu[10005],ax=0,ay=0;
   double xielv=0,txielv;
 struct dian
    {
     long long   int  x,y;
    }d[10005],ans[10005];
int cmp(dian a,dian b)
{
    if(a.x<b.x)
        return 1;
    return 0;
}
int main()
{

   scanf("%lld",&n);
   for(i=0;i<n;i++)
   {
    scanf("%lld%lld",&d[i].x,&d[i].y);
    shunxu[i]=d[i].x;
   }
   sort(d,d+n,cmp);
   temp=n-1;
   for(i=0;i<temp;i++)
   {
      j=i+1;
            if(d[j].y<=d[i].y)
                continue;
            txielv=(double(d[j].y-d[i].y))/(double(d[j].x-d[i].x));
           if(txielv>xielv)
           {
               xielv=txielv;
               ans[0].x=d[i].x;
               ans[0].y=d[j].x;
               ansnum=1;
           }
           else if(txielv==xielv)
           {
               ans[ansnum].x=d[i].x;
               ans[ansnum].y=d[j].x;
           }
   }
     for(i=0;i<ansnum;i++)
     {
         for(j=0;j<n;j++)
         {
             if(shunxu[j]==ans[i].x)
                ax=j+1;
             else if(shunxu[j]==ans[i].y)
                ay=j+1;
         }
         printf("%lld %lld\n",ax,ay);
     }
    return 0;
}

 

只是把一个循环改成了i+1;因为按x排序,所以只要和后一个比较

如果早知道这个规律根本不用那么多行,还是太年轻

 

 

 

// 修复动作识别逻辑(基于关键计算,更可靠) analyzeAction(result) { const self = this; const currentAction = this.selectedActions[this.verificationStep - 1]; if (!currentAction) return; const landmarks = result.landmarks; if (!landmarks) { self.currentPrompt = '未检测到面部特征,请对准摄像头'; self.promptStatus = 'warning'; return; } // 提取必要的面部特征集 const facePoints = landmarks.positions; if (!facePoints || facePoints.length < 68) { self.currentPrompt = '面部特征不足,请调整姿势'; self.promptStatus = 'warning'; return; } // 计算面部中心位置 const faceCenter = { x: (facePoints[0].x + facePoints[16].x) / 2, y: (facePoints[27].y + facePoints[8].y) / 2 }; // 计算面部宽度和高度作为参考 const faceWidth = Math.abs(facePoints[16].x - facePoints[0].x); const faceHeight = Math.abs(facePoints[8].y - facePoints[27].y); // 自适应调整检测阈值(根据面部大小动态调整) const adaptiveThreshold = { nod: Math.max(6, faceHeight * 0.12), // 头阈值(基于面部高度百分比) shake: Math.max(8, faceWidth * 0.15), // 摇头阈值 eye: Math.max(3, faceHeight * 0.05), // 眨眼阈值 mouth: Math.max(4, faceWidth * 0.1) // 张嘴阈值 }; // 1. 头检测(基于鼻尖和下巴的相对位置变化) if (currentAction.type === 'nod') { // 提取关键特征 const forehead = (facePoints[19].y + facePoints[24].y) / 2; // 额头平均y坐标 const eyeLine = (facePoints[36].y + facePoints[45].y) / 2; // 眼线平均y坐标 const jawLine = (facePoints[1].y + facePoints[17].y + facePoints[8].y) / 3; // 下颌平均y坐标 // 计算头部俯仰角(基于三连线斜率) const upperDist = Math.abs(eyeLine - forehead); // 额头到眼线的垂直距离 const lowerDist = Math.abs(jawLine - eyeLine); // 眼线到下颌的垂直距离 const pitchRatio = upperDist / lowerDist; // 俯仰比(低头时upperDist增大,pitchRatio上升) // 初始化基准(取前5帧的pitchRatio均值作为静止基准) if (!self.nodBaseRatio) { self.nodRatioHistory = self.nodRatioHistory || []; self.nodRatioHistory.push(pitchRatio); if (self.nodRatioHistory.length >= 5) { self.nodBaseRatio = self.nodRatioHistory.reduce((a, b) => a + b, 0) / 5; self.nodRatioHistory = []; // 重置历史,开始监测动作 self.currentPrompt = '请缓慢头(上下移动)'; } return; } // 动态阈值(基于基准值的比例,而非固定数值) const nodUpThreshold = self.nodBaseRatio * 0.85; // 抬头时pitchRatio降低(低于基准15%) const nodDownThreshold = self.nodBaseRatio * 1.15; // 低头时pitchRatio升高(高于基准15%) // 动作趋势分析(需完成“低头→抬头”完整周期) self.nodRatioHistory = self.nodRatioHistory || []; self.nodRatioHistory.push({ ratio: pitchRatio, time: Date.now() }); if (self.nodRatioHistory.length > 8) self.nodRatioHistory.shift(); // 保留最近8帧(约640ms) // 判断是否出现“先低头(超过down阈值)再抬头(低于up阈值)”的完整趋势 const hasDown = self.nodRatioHistory.some(r => r.ratio > nodDownThreshold); const hasUp = self.nodRatioHistory.some(r => r.ratio < nodUpThreshold); // 趋势有效性验证:最高出现在最低之后(先低头后抬头) const downPoint = self.nodRatioHistory.find(r => r.ratio > nodDownThreshold); const upPoint = self.nodRatioHistory.findLast(r => r.ratio < nodUpThreshold); const trendValid = downPoint && upPoint && downPoint.time < upPoint.time; // 时间有效性验证(动作持续0.3-1.5秒) const actionDuration = self.nodRatioHistory.length > 1 ? self.nodRatioHistory[self.nodRatioHistory.length - 1].time - self.nodRatioHistory[0].time : 0; const timeValid = actionDuration > 300 && actionDuration < 1500; if (hasDown && hasUp && trendValid && timeValid) { self.currentPrompt = '头完成!'; self.promptStatus = 'success'; self.goToNextStep(); // 重置状态,避免重复触发 self.nodBaseRatio = null; self.nodRatioHistory = []; } else { self.currentPrompt = '请幅度稍大头(额头带动动作)'; self.promptStatus = 'warning'; } } // 2. 摇头检测(基于左右脸边界的相对位置变化) else if (currentAction.type === 'shake') { const noseBridge = facePoints[27]; // 鼻梁关键(水平位置参考) const currentX = noseBridge.x; const currentTime = Date.now(); // 1. 记录当前位置到历史数据(只保留最近10条,约800ms内的记录,与检测频率匹配) this.shakeHistory.push({ x: currentX, time: currentTime }); if (this.shakeHistory.length > 10) { this.shakeHistory.shift(); // 移除最旧的记录 } // 2. 历史数据不足时,不判定 if (this.shakeHistory.length < 5) { this.currentPrompt = '请左右摇头...'; this.promptStatus = 'warning'; return; } // 3. 分析历史数据,判断是否有“左→右”或“右→左”的摆动 const firstX = this.shakeHistory[0].x; // 最早记录的x const lastX = this.shakeHistory[this.shakeHistory.length - 1].x; // 最新记录的x const timeElapsed = currentTime - this.shakeHistory[0].time; // 动作持续时间 // 计算左右摆动的总距离(绝对值) const totalDistance = Math.abs(lastX - firstX); // 4. 判断是否完成一次完整摇头动作: // - 总距离超过最小阈值(确保有明显摆动) // - 时间在合理范围内(不超过最大时间) // - 方向发生反转(先左后右或先右后左) const isLeftToRight = firstX < lastX - this.shakeMinDistance; // 先左后右 const isRightToLeft = firstX > lastX + this.shakeMinDistance; // 先右后左 const isValidTime = timeElapsed <= this.shakeMaxTime; if ((isLeftToRight || isRightToLeft) && totalDistance >= this.shakeMinDistance && isValidTime) { // 判定为完成摇头动作 this.updateActionProgress(true, 'shake'); } else { // 未完成,提示继续 this.updateActionProgress(false, 'shake'); // 若超时未完成,重置历史记录(避免累计无效数据) if (timeElapsed > this.shakeMaxTime) { this.shakeHistory = []; this.currentPrompt = '摇头动作太慢,请重试...'; } } } // 眨眼动作增强逻辑 else if (currentAction.type === 'eye') { } // 4. 张嘴检测(新增) else if (currentAction.type === 'mouth') { // 上嘴唇关键(索引51)和下嘴唇关键(索引57) const upperLip = facePoints[51]; const lowerLip = facePoints[57]; // 计算嘴唇垂直距离 const mouthOpenDistance = Math.abs(upperLip.y - lowerLip.y); // 计算嘴部区域高度作为参考(上唇到下巴距离的1/3) const mouthReference = Math.abs(facePoints[27].y - facePoints[8].y) / 3; // 张嘴判定:距离超过参考值的60% const isMouthOpen = mouthOpenDistance > mouthReference * 0.6; if (isMouthOpen) { this.mouthConsecutiveFrames++; // 要求连续3帧检测到张嘴 if (this.mouthConsecutiveFrames >= this.mouthRequiredFrames) { this.updateActionProgress(true, 'mouth'); } } else { this.mouthConsecutiveFrames = Math.max(0, this.mouthConsecutiveFrames - 1); this.updateActionProgress(false, 'mouth'); } console.log(`张嘴检测:距离=${mouthOpenDistance.toFixed(2)}, 参考值=${(mouthReference * 0.6).toFixed(2)}, 是否张嘴=${isMouthOpen}`); } }, 眨眼如何判断
07-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值