每日三百行代码 第二十四天
#include<bits/stdc++.h>
using namespace std;
const int maxn = 30;
int a[maxn + 10], b[maxn + 10];
map<int, int>L, R;
int build(int la, int ra, int lb, int rb) {
if (la > ra)
return 0;
int root = a[ra];
int i;
for (i = lb; i <= rb && b[i] != root; i++) {
;}
if (i <= rb) {
R[root] = build(ra - rb + i, ra - 1, i + 1, rb);
L[root] = build(la, ra - rb + i - 1, lb, i - 1);
}
return root;
}
void bfs(int root) {
queue<int> Q;
Q.push(root);
int cnt = 0;
while (!Q.empty()) {
int tn = Q.front();
Q.pop();
printf(cnt++ == 0 ? "%d" : " %d", tn);
if (L[tn])
Q.push(L[tn]);
if (R[tn])
Q.push(R[tn]);
}
puts("");
}
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= N; i++)
scanf("%d", &b[i]);
int root = build(1, N, 1, N);
bfs(root);
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int data;
Node* lc, * rc;
};
Node* buildTree(int* post, int* in, int n) {
if (n == 0)
return NULL;
int* mid = in;
while (*(post + n - 1) != *mid)mid++;
Node* T = new Node;
T->data = *mid;
int m = mid - in;
T->lc = buildTree(post, in, m);
T->rc = buildTree(post + m, mid + 1, n - 1 - m);
return T;
}
int main() {
queue<Node*>q;
int N, post[30], in[30], vis[30];
cin >> N;
for (int i = 0; i < N; ++i)
cin >> post[i];
for (int i = 0; i < N; ++i)
cin >> in[i];
q.push(buildTree(post, in, N));
int i = 0;
while (!q.empty()) {
cout << (i++ ? " " : "") << q.front()->data;
if (q.front()->lc)q.push(q.front()->lc);
if (q.front()->rc)q.push(q.front()->rc);
q.pop();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct node{
int upset,squ,cnt;
node(){
cnt = 1;
upset = 0;
squ = 0;
}
}s[10005];
struct GG{
double cnt,upset,squ;
int id;
};
int pre[10005];
bool flag[10005];
int find(int x){
if( x == pre[x])
return x;
return pre[x] = find(pre[x]);
}
void merge(int x, int y){
int fx = find(x);
int fy = find(y);
if(fx > fy)
pre[fx] = fy;
else if(fx < fy)
pre[fy] = fx;
return;
}
bool cmp(GG a, GG b){
if(a.squ != b.squ)
return a.squ > b.squ;
else
return a.id < b.id;
}
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i <= 10000; i ++)
pre[i] = i;
int me, fa, mo, cnt, child;
for(int i = 1; i <= n; i ++)
{
scanf("%d %d %d",&me,&fa,&mo);
flag[me] = true;
if(fa != -1)
{
merge(me, fa);
flag[fa] = true;
}
if(mo != -1)
{
merge(me, mo);
flag[mo] = true;
}
scanf("%d",&cnt);
for(int j = 1; j <= cnt; j ++ )
{
scanf("%d",&child);
merge(child, me);
flag[child] = true;
}
scanf("%d %d",&s[me].upset, &s[me].squ);
}
set<int>st;
for(int i = 10000; i >= 0; i--)
{
if(flag[i] == true)
{
int x = find(i);
st.insert(x);
if(x != i)
{
s[x].cnt += s[i].cnt;
s[x].squ += s[i].squ;
s[x].upset += s[i].upset;
}
}
}
set<int>::iterator it = st.begin();
vector<GG>vec;
while(it!=st.end())
{
GG gg;
gg.id = *it;
gg.cnt = s[*it].cnt;
gg.squ =s[*it].squ * 1.0 / s[*it].cnt * 1.0;
gg.upset = s[*it].upset * 1.0 / s[*it].cnt * 1.0;
vec.push_back(gg);
it++;
}
sort(vec.begin(),vec.end(),cmp);
printf("%d\n",vec.size());
for(int i = 0 ; i < vec.size(); i++)
printf("%04d %.0lf %.3lf %.3lf\n",vec[i].id, vec[i].cnt,vec[i].upset, vec[i].squ);
return 0;
}
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 1e5 + 10;
struct node {
int id, houses;
double area;
};
struct family {
int id, members;
double ave_houses, ave_area;
};
bool cmp(const family &a, const family &b) {
if (a.ave_area != b.ave_area) return a.ave_area > b.ave_area;
return a.id < b.id;
}
int pre[maxn];
int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
int union0(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) {
pre[fx] = fy;
return 1;
}
return 0;
}
int main() {
for (int i = 0; i < maxn; i++) pre[i] = i;
set<int> v;
map<int, node> nodes;
int n;
cin >> n;
while (n--) {
int id, p1, p2, k, houses;
double area;
cin >> id >> p1 >> p2 >> k;
v.insert(id);
if (p1 != -1) {
v.insert(p1);
union0(p1, id);
}
if (p2 != -1) {
v.insert(p2);
union0(p2, id);
}
while (k--) {
int id1;
cin >> id1;
v.insert(id1);
union0(id1, id);
}
cin >> houses >> area;
nodes[id] = node{
id, houses, area};
}
set<int> sset;
map<int, vector<int> > mmap;
for (auto it = v.begin(); it != v.end(); it++) {
int fa = find(*it);
mmap[fa].push_back(*it);
sset.insert(fa);
}
cout << sset.size() << endl;
vector<family> families;
for (auto it = sset.begin(); it != sset.end(); it++) {
int fa = find(*it), sz = mmap[fa].size();
double hs = 0, area = 0;
for (int i = 0; i < sz; i++) {
hs += nodes[mmap[fa][i]].houses;
area += nodes[mmap[fa][i]].area;
}
families.push_back(family{
mmap[fa][0], sz, hs / sz, area / sz});
}
sort(families.begin(), families.end(), cmp);
for (int i = 0; i < families.size(); i++) {
printf("%04d %d %.3lf %.3lf", families[i].id, families[i].members, families[i].ave_houses, families[i].ave_area);
if (i < families.size() - 1) puts("");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
getline(cin,s);
int ans=-1;
for(int i=0;i<s.size();i++)
{
for(int j=s.size()-1;j>=i;j--)
{
int l=i,r=j;
while(s[l]==s[r] && l<=r)
{
l++;
r--;
}
if(l>r)
{
ans=max(ans,j-i+1);
}
}
}
cout<<ans;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string s;
char p[5000];
int len[5000];
void init()
{
p[0]='@';
int l=s.length();
for(int i=1;i<=l*2;i+=2)
{
p[i]='#';p[i+1]=s[i/2];
}
p[l*2+1]='#';
p[l*2+2]='!';
}
int main()
{
ios::sync_with_stdio(false);
getline(cin,s);
init();
int po=0,mx=0,maxn=0,l;
l=strlen(p);
for(int i=1;i<=l;i++)
{
if(i<mx) len[i]=min(len[2*po-i],mx-i);
else len[i]=1;
while(p[i-len[i]]==p[i+len[i]])
len[i]++;
if(i+len[i]>mx)
{
mx=i+len[i];po=i;
}
maxn=max(maxn,len[i]);
}
cout<<maxn-1;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct T{
int id;
int money;
int sumHb;
};
auto cmp=[](T &e1,T &e2){
return tie(e2.money,e2.sumHb,e1.id)<tie(e1.money,e1.sumHb,e2.id);
};
int main(){
int N;
cin>>N;
map<int,int>val;
map<int,int>hb;
vector<int>flag;
for(int i=1;i<=N;i++){
int num,sum=0;
cin>>num;
for(int j=1;j<=num;j++){
int id,mon;
cin>>id>>mon;
if(count(flag.begin(),flag.end(),id)==0){
sum+=mon;
val[id]+=mon;
hb[id]++;
flag.push_back(id);
}
}
val[i]-=sum;
flag.clear();
}
vector<T>ss;
for(auto item : val){
ss.push_back({
item.first,item.second,hb[item.first]});
}
sort(ss.begin(),ss.end(),cmp);
for(int i=0;i<ss.size();i++){
printf("%d %.2f",ss[i].id,(ss[i].money*1.0)/100);
if(i+1!=ss.size())
cout<<endl;
}
return 0;
}
#include<cstdio>
#include<iostream>
using namespace std;
#include<algorithm>
struct node
{
int id;
int num;
int value;
}person[10000];
bool f(struct node a,struct node b)
{
if(a.value!=b.value)
{
return a.value>b.value;
}
else if(a.num != b.num)
{
return a.num>b.num;
}
else if(a.id != b.id)
{
return a.id<b.id;
}
}
int main()
{
int n,k,p,i,j,sum=0,n1;
cin>>n;
for(i=1;i<=n;i++)
{
person[i].id = i;
cin>>k;
sum=0;
for(j=1;j<=k;j++)
{
cin>>n1>>p;
person[n1].value = person[n1].value + p;
person[n1].num ++;
sum = sum + p;
}
person[i].value = person[i].value - sum;
}
sort(person+1,person+n+1,f);
for(i=1;i<=n;i++)
{
printf("%d %.2lf\n",person[i].id,person[i].value*1.0/100);
}
}
#include<iostream>
using namespace std;
int relative[101][101];
int fa[101];
int find(int x) {
return fa[x] == x ? x : find(fa[x]);
}
void unin(int x, int y) {
int a = find(x), b = find(y);
fa[b] = a;
return;
}
int main() {
for (int i = 0; i < 101; i++) {
fa[i] = i;
}
int N, M, K;
cin >> N >> M >> K;
for (int i = 0; i < M; i++) {
int x, y, edge;
cin >> x >> y >> edge;
if (edge == 1)
unin(x, y);
else {
relative[x][y] = -1;
relative[y][x] = -1;
}
}
for (int i = 0; i < K; i++) {
int x, y;
cin >> x >> y;
if (find(x) == find(y) && relative[x][y] != -1)
cout << "No problem"<<endl;
else if (find(x) != find(y) && relative[x][y] != -1)
cout << "OK"<<endl;
else if (find(x) == find(y) && relative[x][y] == -1)
cout << "OK but..."<<endl;
else
cout << "No way"<<endl;
}
return 0;
}
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int pre[35];
int in[35];
typedef struct node* bintree;
queue<bintree> q;
struct node{
bintree left;
bintree right;
int data;
};
bintree huanyuan(int prel,int prer,int inl,int inr)
{
if(prel>prer)return NULL;
bintree bt=new node;
bt->data=pre[prel];
int k;
for(int i=inl;i<=inr;i++)
{
if(in[i]==pre[prel])
{
k=i;
break;
}
}
bt->right=huanyuan(prel+1,prel+k-inl,inl,k-1);
bt->left=huanyuan(prel+k-inl+1,prer,k+1,inr);
return bt;
}
void layeror(bintree bt,vector<int> &vec)
{
if(bt==NULL)return;
q.push(bt);
while(!q.empty())
{
vec.push_back(q.front()->data);
bintree t=q.front();
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>in[i];
}
for(int i=0;i<n;i++)
{
cin>>pre[i];
}
bintree bt=huanyuan(0,n-1,0,n-1);
vector<int> vec;
layeror(bt,vec);
for(int i=0;i<vec.size();i++)
{
cout<<vec[i];
if(i!=vec.size()-1)
cout<<' ';
}
}
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
vector<int>in,pre,level(1000000,-1);
void levelorder(int root,int start,int end,int index)
{
if(start>end) return ;
int i=start;
while(i<end&&pre[root]!=in[i]) i++;
level[index]=pre[root];
levelorder(root+1,start,i-1,2*index+2);
levelorder(root+1+i-start,i+1,end,2*index+1);
}
int main()
{
cin>>n;
in.resize(n); pre.resize(n);
for(int i=0;i<n;i++) cin>>in[i];
for(int i=0;i<n;i++) cin>>pre[i];
levelorder(0,0,n-1,0);
for(int i=0;i<1000000;i++)
{
if(level[i]==-1) continue;
cnt++;
if(i) cout<<" ";
cout<<level[i];
if(cnt==n) break;
}
}
#include <bits/stdc++.h>
using namespace std;
int cnt;
int a[1005];
void build(int x)
{
int t=cnt;
a[cnt]=x;
cnt++;
while(t>1&&(a[t/2]>a[t]))
{
a[t]=a[t/2];
a[t/2]=x;
t=t/2;
}
a[t]=x;
}
int main()
{
int n,m,x,y;
string s;
while(cin>>n>>m)
{
cnt=1;
for(int i=0;i<n;i++)
{
cin>>x;
build(x);
}
map<int,int>mp;
for(int i=1;i<=n;i++) mp[a[i]]=i;
for(int i=0;i<m;i++)
{
cin>>x>>s;
if(s[0]=='a')
{
cin>>y; cin>>s; cin>>s;
if(mp[x]/2==mp[y]/2) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else
{
cin>>s;
if(s[0]=='a')
{
cin>>s; cin>>s; cin>>y;
if(mp[x]/2==mp[y]) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else
{
cin>>s;
if(s[0]=='r')
{
if(mp[x]==1) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else
{
cin>>s>>y;
if(mp[x]==mp[y]/2) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
}
}
}
return 0;
}