问题描述:
试题编号: | 201809-4 |
试题名称: | 再卖菜 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 在一条街上有n个卖菜的商店,按1至n的顺序排成一排,这些商店都卖一种蔬菜。 输入格式 输入的第一行包含一个整数n,表示商店的数量。 输出格式 输出一行,包含n个正整数,依次表示每个商店第一天的菜价。 样例输入 8 样例输出 2 2 2 1 6 5 16 10 数据规模和约定 对于30%的评测用例,2<=n<=5,第二天每个商店的菜价为不超过10的正整数; |
非常鬼畜,又和卖菜相关。
一开始看到题,感觉非常复杂,毫无头绪,写完(水完)第五题后回头看时突然想到dfs。
最终考场上拿了50分。
今天一开始做只拿了20分
20分代码:
CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
int n,d1[301],d2[301];
void dfs(int step)
{
if(step==n)
{
for(int money=1;money<=199;money++)
{
d1[n]=money;
if(int((d1[n-1]+d1[n])/2)>d2[n]) return;
if(int((d1[n-1]+d1[n])/2)==d2[n])
{
for(int j=1;j<=n;j++)
cout<<d1[j]<<' ';
exit(0);
}
}
return;
}
for(int money=1;money<=298;money++)
{
d1[step]=money;
if(step==1) dfs(step+1);
else if(step==2)
{
if(int((d1[1]+d1[2])/2)<d2[1]) continue;
else if((int((d1[1]+d1[2])/2)==d2[1])) dfs(step+1);
else if(int((d1[1]+d1[2])/2)>d2[1]) return;
}
else
{
if(int((d1[step-2]+d1[step-1]+d1[step])/3)<d2[step-1]) continue;
else if(int((d1[step-2]+d1[step-1]+d1[step])/3)==d2[step-1]) dfs(step+1);
else if(int((d1[step-2]+d1[step-1]+d1[step])/3)>d2[step-1]) return;
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>d2[i];
mem(d1,0);
dfs(1);
}
到晚上仔细思考了一下,原因是枚举最后一家商店第一天菜钱后,影响到的应该是倒数第二家和最后一家商店的菜钱
20分的代码只考虑到了对最后一家的影响。改了后我以为是50分,没想到!!!
嗯嗯嗯???这就80分了??一失足成千古恨,现在也记不清考场上漏了啥。只差那30就上400了呀!我tm
80分代码:
CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
int n,d1[301],d2[301];
void dfs(int step)
{
if(step==n)
{
for(int money=1;money<=199;money++)
{
d1[n]=money;
if(int((d1[n-1]+d1[n])/2)>d2[n]||int((d1[n-2]+d1[n-1]+d1[n])/3)>d2[n-1]) return;
if(int((d1[n-1]+d1[n])/2)==d2[n]&&int((d1[n-2]+d1[n-1]+d1[n])/3)==d2[n-1])
{
for(int j=1;j<=n;j++)
cout<<d1[j]<<' ';
exit(0);
}
}
return;
}
for(int money=1;money<=298;money++)
{
d1[step]=money;
if(step==1) dfs(step+1);
else if(step==2)
{
if(int((d1[1]+d1[2])/2)<d2[1]) continue;
else if((int((d1[1]+d1[2])/2)==d2[1])) dfs(step+1);
else if(int((d1[1]+d1[2])/2)>d2[1]) return;
}
else
{
if(int((d1[step-2]+d1[step-1]+d1[step])/3)<d2[step-1]) continue;
else if(int((d1[step-2]+d1[step-1]+d1[step])/3)==d2[step-1]) dfs(step+1);
else if(int((d1[step-2]+d1[step-1]+d1[step])/3)>d2[step-1]) return;
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>d2[i];
mem(d1,0);
dfs(1);
}
至于满分的话,有用记忆化搜索的,有用差分约束的。
哭辽