Increasing Speed Limits

本文针对Google Code Jam中的一道题“Increasing Speed Limits”,介绍了如何通过优化动态规划算法来解决寻找递增子序列数量的问题。通过对原始序列进行排序和离散化处理,并利用线段树进行区间求和,将复杂度降至NlogN。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google Cod Jam Round1C C题 Increasing Speed Limits

Problem

You were driving along a highway when you got caught by the road police for speeding. It turns out that they've been following you, and they were amazed by the fact that you were accelerating the whole time without using the brakes! And now you desperately need an excuse to explain that.

You've decided that it would be reasonable to say "all the speed limit signs I saw were in increasing order, that's why I've been accelerating". The police officer laughs in reply, and tells you all the signs that are placed along the segment of highway you drove, and says that's unlikely that you were so lucky just to see some part of these signs that were in increasing order.

Now you need to estimate that likelihood, or, in other words, find out how many different subsequences of the given sequence are strictly increasing. The empty subsequence does not count since that would imply you didn't look at any speed limits signs at all!

For example, (1, 2, 5) is an increasing subsequence of (1, 4, 2, 3, 5, 5), and we count it twice because there are two ways to select (1, 2, 5) from the list.

Input

The first line of input gives the number of cases, N. N test cases follow. The first line of each case contains n, m, X, Y and Z each separated by a space. n will be the length of the sequence of speed limits. m will be the length of the generating array A. The next m lines will contain the m elements of A, one integer per line (from A[0] to A[m-1]).

Using A, X, Y and Z, the following pseudocode will print the speed limit sequence in order. mod indicates the remainder operation.

for i = 0 to n-1
print A[i mod m]
A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

Note: The way that the input is generated has nothing to do with the intended solution and exists solely to keep the size of the input files low.

Output

For each test case you should output one line containing "Case #T: S" (quotes for clarity) where T is the number of the test case and S is the number of non-empty increasing subsequences mod 1 000 000 007.

Limits

1 ≤ N ≤ 20
1 ≤ m ≤ 100
0 ≤ X ≤ 109
0 ≤ Y ≤ 109
1 ≤ Z ≤ 109
0 ≤ A[i] < Z

Small dataset

1 ≤ mn ≤ 1000

Large dataset

1 ≤ mn ≤ 500 000

Sample


Input
 

Output
 
2
5 5 0 0 5
1
2
1
2
3
6 2 2 1000000000 6
1
2

Case #1: 15
Case #2: 13

The sequence of speed limit signs for case 2 should be 1, 2, 0, 0, 0, 4.

没赶上Round1A 郁闷。
Round1C Solve1和2,3的large不会做,菜。Rank好像是60多,能过。

赛后学习了下,也不算太难。
本来DP方程是这样的
for(i = 0; i < n; ++i) {
 for(j = 0; j < i; ++j) {
  if(A[j] < A[i]) {
    dp[i] += dp[j];
  }
 }
}
如果对A排序并且离散化,则变成了
for(i=0; i < n; ++i) {
  for(j = 0; j < A[i]; ++j) {
   dp[A[i]] += dp[j];
  }
}


大家注意看,内循环其实是一个区间求和。那么对于这种求和,线段树只可以做到NlogN的。
记得以前写过一道题的解题报告,是类似的。
pku1769 点树解决块查询点操作

下面是代码:(solve2函数是一个n^2的DP,偶水small input用的)
// Solution by alpc12  
#include 
< stdio.h >
#include 
< cassert >
#include 
< map >
#include 
< algorithm >
using   namespace  std;

const   int  M  =   100 ;
const   int  N  =   500010 ;
const   int  MOD  =   1000000007 ;

typedef 
long   long  LL;

int  n, m, X, Y, Z;
int  A[N], S[N];
int  st[ 1048576 ];
int  upperbound  =   524288 ;
int  dp[N];

void  generate() {
    
int  i;
    
for (i  =   0 ; i  <  n;  ++ i) {
        S[i] 
=  A[i % m];
        A[i
% m]  =  ((LL)X * A[i % m] + (LL)Y * (i + 1 )) % Z;
    }
    
for (i  =   0 ; i  <  n;  ++ i) {
        A[i] 
=  S[i];  
    }
}

