PTA甲级
1089 Insert or Merge
插入排序:前半部分有序后半部分无序
归并排序:每一个小序列是有序的
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n;
vector<int>last , 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;
}
1091 Acute Stroke
三维广搜
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
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)
{
queue<point>q;
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;
}
1099 Build A Binary Search Tree
dfs+bfs
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
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);
queue<int>q;
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;
}
1095 Cars on Campus
多亏了柳神,我才能写出来,真恶心的模拟
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;
struct node {
char id[10];
int time, flag = 0;
};
bool cmp1(node a, node b) {
if(strcmp(a.id, b.id) != 0)
return strcmp(a.id, b.id) < 0;
else
return a.time < b.time;
}
bool cmp2(node a, node b) {
return a.time < b.time;
}
int main() {
int n, k, maxtime = -1, tempindex = 0;
scanf("%d%d\n", &n, &k);
vector<node> record(n), car;
for(int i = 0; i < n; i++) {
char temp[5];
int h, m, s;
scanf("%s %d:%d:%d %s\n", record[i].id, &h, &m, &s, temp);
int temptime = h * 3600 + m * 60 + s;
record[i].time = temptime;
record[i].flag = strcmp(temp, "in") == 0 ? 1 : -1;
}
sort(record.begin(), record.end(), cmp1);
map<string, int> mapp;
for(int i = 0; i < n - 1; i++) {
if(strcmp(record[i].id, record[i+1].id) == 0 && record[i].flag == 1 && record[i+1].flag == -1) {
car.push_back(record[i]);
car.push_back(record[i+1]);
mapp[record[i].id] += (record[i+1].time - record[i].time);
if(maxtime < mapp[record[i].id]) {
maxtime = mapp[record[i].id];
}
}
}
sort(car.begin(), car.end(), cmp2);
vector<int> cnt(n);
for(int i = 0; i < car.size(); i++) {
if(i == 0)
cnt[i] += car[i].flag;
else
cnt[i] = cnt[i - 1] + car[i].flag;
}
for(int i = 0; i < k; i++) {
int h, m, s;
scanf("%d:%d:%d", &h, &m, &s);
int temptime = h * 3600 + m * 60 + s;
int j;
for(j = tempindex; j < car.size(); j++) {
if(car[j].time > temptime) {
printf("%d\n", cnt[j - 1]);
break;
} else if(j == car.size() - 1) {
printf("%d\n", cnt[j]);
}
}
tempindex = j;
}
for(map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
if(it->second == maxtime)
printf("%s ", it->first.c_str());
}
printf("%02d:%02d:%02d", maxtime / 3600, (maxtime % 3600) / 60, maxtime % 60);
return 0;
}