Submission #1836952
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)
#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))
#define SZ(a) (int) a.size()
#define ALL(a) a.begin(),a.end()
#define PB push_back
#define MP make_pair
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const LL INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;
const int MAX = 2020;
VI g[MAX];
int A[MAX];
int U[MAX];
bool cmp(int x, int y)
{
return A[x] < A[y];
}
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a%b);
}
VI merge(VI& a, VI& b)
{
VI res;
res.reserve(SZ(a) + SZ(b));
a.PB(-INF);
int ind = 0;
FOR (i, 0, SZ(a))
{
while(ind < SZ(b) && b[ind] > a[i])
{
res.PB(b[ind]);
ind++;
}
if (i == SZ(a) - 1) break;
res.PB(a[i]);
}
return res;
}
VI dfs(int x)
{
U[x] = true;
VI res;
FOR (i, 0, SZ(g[x]))
{
int to = g[x][i];
if (U[to]) continue;
VI v = dfs(to);
res = merge(res, v);
}
reverse(ALL(res));
res.PB(A[x]);
reverse(ALL(res));
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
//ios::sync_with_stdio(false); cin.tie(0);
int n;
scanf("%d", &n);
FOR (i, 0, n)
{
scanf("%d", &A[i]);
}
FOR (i, 0, n)
{
FOR (j, 0, n)
{
if (i == j) continue;
if (gcd(A[i], A[j]) != 1) g[i].PB(j);
}
}
FOR (i, 0, n)
{
sort(ALL(g[i]));
}
VI res;
FOR (i, 0, n)
{
if (U[i]) continue;
VI v = dfs(i);
res = merge(res, v);
}
FOR (i, 0, SZ(res))
{
printf("%d ", res[i]);
}
printf("\n");
}
Submission Info
Submission Time |
|
Task |
E - Rearranging |
User |
vjudge5 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
1705 Byte |
Status |
WA |
Exec Time |
522 ms |
Memory |
16000 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:89:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
./Main.cpp:92:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 1600 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample1.txt, sample2.txt |
All |
sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt |
Case Name |
Status |
Exec Time |
Memory |
in1.txt |
WA |
521 ms |
9472 KB |
in10.txt |
WA |
522 ms |
9984 KB |
in11.txt |
WA |
435 ms |
1536 KB |
in12.txt |
WA |
437 ms |
1536 KB |
in13.txt |
WA |
434 ms |
1536 KB |
in14.txt |
WA |
437 ms |
1536 KB |
in15.txt |
WA |
436 ms |
1536 KB |
in16.txt |
WA |
462 ms |
16000 KB |
in17.txt |
WA |
463 ms |
16000 KB |
in18.txt |
WA |
462 ms |
16000 KB |
in19.txt |
WA |
466 ms |
16000 KB |
in2.txt |
WA |
520 ms |
9472 KB |
in20.txt |
WA |
463 ms |
16000 KB |
in21.txt |
WA |
333 ms |
384 KB |
in22.txt |
WA |
334 ms |
384 KB |
in23.txt |
WA |
336 ms |
384 KB |
in24.txt |
WA |
336 ms |
384 KB |
in25.txt |
WA |
334 ms |
384 KB |
in26.txt |
WA |
436 ms |
1536 KB |
in27.txt |
WA |
436 ms |
1536 KB |
in28.txt |
WA |
433 ms |
1536 KB |
in29.txt |
WA |
437 ms |
1536 KB |
in3.txt |
WA |
522 ms |
9216 KB |
in30.txt |
WA |
436 ms |
1536 KB |
in31.txt |
AC |
1 ms |
256 KB |
in32.txt |
AC |
1 ms |
256 KB |
in33.txt |
AC |
1 ms |
256 KB |
in34.txt |
AC |
387 ms |
384 KB |
in35.txt |
AC |
386 ms |
384 KB |
in36.txt |
WA |
459 ms |
2944 KB |
in37.txt |
WA |
458 ms |
2944 KB |
in38.txt |
WA |
459 ms |
2944 KB |
in39.txt |
WA |
461 ms |
3072 KB |
in4.txt |
WA |
521 ms |
9216 KB |
in40.txt |
WA |
458 ms |
2944 KB |
in41.txt |
WA |
462 ms |
3072 KB |
in42.txt |
WA |
461 ms |
2944 KB |
in43.txt |
WA |
458 ms |
3072 KB |
in44.txt |
WA |
459 ms |
2944 KB |
in45.txt |
WA |
460 ms |
2944 KB |
in5.txt |
WA |
519 ms |
9088 KB |
in6.txt |
WA |
521 ms |
9728 KB |
in7.txt |
WA |
521 ms |
9984 KB |
in8.txt |
WA |
521 ms |
9600 KB |
in9.txt |
WA |
520 ms |
9216 KB |
sample1.txt |
AC |
1 ms |
256 KB |
sample2.txt |
AC |
1 ms |
256 KB |