飞机降落
过了20%,第一想法是贪心,后面发现不行,后面的代码是在最开始这个思路上建立的
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct plane
{
int s,e;
int d;
bool operator<(const plane& other) const
{
if(e!=other.e) return e<other.e;
else return s<other.s;
}
}plane;
int n;
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
plane p[n];
for(int i=0;i<n;i++)
{
cin >> p[i].s >> p[i].e >> p[i].d;
p[i].e = p[i].s+p[i].e;
}
sort(p,p+n);
int t = 0;
int flag = 1;
for(int i=0;i<n;i++)
{
int S = p[i].s;
int E = p[i].e;
int D = p[i].d;
if(t<S) t = S;
if(t>E) {flag=0;break;}
t += D;
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
改成爆搜,能过全部数据
#include<iostream>
#include<cstring>
using namespace std;
const int maxN = 15;
typedef struct plane
{
int s,e;
int d;
}plane;
int n;
plane p[maxN];
int vis[maxN];
int flag;
void dfs(int u,int last)
{
if(u==n)
{
flag = 1;
return;
}
for(int i=0;i<n;i++)
{
int S = p[i].s;
int E = p[i].e;
int D = p[i].d;
if(!vis[i]&&last<=E)
{
vis[i] = 1;
dfs(u+1,max(last+D,S+D));
vis[i] = 0;
}
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i=0;i<n;i++)
{
cin >> p[i].s >> p[i].e >> p[i].d;
p[i].e = p[i].s+p[i].e;
}
memset(vis,0,sizeof vis);
flag = 0;
dfs(0,0);
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
分析:
爆搜
没做出来原因:
第一眼看是贪心,然后想当然写了。想象一根从0开始很长的数轴,飞机的下降过程是一条可移动的短线段,T D L,它的头只能在[T,T+D]内放。然后按右端点所有分级排序,最早结束的先下降。我觉得思路没问题,但问gpt思路哪里有问题,gpt说万一遇到右端点重合的,这时候要同时考虑T和L。
然后看其他人代码都是dfs,emmm,一看N最大才10,直接枚举,得到飞机下的顺序,然后就好做了。