题意简单,写题暴力。
但是曲折万分,请诸位大佬当笑话看吧……。
请看版本一。
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int mov[][2] = { 0,1,1,0,0,-1,-1,0 };
struct node
{
int x, y;
};
bool cmp (int a,int b)
{
return a > b;
}
int n, m;
int a[100005];
int mm[105][105];
bool check(node p)
{
if (p.x < 0 || p.x >= m || p.y < 0 || p.y >= n)
return false;
return true;
}
void dfs(node t,int cur,int dic)
{
for (int i = 0; i < 4; i++)
{
node p = t;
p.x += mov[dic%4][0];p.y += mov[dic%4][1];
if (check(p) && mm[p.x][p.y] == 0)
{
mm[p.x][p.y] = a[cur];
dfs(p, cur + 1,dic);
}
else
dic++;
}
}
int main()
{
int t;
while (cin >> t)
{
for(int i=0;i<t;i++)
cin >> a[i];
sort(a, a + t, cmp);
n = sqrt(t);
while (t%n)
{
n--;
}
m = t / n;
//cout << n << " " << m << endl;
node p;
p.x = p.y = 0;
mm[p.x][p.y] = a[0];
dfs(p,1,0);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << mm[i][j] << (j == n - 1 ? "\n" : " ");
}
}
}
//system("pause");
return 0;
}
因为正在学回溯,所以自然而然的想,诶这个题回溯能不能写啊,然后啪啦啪啦如上所示。其实还蛮开心的那个时候。之前还没有注释掉输出来的n和m全错了一次。
| 提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
|---|---|---|---|---|---|---|
| 2018/11/8 23:42:37 | 部分正确 | 24 | 1050 | C++ (g++) | 10 ms | 0413170432 |
| 测试点 | 结果 | 耗时 | 内存 |
|---|---|---|---|
| 0 | 答案正确 | 3 ms | 512KB |
| 1 | 答案正确 | 3 ms | 512KB |
| 2 | 答案正确 | 3 ms | 512KB |
| 3 | 答案正确 | 3 ms | 512KB |
| 4 | 答案正确 | 3 ms | 376KB |
| 5 | 答案正确 | 3 ms | 508KB |
| 6 | 答案正确 | 10 ms | 1132KB |
| 7 | 段错误 | 9 ms | 520KB |
看到这个结果后想想看也是最多有1e4,递归本来就比较慢,更何况这个题目又不是不能循环解决。能循环解决的事情干嘛要用递归呢,一边笑自己蠢一边改。呵呵呵,太天真了,那个时候万万想不到能有今天。
请看版本二
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int mov[][2] = { 0,1,1,0,0,-1,-1,0 };
struct node
{
int x, y;
};
bool cmp (int a,int b)
{
return a > b;
}
int n, m;
int a[1000005];
int mm[10005][10005];
bool check(node p)
{
if (p.x < 0 || p.x >= m || p.y < 0 || p.y >= n)
return false;
return true;
}
int main()
{
int t;
while (cin >> t)
{
for(int i=0;i<t;i++)
scanf("%d",&a[i]);
sort(a, a + t, cmp);
n = sqrt(t);
while (t%n)
{
n--;
}
m = t / n;
//cout << n << " " << m << endl;
node p;
p.x = p.y = 0;
mm[p.x][p.y] = a[0];
int dic = 0;
node tt = p;
for (int cur = 1; cur < t; cur++)
{
for (int i = 0; i < 4; i++)
{
node p = tt;
p.x += mov[dic % 4][0]; p.y += mov[dic % 4][1];
if (check(p) && mm[p.x][p.y] == 0)
{
mm[p.x][p.y] = a[cur];
tt = p;
break;
}
else
dic++;
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d", mm[i][j]);
printf(j == n - 1 ? "\n" : " ");
}
}
}
//system("pause");
return 0;
}
| 提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
|---|---|---|---|---|---|---|
| 2018/11/8 23:52:07 | 部分正确 | 24 | 1050 | C++ (g++) | 37 ms | 0413170432 |
| 测试点 | 结果 | 耗时 | 内存 |
|---|---|---|---|
| 0 | 答案正确 | 9 ms | 432KB |
| 1 | 答案正确 | 21 ms | 404KB |
| 2 | 答案正确 | 9 ms | 512KB |
| 3 | 答案正确 | 21 ms | 444KB |
| 4 | 答案正确 | 20 ms | 612KB |
| 5 | 答案正确 | 23 ms | 384KB |
| 6 | 答案正确 | 37 ms | 876KB |
| 7 | 运行超时 | 0 ms | 0KB |
运行超时了……超时了耶……因为循环四次就表示四个方向都判断过了嘛……非常ok(然而并不需要)。
其实这个时候我们就可以想到因为他是顺时针旋转,所以我们只要判断顺时针的方向是否可以就行了,如果不行也就该出循环了。
之后我又尝试了各种方法,包括紫书上的,对,我是先扩大了mm数组的大小,然后用的紫书,这将是我将要讲的第二件事情。第一件事情是关于变量定义数组的事情。
这个时候我心态已然完全炸裂,网上各种找博客,然后复制粘贴上交,然后我看到一个和我写的很像,但是区别仅有用变量开数组的博客,一交全对,然后我缓缓的把我的代码改成和他的代码类似的样子,改一块,上交一次,然后结果依然和上边的没有变化。
同志们,到这里你们就可以看出来这个案例7该有多么的妖孽!
此时已经接近凌晨1点,为了不打扰室友休息,无奈只能第二天继续,然后一整个晚上就没睡好。
一起床我就开始着急这个玩意,这个我把人家的往我的上改,改一次交一次,然而只改动了数组的变量改成常量偏偏就错了。
ok我们知道了是数组在作怪,那么为什么数组可以用变量定义呢。
C99加了……也就是说一些PTA的G++编译器确实是让过的……好的我们再看clang++,也可以,再看杭电的C++不可以。
惊了!这是什么操作。
那么我想紫书上的代码应当算是相当高明的,为什么也会超时,然后我照着一位博主的博客又敲了一遍,案例7依然超时。
这是为什么呢。(其实我们从这张可以ac的图片上我们也可以看出这个案例7有多么妖孽……)


