Three Equal

Time limit: 1000 ms
Memory limit: 256 MB

 

You are given an array AA of NN integers between 00 and 22. With cost 11 you can apply the following operation A_i = ((A_i + 1)\ \% \ 3)A​i​​=((A​i​​+1) % 3).

Find the minimum cost to make all elements equal.

Standard input

The first line contains one integer NN.

The second line contains NN integers representing the elements of the array AA.

Standard output

Output a single number representing the minimum cost to make all elements of AA equal.

Constraints and notes

  • 1 \leq N \leq 10^31≤N≤10​3​​
  • The elements of the array AA are integers between 00 and 22.
Input Output
4
1 0 0 2
3
3
1 2 2
1
3
1 1 1
0

问你n个数到一个相同模数的最小值,那就直接枚举这三个模数好了,当时手残还打错一个字母

#include <bits/stdc++.h>
using namespace std;
int b[];
int main()
{
int n;
cin>>n;
for(int i=,x;i<n;i++)
{
if(x%==)b[]+=,b[]+=;
else if(x%==)b[]+=,b[]+=;
else b[]+=,b[]+=;
}
cout<<min(min(b[],b[]),b[]);
return ;
}

Ricocheting Balls

Time limit: 1000 ms
Memory limit: 256 MB

 

There are NN falling balls situated at some height levels; more specifically the i^{\text{th}}i​th​​ ball is H_iH​i​​ meters above the ground. The balls are supposed to be falling at 11 meter per second, but they're not; they're stuck in time, hovering.

You can repeat the following process as many times as you want (possibly 00 times): you will unfreeze the time for 11 second and then freeze it back up.

If a ball hits the ground, which is situated at the height level 00, it will ricohet and start ascending instead; therefore, the next time it is unfrozen, it will actually go upwards to the height level 11, then 22, 33, 44 and so on...

You want to find the moment of time that minimizes the sum of heights of all the balls. Print the value of the sum obtained at this moment of time.

Standard input

The first line contains an integer NN.

The next line contains NN integers, representing HH.

Standard output

Print an integer, the minimum sum of heights of all balls that can be obtained by the above-described process.

Constraints and notes

  • 1 \leq N \leq 10^51≤N≤10​5​​
  • 1 \leq H_i \leq 10^91≤H​i​​≤10​9​​
Input Output Explanation
4
1 4 5 2
6

Moment of time 00: [1, 4, 5, 2][1,4,5,2], summing to 1212.

Moment of time 11: [0, 3, 4, 1][0,3,4,1], summing to 88.

Moment of time 22: [1, 2, 3, 0][1,2,3,0], summing to 66. We can notice how the first ball starts ascending, after it hit the ground.

Moment of time 33: [2, 1, 2, 1][2,1,2,1], summing to 66.

9
1 7 1 1 1 5 3 6 3
17

小球每1s落下1m,问你这个距离地面距离的最小和,当然是选择中位数了

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int h[N];
int main()
{
int n;
cin>>n;
long long ans=;
for(int i=; i<n; i++)cin>>h[i];
sort(h,h+n);
for(int i=; i<n; i++)
ans+=abs(h[i]-h[(n-)/]);
cout<<ans<<endl;
return ;
}

Binary Isomorphism

Time limit: 1000 ms
Memory limit: 256 MB

 

You are given two binary trees. These two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping the children of some nodes. Two leaves are isomorphic.

Both these trees will be given through their parents array. In a parents array,

  • nodes are 11-based
  • there is only one position rr where P_r = 0P​r​​=0. This means that rr is the root of the tree.
  • for every node i \neq ri≠r, its direct parent is P_iP​i​​.

Standard input

The first line contains TT, the number of test cases.

For every test:

  • the first line contains NN, representing the number of nodes in both of the trees
  • the second line contains NN integers, representing the parents array of the first tree
  • the third line contains NN integers, representing the parents array of the second tree

Standard output

For every test case, print a line containing 1 if the two trees are isomorphic, or 0 otherwise.

