插入排序:前半部分有序后半部分无序
归并排序:每一个小序列是有序的
#include
#include
#include
using namespace std;
int n;
vectorlast , now;
bool check()
{
for(int i = now.size() - 1;i >= 0;i --)
{
if(now[i] != last[i])
{
if(i >= 0)
{
if(now[i - 1] > now[i]) return false;
}
}
}
return true;
}
void merge()
{
puts("Merge Sort");
bool f = 0;
for (int step = 2;step / 2 < n;step *= 2)
{
for(int i = 0;i < n;i += step)
sort(last.begin() + i , last.begin() + min(i + step, n));
if (f)
{
cout << last[0];
for (int k = 1; k < n;k ++) cout << " " << last[k];
break;
}
if (last == now) f = 1;
}
}
void Insert()
{
puts("Insertion Sort");
bool f = 0;
for(int i = 2;i <= n;i ++)
{
sort(last.begin() , last.begin() + i);
if(f)
{
for(int i = 0;i < n;i ++)
{
if(i) cout << " ";
cout << last[i];
}
break;
}
if(last == now) f = 1;
}
}
int main()
{
cin >> n;
for(int i = 0;i < n;i ++)
{
int x;
cin >> x;
last.push_back(x);
}
for(int i = 0;i < n;i ++)
{
int x;
cin >> x;
now.push_back(x);
}
if(check()) Insert();
else merge();
return 0;
}
三维广搜
#include
#include
#include
#include
using namespace std;
const int N = 1e5 + 10;
int m , n , l , t;
int g[60][200][200];
bool st[60][200][200];
int res = 0;
int dx[6] = {0 , 0 , 1 , -1 , 0 , 0};
int dy[6] = {1 , -1 , 0 , 0 , 0 , 0};
int dz[6] = {0 , 0 , 0 , 0 , 1 , -1};
struct point
{
int x , y , z;
};
void bfs(point p)
{
queueq;
q.push(p);
int w = 0;
st[p.z][p.x][p.y] = true;
while(!q.empty())
{
auto t = q.front();
q.pop();
st[t.z][t.x][t.y] = true;
w ++;
for(int i = 0;i < 6;i ++)
{
int tx = t.x + dx[i] , ty = t.y + dy[i] , tz = t.z + dz[i];
if(tx < 0 || tx >= m || ty < 0 || ty >= n || tz < 0 || tz >= l) continue;
if(st[tz][tx][ty] || !g[tz][tx][ty]) continue;
st[tz][tx][ty] = true;
q.push({tx , ty , tz});
}
}
if(w >= t) res += w;
}
int main()
{
memset(st , 0 , sizeof st);
cin >> m >> n >> l >> t;
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int h = 0;h < n;h ++)
cin >> g[i][j][h];
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int h = 0;h < n;h ++)
if(!st[i][j][h] && g[i][j][h])
{
point p = {j , h , i};
bfs(p);
}
cout << res << endl;
return 0;
}
dfs+bfs
#include
#include
#include
#include
#include
using namespace std;
const int N = 110;
int n;
int l[N] , r[N] , w[N];
int tree_w[N] , cnt = 0;
void dfs(int u)
{
if(l[u] != -1) dfs(l[u]);
tree_w[u] = w[cnt ++];
if(r[u] != -1) dfs(r[u]);
}
int main()
{
memset(l , -1 , sizeof l);
memset(r , -1 , sizeof r);
cin >> n;
for(int i = 0;i < n;i ++)
{
int x , y;
cin >> x >> y;
if(x != -1) l[i] = x;
if(y != -1) r[i] = y;
}
for(int i = 0;i < n;i ++) cin >> w[i];
sort(w , w + n);
dfs(0);
queueq;
bool is_first = true;
q.push(0);
while(!q.empty())
{
int t = q.front();
q.pop();
if(!is_first) cout << " ";
is_first = false;
cout << tree_w[t];
if(l[t] != -1) q.push(l[t]);
if(r[t] != -1) q.push(r[t]);
}
return 0;
}
多亏了柳神,我才能写出来,真恶心的模拟
#include
#include
#include
#include
#include
#include