王德发!数组大小还能影响时间的吗?我的天哪!!
基础差的哭出了声我的天哪……还有这种事情orz
我这一整个晚上和一整个白天在纠结点什么……
好的,今天本咸鱼的丢人故事就讲到这里了,我震惊了,你呢?
最后把AC的代码发一下吧……题目是真的水,案例是真的妖孽。
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp(int a, int b)
{
return a > b;
}
int a[100005];
int mm[10005][1005];
int main()
{
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
scanf("%d", &a[i]);
sort(a, a + t, cmp);
int n, m;
n = sqrt(t);
while (t%n)
{
n--;
}
m = t / n;
int cur = 0;
int x, y;
mm[1][1] = a[cur];
x = 1;
y = 1;
while (cur < t-1)
{
while (!mm[x][y + 1] && y+1 <= n)
mm[x][++y] = a[++cur];
while (!mm[x+1][y] && x+1 <= m)
mm[++x][y] = a[++cur];
while (!mm[x-1][y] && x-1 > 0)
mm[--x][y] = a[++cur];
while (!mm[x][y-1] && y-1 > 0)
mm[x][--y] = a[++cur];
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
printf("%d", mm[i][j]);
printf(j==n?"\n":" ");
}
}
system("pause");
}
本文记录了作者在PTA平台解决编程挑战时,因数组定义方式不同导致程序效率变化的经历。从递归到循环,再到最终的AC代码,详细展示了如何优化算法以提高效率,特别关注于数组大小对程序运行时间的影响。
3039

被折叠的 条评论
为什么被折叠?