Constraints and notes

  • 1 \leq T \leq 201≤T≤20
  • 1 \leq N \leq 10^51≤N≤10​5​​. The sum of all values of NN in a test is \leq 10^5≤10​5​​
Input Output Explanation
3
4
3 0 2 3
3 4 0 3
5
5 1 0 3 4
5 1 4 2 0
5
0 1 2 1 4
0 1 4 1 2
0
1
1

13243142

1523442351

1234512543

给你两个二叉树,看其是否同构

需要dfs看其每个节点孩子数是否相同

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
vector<int>v[N];
vector<int>v2[N];
int dfs(int x,int y)
{
if(v[x].size()!=v2[y].size())return ;
if(v[x].size()==)return ;
if(v[x].size()==)return dfs(v[x][],v2[y][]);
else
{
if(dfs(v[x][],v2[y][])&&dfs(v[x][],v2[y][])||dfs(v[x][],v2[y][])&&dfs(v[x][],v2[y][]))
return ;
return ;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,rt1,rt2;
scanf("%d",&n);
for(int i=; i<=n; i++)
v[i].clear(),v2[i].clear();
for(int i=; i<=n; i++)
{
int x;
scanf("%d",&x);
if(x==)
rt1=i;
else v[x].push_back(i);
}
for(int i=; i<=n; i++)
{
int x;
scanf("%d",&x);
if(x==)
rt2=i;
else v2[x].push_back(i);
}
printf("%d\n",dfs(rt1,rt2));
}
return ;
}

还可以去括号匹配的思想

#include <bits/stdc++.h>
#define mod 1000000007
#define p 666013
using namespace std; int put[];
vector <int> adia[]; pair <int, int> dfs(int nod) {
vector <pair <int, int>> fii;
for (auto i : adia[nod]) {
fii.push_back(dfs(i));
} sort(fii.begin(), fii.end());
int lact();
int ans = '(';
for (auto i : fii) {
ans += 1ll * i.first * put[lact] % mod;
lact += i.second;
}
ans += 1ll * ')' * lact;
lact++;
return { ans, lact };
} void solve()
{
int n;
cin >> n;
vector <pair <int, int>> v; for (int q(); q < ; q++) {
for (int i(); i <= n; i++)
adia[i] = vector <int> (); int root;
for (int i(); i <= n; i++) {
int tata;
cin >> tata;
if (tata == )
root = i;
else
adia[tata].push_back(i);
}
auto x = dfs(root);
v.push_back(x);
} cout << (v[] == v[]) << '\n';
} int main() {
put[] = ; for (int i(); i < ; i++)
put[i] = 1ll * * put[i - ] % mod;
int t;
cin >> t; while (t--)
solve();
return ;
}

Russian Dolls Ways

Time limit: 1000 ms
Memory limit: 256 MB

 

You have NN Russian dolls, for the i^{th}i​th​​ doll you know its size A_iA​i​​.

A doll of size jj can be put inside a doll of size ii if j < ij<i. In addition, a doll of size ii can nest only onesmaller doll jj. But that doll jj, can nest another smaller doll, in a recursive manner.

For example, if you have three dolls of sizes 33, 1010 and 77, you can put the first doll inside the third, and then the third inside the second.

Your goal is to count the number of nestings that minimizes the number of dolls at the end.

Consider the array PP of size NN where P_iP​i​​ is index of the doll in which the doll ii is nested into. If doll iiis not nested into any other doll, P_i = 0P​i​​=0. Two ways are considered distinct if there is a 1 \leq i \leq N1≤i≤Nsuch that P1_i \neq P2_iP1​i​​≠P2​i​​.

Standard input

The first line contains a single integer NN.

The second line contains NN integers representing the elements of AA, the sizes of the dolls.

Standard output

Output a single number representing the number of ways to nest the dolls in order to achieve the minimum number of dolls at the end, modulo 10^9+710​9​​+7.

