数独和dij卡了时间
A - Keyboard
CodeForces - 474A
Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way:
qwertyuiop
asdfghjkl;
zxcvbnm,./
Unfortunately Mole is blind, so sometimes it is problem for him to put his hands accurately. He accidentally moved both his hands with one position to the left or to the right. That means that now he presses not a button he wants, but one neighboring button (left or right, as specified in input).
We have a sequence of characters he has typed and we want to find the original message.
Input
First line of the input contains one letter describing direction of shifting ('L' or 'R' respectively for left or right).
Second line contains a sequence of characters written by Mole. The size of this sequence will be no more than 100. Sequence contains only symbols that appear on Mole's keyboard. It doesn't contain spaces as there is no space on Mole's keyboard.
It is guaranteed that even though Mole hands are moved, he is still pressing buttons on keyboard and not hitting outside it.
Output
Print a line that contains the original message.
Example
R
s;;upimrrfod;pbr
allyouneedislove
这个题目都不用怎么读吧,就是左移或者右移一位,但是去建立一个映射也挺麻烦,直接找到这个字母输出它的左右两边还行,毕竟qwert键盘没什么规律
#includeusing namespace std; string t,c,s="qwertyuiopasdfghjkl;zxcvbnm,./"; int main() { cin>>c>>t; for(int i=0; t[i]; i++) { int j=0; for(;s[j]&&s[j]!=t[i]; j++); if(c=="R")putchar(s[j-1]); else putchar(s[j+1]); } return 0; }
B - Duff and Meat
CodeForces - 588A
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., anand p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Example
3
1 3
2 2
3 1
10
3
1 3
2 1
3 2
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
看提示猜意思,就是找pi最小的,但是有顺序,求一下和,并不会爆LL,1e5*1e2*1e2
#includeusing namespace std; long long ans; const int N=1e5+5; int a[N],p[N]; int main() { int n; cin>>n; cin>>a[1]>>p[1]; ans+=a[1]*p[1]; for(int i=2; i<=n; i++) { cin>>a[i]>>p[i]; p[i]=min(p[i-1],p[i]); ans+=a[i]*p[i]; } cout< endl; return 0; }
C - Party
CodeForces - 115A
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
- Employee A is the immediate manager of employee B
- Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all nemployees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Example
5
-1
1
2
1
-1
3
Note
For the first example, three groups are sufficient, for example:
- Employee 1
- Employees 2 and 4
- Employees 3 and 5
这个题目就是类似并查集,A是B的祖先,B是C的祖先,那么A也是C的祖先。直接遍历每个值就好,就是就是形成一条链,那就是n^2的复杂度,显然没有问题
#includeusing namespace std; int main() { int n,a[2005],ans=0; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1; i<=n;i++) { int k=a[i],t=0; while(k>0)k=a[k],t++; ans=max(t,ans); } cout< 1; }
D - Mahmoud and a Triangle
CodeForces - 766B
Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.
Mahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.
Input
The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.
Output
In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise.
Example
5
1 5 3 2 4
YES
3
4 1 2
NO
Note
For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.
给你n条边看看能不能生成一个三角形,三边不排序的话就是两边之和大于第三边,两边之差小于第三边
就是两条较短的边小于第三边
#includeusing namespace std; const int N=1e5+5; int a[N]; int main() { int n,f=0; cin>>n; for(int i=0; i ) cin>>a[i]; sort(a,a+n); for(int i=2; i ) if(a[i-2]+a[i-1]>a[i])f=1; printf("%s\n",f?"YES":"NO"); }
E - Vasya and Petya's Game
CodeForces - 576A
Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.
Petya can ask questions like: "Is the unknown number divisible by number y?".
The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.
Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.
Input
A single line contains number n (1 ≤ n ≤ 103).
Output
Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n).
If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.
Example
4
3
2 4 3
6
4
2 4 3 5
Note
The sequence from the answer to the first sample test is actually correct.
If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.
If the unknown number is divisible by 4, it is 4.
If the unknown number is divisible by 3, then the unknown number is 3.
Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
每次猜一个值是否可以被n整除
这个题目是特判题的,但是我去猜的一定要最少的,我可以利用埃筛去搞,筛素数,一边用素数的j次方去搞,就是所给的样例
#includeusing namespace std; const int N=1e3+5; int a[N]; vector<int>V; int main() { int n; cin>>n; for(int i=2; i<=n; i++) { if(!a[i]) { for(int j=i+i; j<=n; j+=i) a[j]=1; for(int j=i; j<=n; j*=i)V.push_back(j); } } printf("%d\n",V.size()); for(auto X:V) printf("%d ",X); }
F - Dijkstra?
CodeForces - 20C
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Example
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
1 4 3 5
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
1 4 3 5
dij,SPFA应该都可以过,输出1到n的最短路径
每条边都是10^6,会爆int,温馨提示
#includeusing namespace std; typedef long long ll; const ll INF=1e18; const int N=1e5+5; int n,m,pre[N]; vector int> > G[N]; vector<int>V; ll d[N]; priority_queue<int> Q; int main() { scanf("%d%d",&n,&m); for(int i=0,u,v,w; i ) scanf("%d%d%d",&u,&v,&w),G[u].push_back(make_pair(w,v)),G[v].push_back(make_pair(w,u)); fill(d,d+n+1,INF); d[1]=0,Q.push(1); while(!Q.empty()) { int u=Q.top(); Q.pop(); for(pair int>E:G[u]) { int v=E.second; if(d[u]+E.first E.first,Q.push(v); } } if(d[n]==INF)printf("-1\n"); else { int now=n; while(now)V.push_back(now),now=pre[now]; reverse(V.begin(),V.end()); for(auto X:V)printf("%d ",X); } return 0; }
G - 24 Game
CodeForces - 468A
Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game.
Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a × b.
After n - 1 steps there is only one number left. Can you make this number equal to 24?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes).
If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: "a op b = c". Where a and b are the numbers you've picked at this operation; op is either "+", or "-", or "*"; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces.
If there are multiple valid answers, you may print any of them.
Example
1
NO
8
YES
8 * 7 = 56
6 * 5 = 30
3 - 4 = -1
1 - 2 = -1
30 - -1 = 31
56 - 31 = 25
25 + -1 = 24
给你1-n,都用上,构造出24
4的话构造4*3*2*1,其他做差,只用把5也够造出来就行了
#includeusing namespace std; int main() { int n; cin>>n; if(n<4) printf("NO"); else { printf("YES\n"); if(n&1) { printf("%d + %d = %d\n",1,2,3); printf("%d - %d = %d\n",5,3,2); printf("%d * %d = %d\n",2,3,6); printf("%d * %d = %d\n",6,4,24); for(int i=6;i 2) printf("%d - %d = %d\n",i+1,i,1); for(int i=5;i 2) printf("%d * %d = %d\n",1,24,24); } else { printf("%d * %d = %d\n",4,3,12); printf("%d * %d = %d\n",12,2,24); for(int i=5;i 2) printf("%d - %d = %d\n",i+1,i,1); for(int i=2;i 2) printf("%d * %d = %d\n",1,24,24); } } return 0; }
H - Fox And Names
CodeForces - 510C
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.
Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
Example
3
rivest
shamir
adleman
bcdefghijklmnopqrsatuvwxyz
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Impossible
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
aghjlnopefikdmbcqrstuvwxyz
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
acbdefhijklmnogpqrstuvwxyz
求是否存在一个字母表,能满足给出的字符串是从小到大给出的!若存在输出此字母表,若不存在,输出Impossible!
搜索下,标记最长公共前缀之后的字符
#includeusing namespace std; int n,v[27],b[27][27],F=1; string s,t,ans; void dfs(int u) { v[u]=1; for(int i=0; i<26&&F; i++) { if(b[u][i]) { if(v[i]==1)F=0; else if(v[i]!=-1) dfs(i); } } if(!F)return; v[u]=-1,ans+=u+'a'; } int main() { cin>>n>>s; for(int i=1; i ) { cin>>t; int f=1; for(int j=0; t[j]&&s[j]; j++) if(s[j]!=t[j]) { f=0,b[t[j]-'a'][s[j]-'a']=1; break; } if(f&&s.size()>t.size()) { cout<<"Impossible\n"; return 0; } s=t; } for(int i=0; i<26; i++)if(v[i]!=-1)dfs(i); if(!F)cout<<"Impossible\n"; else cout<<ans; return 0; }
I - Equivalent Strings
CodeForces - 559B
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases:
- They are equal.
- If we split string a into two halves of the same size a1 and a2, and string binto two halves of the same size b1 and b2, then one of the following is correct:
- a1 is equivalent to b1, and a2 is equivalent to b2
- a1 is equivalent to b2, and a2 is equivalent to b1
As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.
Gerald has already completed this home task. Now it's your turn!
Input
The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.
Output
Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.
Example
aaba
abaa
YES
aabb
abab
NO
Note
In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".
In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".
按照题意所说,如果偶数长度就往下分啊,奇数就返回,因为要比较,所以要选择一个字典序的,因为本来是可以随便加的
#includeusing namespace std; string la(string s) { if(s.size()%2)return s; string s1=la(s.substr(0,s.size()/2)),s2=la(s.substr(s.size()/2)); return s1 s1); } int main() { string s,c; cin>>s>>c; printf("%s\n",la(s)==la(c)?"YES":"NO"); }