当前位置:网站首页>[hdu] P7079 Pty loves lines
[hdu] P7079 Pty loves lines
2022-06-22 09:18:00 【Lupin123123】
经典的求直接交点数的问题
之前做过简化版 ,并且安利一下自己的题解
如果依旧像之前一样用二维dp[i][j]表示能否达到i条直线,j个交点的情况,将面临运行超时,dp数组存不下的尴尬局面。那么考虑新的做法。
考虑讲n条直线划分为若干组,每一组内的直线两两平行且这个组是极大的。设总共分成了 p p p 组,每一组的直线数为 a i a_i ai,那么交点数就是 n ( n − 1 ) 2 − ∑ i = 1 k a i ( a i − 1 ) 2 \frac{n(n-1)}{2}- \sum_{i=1}^k\frac{a_i(a_i-1)}{2} 2n(n−1)−∑i=1k2ai(ai−1),且 ∑ i = 1 p a i = n \sum_{i=1}^pa_{i}=n ∑i=1pai=n。因为如果n条直线两两相交,共有 n ( n − 1 ) 2 \frac{n(n-1)}{2} 2n(n−1) 个交点,每一组里是没有交点的,减去即可。
观察 n ( n − 1 ) 2 − ∑ i = 1 k a i ( a i − 1 ) 2 \frac{n(n-1)}{2}- \sum_{i=1}^k\frac{a_i(a_i-1)}{2} 2n(n−1)−∑i=1k2ai(ai−1),只要考虑 ∑ i = 1 k a i ( a i − 1 ) 2 \sum_{i=1}^k\frac{a_i(a_i-1)}{2} ∑i=1k2ai(ai−1)的所有取值,即可得到所有可能的交点数。
设 f ( n ) f(n) f(n)为 ∑ a i ( a i − 1 ) 2 \sum\frac{a_i(a_i-1)}{2} ∑2ai(ai−1) 取 n n n时 ∑ a i \sum a_i ∑ai 的最小值,有递推关系: f ( n ) = m i n { f ( n − k ( k − 1 ) 2 ) + k } f(n)=min\{f(n-\frac{k(k-1)}{2})+k\} f(n)=min{ f(n−2k(k−1))+k}。预处理f[],从 n ( n − 1 ) 2 \frac {n(n-1)}{2} 2n(n−1)倒序枚举j,判断j能否取到,能取到就输出 n ( n − 1 ) 2 − j \frac {n(n-1)}{2}-j 2n(n−1)−j。
完整代码:
(官方的标程个人感觉可读性不强)
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 3e7+5;
using namespace std;
int kase,n;
const int m=700;
int f[maxn];
int main()
{
FAST;
memset(f,INF,sizeof(f));
f[0]=0;
for (int i=1; i<=m*(m-1)/2; i++)
{
for (int k=2; k*(k-1)<=2*i; k++)
f[i]=min(f[i],f[i-k*(k-1)/2]+k);
}
cin>>kase;
while(kase--)
{
cin>>n;
for (int i=n*(n-1)/2; i>=1; i--)
if (f[i]<=n) cout<<n*(n-1)/2-i<<' ';
cout<<n*(n-1)/2<<endl;
}
return 0;
}
边栏推荐
- Win+sublime Text3 + go 1.9.2 environment setup diagram
- Byte/byte? Don't get dizzy!
- [network security officer] an attack technology that needs to be understood - high hidden and high persistent threats
- 重写?重载?你晕了吗?
- IS_ ERR()
- Shengdun technology joined dragon lizard community to build a new open source ecosystem
- A readme of an old open source person - how to do open source well
- Php+stripe payment API, the latest PHP version of stripe overseas payment tutorial
- Stream流创建_操作_收集_案例
- 性能优化专题
猜你喜欢

在ensp上做多出口服务器映射

模糊查询和聚合函数

Node cannot recognize the 'node' entry as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is correc

OpenCV每日函数 直方图相关(3)

C# 进程如何使用非静态方法

一个老开源人的自述-如何干好开源这件事
![Let you get started [uni app]](/img/e7/bc33d54a345901f67afa856769045f.png)
Let you get started [uni app]

嵌入式开发专业术语概念汇总

Detr: end to end object detection with transformers (from Li Mu and Zhu)

Servlet的生命周期
随机推荐
Shengdun technology joined dragon lizard community to build a new open source ecosystem
Win+sublime Text3 + go 1.9.2 environment setup diagram
Process status summary
scnprintf和snprintf的区别
Sound and shadow 2022 heavy release! Detailed explanation of the new functions of Huisheng Huiying 2022
Find the size of cosine
Shell中的单中括号和双中括号的区别
Embedded development terminology concept summary
==经典面试题
C语言刷题 | 三目运算实现判断大写(16)
How C processes use non static methods
一个老开源人的自述-如何干好开源这件事
微服务架构概述
Byte/byte? Don't get dizzy!
PHP seven methods to obtain complete instances of file name suffixes [collect]
PHP online common color comparison table
Unicode characters / static non static access
Overview of microservice architecture
Hoo Research Institute of Hufu: how does cosmos connect the chain with the "port" of the chain?
The difference between scnprintf and snprintf