Constraints and notes

  • 1 \leq N \leq 10^51≤N≤10​5​​
  • 1 \leq A_i \leq 10^91≤A​i​​≤10​9​​
Input Output Explanation
3
1 2 3
1

The minimum number of dolls at the end is 11

4
1 2 2 3
4

The minimum number of dolls at the end is 22.

The PP array from the input is

[2, 4, 0, 0][2,4,0,0]

[2, 0, 4, 0][2,0,4,0]

[3, 4, 0, 0][3,4,0,0]

[3, 0, 4, 0][3,0,4,0]

6
1 1 2 2 3 3
4

The minimum number of dolls at the end is 22.

The PP array from the input is

[3, 4, 5, 6, 0, 0][3,4,5,6,0,0]

[4, 3, 5, 6, 0, 0][4,3,5,6,0,0]

[3, 4, 6, 5, 0, 0][3,4,6,5,0,0]

[4, 3, 6, 5, 0, 0][4,3,6,5,0,0]

俄罗斯套娃,就是让你求一下这个长度为n的序列最多能分成几个序列,其实就是重复个数最多的那个个数k

然后就是每个值都要选一个大小为k的盒子

#include<bits/stdc++.h>
using namespace std;
const int MD=1e9+;
long long ans=;
unordered_map<int,int> M;
vector<int>V;
int main() {
ios_base::sync_with_stdio(),cin.tie(),cout.tie();
int n;
cin>>n;
for(int i=,x;i<n;i++)cin>>x,M[x]++;
for(auto X:M)V.push_back(X.second);
sort(V.begin(),V.end());
int k=*V.rbegin();
V.pop_back();
for(auto X:V)
for(int i=;i<X;i++)ans=ans*(k-i)%MD;
cout<<ans;
}

Strange Substring

Time limit: 2000 ms
Memory limit: 256 MB

 

You are given two strings AA and BB, consisting only of lowercase letters from the English alphabet. Count the number of distinct strings SS, which are substrings of AA, but not substrings of BB.

Standard input

The first line contains AA.

The second line contains BB.

Standard output

Print the answer on the first line.

Constraints and notes

  • 1 \leq |A|, |B| \leq 10^51≤∣A∣,∣B∣≤10​5​​
Input Output
abcab
bcab
3
aaa
aa
1
acabad
abcacd
12

题意很好理解,就是让你找是A的子串,但是不是B的子串的个数