int   get ( int  x,  int  y) {  //  左闭右开
    x  +=  upperbound, y  +=  upperbound;
    
int  ans  =   0 ;
    
while (x  +   1   <  y) {
        
if (x & 1 ) {  //  x是右子树 
            ans  =  (ans  +  st[x])  %  MOD;
            x
++ ;
        }
        
if (y & 1 ) {  //  y是右子树
            y -- ;
            ans 
=  (ans  +  st[y])  %  MOD;
        }
        x 
>>=   1 ;
        y 
>>=   1 ;
    }
    
if (x  <  y) 
        ans 
=  (ans  +  st[x])  %  MOD;
    
return  ans;
}

void  ins( int  x,  int  a) {
    x 
+=  upperbound;
    
while (x  >   0 ) {
        st[x] 
=  (st[x]  +  a)  %  MOD;
        x 
>>=   1 ;
    }
}

void  solve() {
    memset(st, 
0 sizeof (st));
    sort(S, S 
+  n);
    map
< int int >  mm;
    
int  i, j  =   0 , ans  =   0 ;
    
for (i  =   0 ; i  <  n;  ++ i) {
        
if ( ! mm.count(S[i])) {
            mm[S[i]] 
=   ++ j;
        }
    }
    ins(
0 1 );
    
for (i  =   0 ; i  <  n;  ++ i) {
        A[i] 
=  mm[A[i]];
        
int  sum  =   get ( 0 , A[i]);
        ans 
=  (ans  +  sum)  %  MOD;
        ins(A[i], sum);
    }
    printf(
" %d/n " , ans);
}

void  solve2() {
    
int  i, j, k;
    
for (i  =   0 ; i  <  n;  ++ i) dp[i]  =   1 ;
    
for (i  =   1 ; i  <  n;  ++ i) {
        
for (j  =   0 ; j  <  i;  ++ j) {
            
if (S[j]  <  S[i]) {
                dp[i] 
+=  dp[j];
                dp[i] 
%=  MOD;
            }
        }
    }
    LL sum 
=   0 ;
    
for (i  =   0 ; i  <  n;  ++ i) {
        sum 
+=  dp[i];
        sum 
%=  MOD;
    }
    printf(
" %I64d/n " , sum);
}

int  main()
{
//     freopen("C-large.in", "r", stdin);
//     freopen("C-large.txt", "w", stdout);

    
int  ntc, i, j, k, tc = 0 ;
    scanf(
" %d " & ntc);
    
while (ntc -- ) {
        printf(
" Case #%d:  " ++ tc);
        scanf(
" %d%d%d%d%d " & n,  & m,  & X,  & Y,  & Z);
        
for (i  =   0 ; i  <  m;  ++ i) scanf( " %d " , A + i);
        generate();
//         solve2();
        solve();
    }
    
return   0 ;
}

 

文章来自:http://www.cppblog.com/sicheng/archive/2008/08/02/57824.html

