A 签到
奇数不变,偶数减一
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 1e5+10; 5 int n, a[N], b[N], tot; 6 int M = 1e9; 7 int main() { 8 cin >> n; 9 for(int i = 0; i < n; i ++) { 10 cin >> a[i]; 11 } 12 for(int i = 0; i < n; i ++) { 13 if(a[i]&1) printf("%d ",a[i]); 14 else printf("%d ",a[i]-1); 15 } 16 return 0; 17 }
B. Polycarp's Practice
n个数,去k个区间,是的区间的最大值之和最大。
直接去最大的k个数就行。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 1e5+10; 5 int n, k, a[N], x; 6 struct Nod{ 7 int id, num; 8 }e[N]; 9 bool cmp(Nod a, Nod b) { 10 return a.num > b.num; 11 } 12 int main() { 13 cin >> n >> k; 14 for(int i = 1; i <= n; i ++) { 15 cin >> x; 16 e[i].id = i; 17 e[i].num = x; 18 } 19 sort(e+1,e+1+n,cmp); 20 int ans = 0; 21 for(int i = 1; i <= k; i ++) { 22 a[i] = e[i].id; 23 ans += e[i].num; 24 } 25 printf("%d ",ans); 26 if(k==1) return 0*printf("%d ",n); 27 sort(a+1,a+1+k); 28 for(int i = 1; i < k; i ++) { 29 printf("%d ",a[i]-a[i-1]); 30 } 31 printf("%d ",n-a[k-1]); 32 return 0; 33 }
C. Three Parts of the Array
n个数,划分为三个区间,可以为空,求第一个区间之和与第三区间之和相同时,最大值是多少。
1至n,遍历下,求出前i个数之和,判断是否[j,n]之和等于前面的就行,二分查找。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 ll n, a[N], sum[N], pre[N]; 6 bool ok(int l, int r, ll cnt) { 7 while(l <= r) { 8 int m = (l+r) >> 1; 9 if(pre[m] == cnt) return true; 10 if(pre[m] > cnt) l = m+1; 11 else r = m-1; 12 } 13 return false; 14 } 15 int main() { 16 cin >> n; 17 for(int i = 1; i <= n; i ++) { 18 cin >> a[i]; 19 sum[i] = sum[i-1] + a[i]; 20 } 21 for(int i = n; i >= 0; i --) pre[i] = pre[i+1] + a[i]; 22 ll MAX = 0; 23 for(int i = 1; i <= n; i ++) { 24 ll ans = sum[i]; 25 if(ok(i+1,n, ans)) { 26 MAX = ans; 27 } 28 } 29 printf("%lld ",MAX); 30 return 0; 31 }
D. Two Strings Swaps
You are given two strings aa and bb consisting of lowercase English letters, both of length nn. The characters of both strings have indices from 11 to nn, inclusive.
You are allowed to do the following changes:
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and bibi;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and an−i+1an−i+1;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters bibi and bn−i+1bn−i+1.
Note that if nn is odd, you are formally allowed to swap a⌈n2⌉a⌈n2⌉ with a⌈n2⌉a⌈n2⌉ (and the same with the string bb) but this move is useless. Also you can swap two equal characters but this operation is useless as well.
You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps.
In one preprocess move you can replace a character in aa with another character. In other words, in a single preprocess move you can choose any index ii (1≤i≤n1≤i≤n), any character cc and set ai:=cai:=c.
Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings aa and bb equal by applying some number of changes described in the list above.
Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess movesto the string bb or make any preprocess moves after the first change is made.
The first line of the input contains one integer nn (1≤n≤1051≤n≤105) — the length of strings aa and bb.
The second line contains the string aa consisting of exactly nn lowercase English letters.
The third line contains the string bb consisting of exactly nn lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string aa equal to string bb with a sequence of changes from the list above.
7
abacaba
bacabaa
4
5
zcabd
dbacz
0
In the first example preprocess moves are as follows: a1:=a1:='b', a3:=a3:='c', a4:=a4:='a' and a5:=a5:='b'. Afterwards, a=a="bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a2,b2)swap(a2,b2) and swap(a2,a6)swap(a2,a6). There is no way to use fewer than 44preprocess moves before a sequence of changes to make string equal, so the answer in this example is 44.
In the second example no preprocess moves are required. We can use the following sequence of changes to make aa and bb equal: swap(b1,b5)swap(b1,b5), swap(a2,a4)swap(a2,a4).
两个长度为n的字符串,需要让两个字符串变成相同时的最小修改数。有三个操作和一个修改。
1、交换ai与bi
2、交换ai与an-i+1
3、交换bi与bn-i+1
将ai修改成任意字符
有几种情况,
当为 a a 不需要改变 a c 改变一次 a a 改变两次
b b b a b c
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 int n; 6 set<char> st; 7 char str1[N], str2[N]; 8 int main() { 9 cin >> n >> str1+1 >> str2+1; 10 int ans = 0; 11 for(int i = 1, j = n; i < j; i ++, j--) { 12 int now = min((str1[i]!=str2[i])+(str1[j]!=str2[j]), (str1[i]!=str2[j])+(str1[j]!=str2[i])); 13 if(str2[i]==str2[j]) now = min(now, int(str1[i]!=str1[j])); 14 ans += now; 15 } 16 ans += (n%2==1&&str1[(n+1)/2]!=str2[(n+1)/2]); 17 printf("%d ",ans); 18 return 0; 19 }
E. Military Problem
In this problem you will have to help Berland army with organizing their command delivery system.
There are nn officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer aa is the direct superior of officer bb, then we also can say that officer bb is a direct subordinate of officer aa.
Officer xx is considered to be a subordinate (direct or indirect) of officer yy if one of the following conditions holds:
- officer yy is the direct superior of officer xx;
- the direct superior of officer xx is a subordinate of officer yy.
For example, on the picture below the subordinates of the officer 33 are: 5,6,7,8,95,6,7,8,9.
The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.
Formally, let's represent Berland army as a tree consisting of nn vertices, in which vertex uu corresponds to officer uu. The parent of vertex uucorresponds to the direct superior of officer uu. The root (which has index 11) corresponds to the commander of the army.
Berland War Ministry has ordered you to give answers on qq queries, the ii-th query is given as (ui,ki)(ui,ki), where uiui is some officer, and kiki is a positive integer.
To process the ii-th query imagine how a command from uiui spreads to the subordinates of uiui. Typical DFS (depth first search) algorithm is used here.
Suppose the current officer is aa and he spreads a command. Officer aa chooses bb — one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then aa chooses the one having minimal index. Officer aa gives a command to officer bb. Afterwards, bb uses exactly the same algorithm to spread the command to its subtree. After bb finishes spreading the command, officer aa chooses the next direct subordinate again (using the same strategy). When officer aa cannot choose any direct subordinate who still hasn't received this command, officer aa finishes spreading the command.
Let's look at the following example:
If officer 11 spreads a command, officers receive it in the following order: [1,2,3,5,6,8,7,9,4][1,2,3,5,6,8,7,9,4].
If officer 33 spreads a command, officers receive it in the following order: [3,5,6,8,7,9][3,5,6,8,7,9].
If officer 77 spreads a command, officers receive it in the following order: [7,9][7,9].
If officer 99 spreads a command, officers receive it in the following order: [9][9].
To answer the ii-th query (ui,ki)(ui,ki), construct a sequence which describes the order in which officers will receive the command if the uiui-th officer spreads it. Return the kiki-th element of the constructed list or -1 if there are fewer than kiki elements in it.
You should process queries independently. A query doesn't affect the following queries.
The first line of the input contains two integers nn and qq (2≤n≤2⋅105,1≤q≤2⋅1052≤n≤2⋅105,1≤q≤2⋅105) — the number of officers in Berland army and the number of queries.
The second line of the input contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn (1≤pi<i1≤pi<i), where pipi is the index of the direct superior of the officer having the index ii. The commander has index 11 and doesn't have any superiors.
The next qq lines describe the queries. The ii-th query is given as a pair (ui,kiui,ki) (1≤ui,ki≤n1≤ui,ki≤n), where uiui is the index of the officer which starts spreading a command, and kiki is the index of the required officer in the command spreading sequence.
Print qq numbers, where the ii-th number is the officer at the position kiki in the list which describes the order in which officers will receive the command if it starts spreading from officer uiui. Print "-1" if the number of officers which receive the command is less than kiki.
You should process queries independently. They do not affect each other.
9 6
1 1 1 3 5 3 5 7
3 1
1 5
3 4
7 3
1 8
1 9
3
6
8
-1
9
4
求从u考试按深度遍历第k个数是多少,没有则为-1
1为根,从根遍历求的顺序储存,其它的点遍历一定在根遍历的子序列中,所以只要求下从u开始遍历有多少个,并且开始的位置再根遍历的序列中的第几位就行了。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 std::vector<int> v[N]; 6 int n, q, x, ans = 1, tot; 7 int a[N], cnt[N]; 8 map<int,int> mp; 9 void dfs(int u) { 10 int tmp = ans; 11 for(int i = 0; i < v[u].size(); i ++) { 12 ans++; 13 a[tot++] = v[u][i]; 14 dfs(v[u][i]); 15 } 16 // printf("%d %d %d ",u,ans,tmp ); 17 cnt[u] = ans - tmp+1; 18 } 19 int main() { 20 cin >> n >> q; 21 for(int i = 2; i <= n; i ++) { 22 scanf("%d", &x); 23 v[x].push_back(i); 24 } 25 a[tot++] = 1; 26 cnt[1] = n; 27 dfs(1); 28 for(int i = 0; i < tot; i ++) { 29 mp[a[i]] = i; 30 } 31 // for(int i = 1; i <= tot; i ++) printf("%d ", cnt[i]);printf(" "); 32 while(q--) { 33 int u, x; 34 scanf("%d%d",&u, &x); 35 if(cnt[u] < x) printf("-1 "); 36 else { 37 printf("%d ",a[mp[u]+x-1]); 38 } 39 } 40 return 0; 41 }