benq的SA+LCP

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; struct suffix_array {
suffix_array(const char* S) : N(strlen(S)) {
vector<int> V;
for(int i = ; i < N; i++) V.push_back(S[i]);
init(V);
} suffix_array(const vector<int>& VV) : N(VV.size()) {
vector<int> V(VV);
init(V);
} int N;
vector<int> SA;
vector<int> RA; void compress(vector<int>& V, vector<int>& C) {
copy(V.begin(), V.end(), C.begin());
sort(C.begin(), C.end());
auto cend = unique(C.begin(), C.end());
for(int i = ; i < N; i++) {
V[i] = lower_bound(C.begin(), cend, V[i]) - C.begin() + ;
}
V.push_back(); C.push_back();
} void compute_sa(vector<int>& V, vector<int>& C) {
vector<int> T(N + );
for(int i = ; i <= N; i++) SA.push_back(i);
for(int ski = ; V[SA[N]] < N; ski = ski ? ski << : ) {
fill(C.begin(), C.end(), );
for(int i = ; i < ski; i++) T[i] = N - i;
for(int i = , p = ski; i <= N; i++) if(SA[i] >= ski) T[p++] = SA[i] - ski;
for(int i = ; i <= N; i++) C[V[i]]++;
for(int i = ; i <= N; i++) C[i] += C[i - ];
for(int i = N; i >= ; i--) SA[--C[V[T[i]]]] = T[i]; T[SA[]] = ;
for(int j = ; j <= N; j++) {
int a = SA[j];
int b = SA[j - ];
T[a] = T[b] + (a + ski >= N || b + ski >= N ||
V[a] != V[b] || V[a + ski] != V[b + ski]);
}
V.swap(T);
}
} void compute_lcp(const vector<int>& OV) {
LCP = vector<int>(N);
int len = ;
for(int i = ; i < N; i++, len = max(, len - )) {
int si = RA[i];
int j = SA[si - ];
for(; i + len < N && j + len < N && OV[i + len] == OV[j + len]; len++);
LCP[si - ] = len;
}
} void init(vector<int>& V) {
vector<int> OV(V);
vector<int> C(N);
compress(V, C);
compute_sa(V, C);
RA.resize(N + );
for(int i = ; i <= N; i++) RA[SA[i]] = i;
compute_lcp(OV);
} vector<int> LCP;
}; int main() {
ios_base::sync_with_stdio(false); string S;
vector<pair<int, int> > A; int N = ;
for (int i = ; i < N; i++) {
string T; cin >> T; S += T;
S += "?";
for (int j = ; j < T.size(); j++) {
A.push_back(make_pair(i, T.size() - j));
}
A.push_back(make_pair(-, -));
}
A.push_back(make_pair(-, -)); vector<long long> result(N);
suffix_array sa(S.c_str());
sa.LCP.push_back();
for (int i = ; i <= sa.N; ) {
int j = sa.SA[i];
int ind = A[j].first;
if (ind == -) {
++i;
continue;
}
int sz = ;
while (i + sz <= sa.N && A[sa.SA[i + sz]].first == ind) {
++sz;
} int ln = sa.LCP[i - ];
for (int j = i; j < i + sz; j++) {
result[ind] += max(A[sa.SA[j]].second - max(ln, sa.LCP[j]), );
ln = min(ln, sa.LCP[j]);
}
i += sz;
}
cout << result[]; return ;
}

一种我倾向于的写法

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define N 2000000
using namespace std;
struct node{
int son[];
int len,fail;
bool sig;
}tri[N];
char S[N];
int len,L,lst;
int in[N],d[N];
void add(int last,int c,bool sig){
static int v,u,up,up1;
v=++L;
u=last;
tri[v].len=tri[u].len+;
for (;u&&!tri[u].son[c];u=tri[u].fail)tri[u].son[c]=v;
if (!u)tri[v].fail=;
else{
up=tri[u].son[c];
if (tri[up].len==tri[u].len+)tri[v].fail=up;
else{
up1=++L;
tri[up1]=tri[up];
tri[up1].len=tri[u].len+;
tri[up].fail=tri[v].fail=up1;
for (;u&&tri[u].son[c]==up;u=tri[u].fail)tri[u].son[c]=up1;
}
}
tri[v].sig=sig;
lst=v;
}
int main(){
scanf(" %s",S+);
len=strlen(S+);
lst=,L=;
for (int i=;i<=len;i++)add(lst,S[i]-'a',);
scanf(" %s",S+);
len=strlen(S+);
add(lst,,);
for (int i=;i<=len;i++)add(lst,S[i]-'a',);
for (int i=;i<=L;i++)in[tri[i].fail]++;
int l=,r=;
for (int i=;i<=L;i++)
if (!in[i])d[++r]=i;
while (l!=r){
++l;
tri[tri[d[l]].fail].sig|=tri[d[l]].sig;
if (!(--in[tri[d[l]].fail]))
d[++r]=tri[d[l]].fail;
}
long long ans=;
for (int i=;i<=L;i++)
if (!tri[i].sig)
ans+=tri[i].len-tri[tri[i].fail].len;
printf("%lld\n",ans);
return ;
}

