Contest1657 - 2019年我能变强组队训练赛第十四场
Similarity of Subtrees
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=100100;
ull ha[maxn];
vector<int>E[maxn];
unordered_map<ull,int>m;
void dfs(int u,int fa) {
ha[u] = 1;
for (auto v:E[u]) {
dfs(v, u);
ha[u] = ha[u] + ha[v] *131;
}
m[ha[u]] ++;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1,u,v; i < n; i++) {
scanf("%d%d", &u, &v);
E[u].push_back(v);
}
dfs(1, 0);
ll ans = 0;
for (unordered_map<ull, int>::iterator it = m.begin(); it != m.end(); it++) {
ll k = it->second;
ans += k * (k - 1) / 2;
}
printf("%lld
", ans);
return 0;
}
We don't wanna work!
#include <bits/stdc++.h>
using namespace std;
const int maxn=100010;
int n,m,nn;
struct node {
string na;
int x, ti;
}a[maxn],tmp;
char op;
bool cmp(node a,node b) {
if (a.x == b.x) return a.ti > b.ti; else return a.x > b.x;
}
struct cmp1 {
bool operator()(const node a, const node b) const {
if (a.x == b.x) return a.ti < b.ti; else return a.x < b.x;
}
};
struct cmp2 {
bool operator()(const node a, const node b) const {
if (a.x == b.x) return a.ti > b.ti; else return a.x > b.x;
}
};
set<node,cmp1>s1;
set<node,cmp2>s2;
map<string,node>mp;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].na >> a[i].x;
a[i].ti = i;
mp[a[i].na] = a[i];
}
nn = n;
sort(a + 1, a + n + 1, cmp);
int n2 = n * 0.2;
for (int i = 1; i <= n2; i++) {
s1.insert(a[i]);
}
for (int i = n2 + 1; i <= n; i++) {
s2.insert(a[i]);
}
cin >> m;
for (int i = nn + 1; i <= nn + m; i++) {
cin >> op;
string s;
if (op == '-') {
cin >> s;
tmp = mp[s];
if (s1.erase(tmp)) n2--;
s2.erase(tmp);
if (n2 > (int) ((n-1) * 0.2)) {
n2--;
tmp = *s1.begin();
s1.erase(tmp);
s2.insert(tmp);
cout << tmp.na << " is not working now." << endl;
}
n--;
if (n2 < (int) (n * 0.2)) {
n2++;
tmp = *s2.begin();
s2.erase(tmp);
s1.insert(tmp);
cout << tmp.na << " is working hard now." << endl;
}
} else {
cin >> a[i].na >> a[i].x;
a[i].ti = i;
mp[a[i].na] = a[i];
if (n2 < (int) ((n + 1) * 0.2)) {
tmp = *s2.begin();
if ((a[i].x > tmp.x) || (a[i].x == tmp.x && a[i].ti > tmp.ti)) {
s1.insert(a[i]);
cout << a[i].na << " is working hard now." << endl;
} else {
s2.erase(tmp);
s1.insert(tmp);
s2.insert(a[i]);
cout << a[i].na << " is not working now." << endl;
cout << tmp.na << " is working hard now." << endl;
}
n2++;
} else {
if (n2 != 0) {
tmp = *s1.begin();
if ((a[i].x > tmp.x) || (a[i].x == tmp.x && a[i].ti > tmp.ti)) {
s1.erase(tmp);
s1.insert(a[i]);
s2.insert(tmp);
cout << a[i].na << " is working hard now." << endl;
cout << tmp.na << " is not working now." << endl;
} else {
s2.insert(a[i]);
cout << a[i].na << " is not working now." << endl;
}
} else {
tmp = *s2.begin();
if ((int) (0.2 * (n + 1)) > 0) {
if ((a[i].x > tmp.x) || (a[i].x == tmp.x && a[i].ti > tmp.ti)) {
s1.insert(a[i]);
cout << a[i].na << " is working hard now." << endl;
} else {
s2.erase(tmp);
s1.insert(tmp);
s2.insert(a[i]);
cout << a[i].na << " is not working now." << endl;
cout << tmp.na << " is working hard now." << endl;
}
} else {
s2.insert(a[i]);
cout << a[i].na << " is not working now." << endl;
}
}
}
n++;
}
}
return 0;
}
Parentheses
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main()
{
cin>>n;
int cnt=1;
ll cha=0;
ll s=0;
while(s<n)
{
cha++;
s+=cha;
}
ll tt=s-n;
ll t=cha-tt;
if(tt==0)
{
for(int i=1; i<=cha; ++i)
printf(")");
for(int i=1; i<=cha; ++i)
printf("(");
}
else
{
for (int i=0; i<cha; i++)
if (i==t)
printf("()");
else
printf(")");
for (int i=1; i<cha; i++)
printf("(");
}
printf("
");
}