#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <stdexcept> #include <fstream> #include <iomanip> #include <limits> using namespace std; const double MIN_RADIUS = 10.0; // 最小转弯半径约束 // 二维点结构 struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); } Point operator*(double s) const { return Point(x * s, y * s); } Point operator/(double s) const { return Point(x / s, y / s); } double dot(const Point& p) const { return x * p.x + y * p.y; } double cross(const Point& p) const { return x * p.y - y * p.x; } double norm() const { return sqrt(x*x + y*y); } Point normalized() const { double n = norm(); return n > 0 ? *this / n : *this; } Point perpendicular() const { return Point(-y, x); } // 垂直向量 friend ostream& operator<<(ostream& os, const Point& p) { os << "(" << p.x << ", " << p.y << ")"; return os; } }; // 圆弧段 struct ArcSegment { Point center; double radius; double start_angle; // 弧度 double end_angle; // 弧度 vector<Point> generatePoints(int num_points = 30) const { vector<Point> points; double angle_step = (end_angle - start_angle) / num_points; for (int i = 0; i <= num_points; i++) { double angle = start_angle + i * angle_step; double x = center.x + radius * cos(angle); double y = center.y + radius * sin(angle); points.push_back(Point(x, y)); } return points; } }; // 直线段 struct LineSegment { Point start; Point end; vector<Point> generatePoints(int num_points = 30) const { vector<Point> points; Point dir = (end - start); double length = dir.norm(); if (length < 1e-10) return points; dir = dir / length; for (int i = 0; i <= num_points; i++) { double t = static_cast<double>(i) / num_points; Point p = start + dir * (t * length); points.push_back(p); } return points; } }; // 路径规划器 class PathPlanner { private: double min_radius; public: PathPlanner(double min_radius = MIN_RADIUS) : min_radius(min_radius) {} // 生成满足转弯半径的几何路径 vector<Point> generateGeometricPath(const Point& start, const Point& end) { // 设计关键点 Point A = start; Point B(-2, 4); // 第一个转弯起点 Point C(4, 11); // 第二个转弯起点 Point D = end; // 计算转弯参数 - 确保半径至少为min_radius double R = min_radius * 1.5; // 使用1.5倍最小转弯半径 // 第一个转弯:从垂直转向水平 Point center1(B.x + R, B.y); double angle1_start = M_PI; // 180度 double angle1_end = M_PI_2; // 90度 // 第二个转弯:从水平转向垂直 Point center2(C.x, C.y - R); double angle2_start = -M_PI_2; // -90度 double angle2_end = 0; // 0度 // 生成路径点 vector<Point> path_points; // 第一段直线: A to B LineSegment line1 = {A, B}; vector<Point> line1_points = line1.generatePoints(40); path_points.insert(path_points.end(), line1_points.begin(), line1_points.end()); // 第一段圆弧: B to arc_end1 ArcSegment arc1 = {center1, R, angle1_start, angle1_end}; Point arc_end1(center1.x, center1.y - R); vector<Point> arc1_points = arc1.generatePoints(50); path_points.insert(path_points.end(), arc1_points.begin(), arc1_points.end()); // 第二段直线: arc_end1 to arc_start2 Point arc_start2(center2.x - R, center2.y); LineSegment line2 = {arc_end1, arc_start2}; vector<Point> line2_points = line2.generatePoints(40); path_points.insert(path_points.end(), line2_points.begin(), line2_points.end()); // 第二段圆弧: arc_start2 to C ArcSegment arc2 = {center2, R, angle2_start, angle2_end}; vector<Point> arc2_points = arc2.generatePoints(50); path_points.insert(path_points.end(), arc2_points.begin(), arc2_points.end()); // 第三段直线: C to D LineSegment line3 = {C, D}; vector<Point> line3_points = line3.generatePoints(40); path_points.insert(path_points.end(), line3_points.begin(), line3_points.end()); return path_points; } // 从几何路径生成贝塞尔曲线控制点(增加控制点数量) vector<Point> generateBezierControlPoints(const vector<Point>& path_points) { vector<Point> control_points; if (path_points.size() < 4) return control_points; // 增加控制点数量 - 每10个路径点取一个控制点 int step = max(1, (int)path_points.size() / 20); for (int i = 0; i < path_points.size(); i += step) { control_points.push_back(path_points[i]); } // 确保包含起点和终点 if (control_points.back() != path_points.back()) { control_points.push_back(path_points.back()); } // 在转弯区域添加额外控制点 int turn1_start = path_points.size() * 40 / 100; // 第一个转弯开始位置 int turn1_end = path_points.size() * 50 / 100; // 第一个转弯结束位置 int turn2_start = path_points.size() * 70 / 100; // 第二个转弯开始位置 int turn2_end = path_points.size() * 80 / 100; // 第二个转弯结束位置 // 添加第一个转弯区域的额外控制点 for (int i = turn1_start; i <= turn1_end; i += step/2) { if (i < path_points.size()) { control_points.push_back(path_points[i]); } } // 添加第二个转弯区域的额外控制点 for (int i = turn2_start; i <= turn2_end; i += step/2) { if (i < path_points.size()) { control_points.push_back(path_points[i]); } } // 排序并去重 sort(control_points.begin(), control_points.end(), [](const Point& a, const Point& b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; }); auto last = unique(control_points.begin(), control_points.end(), [](const Point& a, const Point& b) { return abs(a.x - b.x) < 0.1 && abs(a.y - b.y) < 0.1; }); control_points.erase(last, control_points.end()); // 确保控制点间距足够 for (int i = 1; i < control_points.size() - 1; i++) { double dist1 = (control_points[i] - control_points[i-1]).norm(); double dist2 = (control_points[i+1] - control_points[i]).norm(); // 如果间距小于转弯半径,调整位置 if (dist1 < min_radius || dist2 < min_radius) { Point dir = (control_points[i+1] - control_points[i-1]).normalized(); control_points[i] = control_points[i-1] + dir * min_radius * 1.5; } } return control_points; } }; // 贝塞尔曲线类 class BezierCurve { private: vector<Point> controlPoints; // 计算二项式系数 double binomialCoefficient(int n, int k) const { if (k < 0 || k > n) return 0; if (k == 0 || k == n) return 1; double result = 1; for (int i = 1; i <= k; i++) { result = result * (n - i + 1) / i; } return result; } // 计算伯恩斯坦基函数 double bernstein(int n, int i, double t) const { return binomialCoefficient(n, i) * pow(t, i) * pow(1 - t, n - i); } public: BezierCurve(const vector<Point>& points) : controlPoints(points) { if (points.size() < 2) { throw invalid_argument("At least 2 control points required"); } } // 计算曲线上的点 Point evaluate(double t) const { if (t < 0 || t > 1) { t = max(0.0, min(1.0, t)); } Point result(0, 0); int n = controlPoints.size() - 1; for (int i = 0; i <= n; i++) { double basis = bernstein(n, i, t); result = result + controlPoints[i] * basis; } return result; } // 计算一阶导数(速度) Point derivative(double t) const { if (t < 0 || t > 1) { t = max(0.0, min(1.0, t)); } Point result(0, 0); int n = controlPoints.size() - 1; for (int i = 0; i < n; i++) { double basis = bernstein(n - 1, i, t); Point diff = controlPoints[i + 1] - controlPoints[i]; result = result + diff * (n * basis); } return result; } // 计算二阶导数(加速度) Point secondDerivative(double t) const { if (t < 0 || t > 1) { t = max(0.0, min(1.0, t)); } Point result(0, 0); int n = controlPoints.size() - 1; if (n < 2) { return result; } for (int i = 0; i < n - 1; i++) { double basis = bernstein(n - 2, i, t); Point diff = (controlPoints[i + 2] - controlPoints[i + 1]) - (controlPoints[i + 1] - controlPoints[i]); result = result + diff * (n * (n - 1) * basis); } return result; } // 计算曲率 double curvature(double t) const { Point vel = derivative(t); Point acc = secondDerivative(t); double speed = vel.norm(); if (speed < 1e-10) { return numeric_limits<double>::infinity(); } double crossProd = fabs(vel.cross(acc)); return crossProd / pow(speed, 3); } // 计算曲率半径 double curvatureRadius(double t) const { double k = curvature(t); if (k < 1e-10 || k > 1e10) { return numeric_limits<double>::infinity(); } return 1.0 / k; } // 检查曲率约束 bool checkCurvatureConstraint(double minRadius, int samples = 200) const { for (int i = 0; i <= samples; i++) { double t = static_cast<double>(i) / samples; double radius = curvatureRadius(t); if (radius < minRadius && !isinf(radius)) { cerr << "Curvature violation at t=" << t << ": radius=" << radius << "m < minRadius=" << minRadius << "m" << endl; return false; } } return true; } // 获取曲线上的点集 vector<Point> getCurvePoints(int samples = 200) const { vector<Point> points; points.reserve(samples + 1); for (int i = 0; i <= samples; i++) { double t = static_cast<double>(i) / samples; points.push_back(evaluate(t)); } return points; } // 获取最小曲率半径 double getMinCurvatureRadius(int samples = 500) const { double minRadius = numeric_limits<double>::infinity(); for (int i = 0; i <= samples; i++) { double t = static_cast<double>(i) / samples; double radius = curvatureRadius(t); if (!isinf(radius) { if (radius < minRadius) { minRadius = radius; // 调试信息 // cerr << "New min radius: " << minRadius << " at t=" << t << endl; } } } return minRadius; } }; // 导出点集到CSV文件 void exportToCSV(const string& filename, const vector<Point>& points) { ofstream file(filename); if (!file) { cerr << "Error opening file: " << filename << endl; return; } file << "x,y" << endl; file << fixed << setprecision(6); for (const auto& p : points) { file << p.x << "," << p.y << endl; } cout << "Exported " << points.size() << " points to " << filename << endl; } // 可视化路径 void visualizePath() { ofstream script("visualize.py"); if (!script) { cerr << "Error creating visualization script" << endl; return; } script << R"( import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Circle # 加载数据 control = np.genfromtxt('control_points.csv', delimiter=',', skip_header=1) original = np.genfromtxt('original_curve.csv', delimiter=',', skip_header=1) geometric = np.genfromtxt('geometric_path.csv', delimiter=',', skip_header=1) constrained_controls = np.genfromtxt('constrained_control_points.csv', delimiter=',', skip_header=1) constrained = np.genfromtxt('constrained_curve.csv', delimiter=',', skip_header=1) # 可视化 plt.figure(figsize=(12, 10)) plt.plot(control[:,0], control[:,1], 'ro-', markersize=4, label='Original Control Points') plt.plot(original[:,0], original[:,1], 'b-', linewidth=1.5, alpha=0.7, label='Original Curve') plt.plot(geometric[:,0], geometric[:,1], 'm--', linewidth=2, alpha=0.5, label='Geometric Path') plt.plot(constrained_controls[:,0], constrained_controls[:,1], 'go-', markersize=5, label='Constrained Control Points') plt.plot(constrained[:,0], constrained[:,1], 'g-', linewidth=2.5, label='Constrained Curve') # 添加标签和标题 plt.legend(loc='upper left') plt.grid(True) plt.axis('equal') plt.title('Path Planning with Curvature Constraint (Min Radius: 10m)') plt.xlabel('X') plt.ylabel('Y') # 显示曲率约束区域 plt.text(-3, 16, "Min Turning Radius: 10m", fontsize=12, bbox=dict(facecolor='white', alpha=0.8)) # 绘制转弯半径圆 circle1 = Circle((-2+15, 4), 10, color='orange', fill=False, linestyle='--', label='Turning Radius (15m)') circle2 = Circle((4, 11-15), 10, color='purple', fill=False, linestyle='--') plt.gca().add_patch(circle1) plt.gca().add_patch(circle2) # 添加圆弧中心标记 plt.plot(-2+15, 4, 'yo', markersize=8, label='Turn Center 1') plt.plot(4, 11-15, 'co', markersize=8, label='Turn Center 2') # 保存并显示 plt.savefig('path_comparison.png', dpi=300) plt.show() )"; cout << "Visualization script saved as visualize.py" << endl; cout << "Run 'python visualize.py' to generate the plot" << endl; } int main() { // 测试用例控制点 vector<Point> controlPoints = { {-2, 0}, {-2, 0.5}, {-2, 1}, {-2, 1.5}, {-2, 2}, {-2, 2.5}, {-2, 3}, {-2, 3.5}, {-2, 4}, {4, 11}, {4, 11.5}, {4, 12}, {4, 12.5}, {4, 13}, {4, 13.5}, {4, 14}, {4, 14.5}, {4, 15} }; try { // 1. 创建原始贝塞尔曲线 BezierCurve originalCurve(controlPoints); // 检查原始曲线的曲率约束 bool originalSatisfies = originalCurve.checkCurvatureConstraint(MIN_RADIUS); double originalMinRadius = originalCurve.getMinCurvatureRadius(); cout << "Original curve min curvature radius: " << originalMinRadius << " m" << endl; cout << "Original curve satisfies curvature constraint: " << (originalSatisfies ? "YES" : "NO") << endl; // 导出原始曲线和控制点 exportToCSV("control_points.csv", controlPoints); vector<Point> originalCurvePoints = originalCurve.getCurvePoints(200); exportToCSV("original_curve.csv", originalCurvePoints); // 2. 创建几何路径和满足约束的贝塞尔曲线 PathPlanner planner(MIN_RADIUS); // 生成几何路径(直线+圆弧) vector<Point> geometricPath = planner.generateGeometricPath(controlPoints[0], controlPoints.back()); exportToCSV("geometric_path.csv", geometricPath); // 从几何路径生成控制点(增加控制点数量) vector<Point> constrainedControlPoints = planner.generateBezierControlPoints(geometricPath); exportToCSV("constrained_control_points.csv", constrainedControlPoints); // 创建约束曲线 BezierCurve constrainedCurve(constrainedControlPoints); // 检查优化后的曲线 bool constrainedSatisfies = constrainedCurve.checkCurvatureConstraint(MIN_RADIUS); double constrainedMinRadius = constrainedCurve.getMinCurvatureRadius(); cout << "\nConstrained curve min curvature radius: " << constrainedMinRadius << " m" << endl; cout << "Constrained curve satisfies curvature constraint: " << (constrainedSatisfies ? "YES" : "NO") << endl; // 导出优化后的曲线 vector<Point> constrainedCurvePoints = constrainedCurve.getCurvePoints(200); exportToCSV("constrained_curve.csv", constrainedCurvePoints); // 3. 生成可视化脚本 visualizePath(); if (constrainedSatisfies) { cout << "\nSUCCESS: Generated path satisfies minimum turning radius of " << MIN_RADIUS << " meters with " << constrainedControlPoints.size() << " control points." << endl; } else { cerr << "\nWARNING: Curve does not fully satisfy curvature constraints. " << "Consider further increasing the number of control points." << endl; } } catch (const exception& e) { cerr << "Error: " << e.what() << endl; return 1; } return 0; }最小转弯半径为10m,优化一下它的几何路径
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值