csa Round #73 (Div. 2 only)的更多相关文章

  1. hdu5634 BestCoder Round #73 (div.1)

    Rikka with Phi  Accepts: 5  Submissions: 66  Time Limit: 16000/8000 MS (Java/Others)  Memory Limit: ...

  2. hdu5631 BestCoder Round #73 (div.2)

    Rikka with Graph  Accepts: 123  Submissions: 525  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  3. hdu5630 BestCoder Round #73 (div.2)

    Rikka with Chess  Accepts: 393  Submissions: 548  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  4. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

  5. BestCoder Round #73 (div.2)

    1001 Rikka with Chess ans = n / 2 + m / 2 1002 Rikka with Graph 题意:n + 1条边,问减去至少一条使剩下的图连通的方案数. 分析:原来 ...

  6. BestCoder Round #73 (div.2)(hdu 5630)

    Rikka with Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. csa Round #66 (Div. 2 only)

    csa66 Risk Rolls Time limit: 1000 msMemory limit: 256 MB   Alena and Boris are playing Risk today. W ...

  8. CSA Round #53 (Div. 2 only) Histogram Partition(模拟)

    传送门 题意 给出一个数组A,你有一个数组B(一开始全为0),询问多少次操作后B转化为A 一次操作:选择一段区间,加上某个正整数 分析 构建一个栈, 输入一个数,若当前栈空或栈顶元素比输入小,则加入栈 ...

  9. CSA Round #50 (Div. 2 only) Min Swaps(模拟)

    传送门 题意 给出一个排列,定义\(value为\sum_{i=1}^{n-1}abs(f[i+1]-f[i])\) \(swap(a[i],a[j])(i≠j)为一次交换\),询问最少的交换次数使得 ...

随机推荐

  1. 键盘各键对应的ASCII码值(包括鼠标和键盘所有的键)

    ESC键 VK_ESCAPE (27)回车键: VK_RETURN (13)TAB键: VK_TAB (9)Caps Lock键: VK_CAPITAL (20)Shift键: VK_SHIFT ($ ...

  2. encryptjs 加密 前端数据(vue 使用 RSA加密、java 后端 RSA解密)

    1.index.html引入 <script src="./static/js/jsencrypt.min.js"></script> 或者 npm i j ...

  3. JavaScript_9_循环

    1. JavaScript for/in 语句循环遍历对象的属性: 可以遍历数组,也可以遍历一个对象的所有属性 <body> <p>点击按钮,循环遍历对象“person”的属性 ...

  4. React 官方脚手架 create-react-app快速生成新项目

    进入新公司已经半年了,各个业务线,技术栈都已经熟悉,工作也已经游刃有余,决定慢下脚步,沉淀积累,回顾一下所用技术栈所包含的基本知识,以及再公司中的实战. 首先回顾新项目搭建 react脚手架目前使用较 ...

  5. git clone 和 download 不一样,能用git clone 就用git clone,download的代码,经常出现安装bug

    git clone 和 download 不一样,能用git clone 就用git clone,download的代码,经常出现安装bug

  6. WINDOWS-基础:LPTSTR

    1. LPTSTR解释 与char*等价,表示普通字符/字符串变量,指向字符/字符串的指针. LP:  长指针(long pointer). T:   win32环境中有一个_T宏,用来标识字符是否采 ...

  7. ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点

    Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibern ...

  8. 从输入URL到页面加载完成的过程中都发生了什么事情?

    为了便于理解,我将整个过程分为了六个问题来展开. 第一个问题:从输入 URL 到浏览器接收的过程中发生了什么事情? 从触屏到 CPU 首先是「输入 URL」,大部分人的第一反应会是键盘,不过为了与时俱 ...

  9. iOS开发之蓝牙业务封装

    因为公司做智能家居开发,有很多蓝牙的智能硬件.因此项目中经常需要和蓝牙打交道.为此为了提高开发效率,就把蓝牙的公共业务进行了封装. 本文将对封装的思路做一个简单的阐述. 首先我们需要一个头文件.在这个 ...

  10. Codeforces Round #513 (rated, Div. 1 + Div. 2)

    前记 眼看他起高楼:眼看他宴宾客:眼看他楼坍了. 比赛历程 开考前一分钟还在慌里慌张地订正上午考试题目. “诶这个数位dp哪里见了鬼了???”瞥了眼时间,无奈而迅速地关去所有其他窗口,临时打了一个缺省 ...