Codeforces Round #401 (Div. 2) A,B,C,D,E
思路:模6,自己手模拟一下即可;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; int a[10]={2,3,3,2,1,1}; int b[10]={1,1,2,3,3,2}; int c[10]={3,2,1,1,2,3}; int main() { int n; int x; scanf("%d%d",&n,&x); n%=6; if(!n)n=6; x++; if(a[n-1]==x) printf("0\n"); else if(b[n-1]==x) printf("1\n"); else printf("2\n"); return 0; }
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains n digits — Sherlock's credit card number.
The third line contains n digits — Moriarty's credit card number.
First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
3
123
321
0
2
2
88
00
2
0
First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.
题意:两个人,n个数字,第一个人出数字的顺序不变,第二个人的随意,出的数字小于另一个人的算赢;
求第二个人赢得最小次数,第一个人赢的最大次数;
思路:标记,贪心;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; char a[N]; char b[N]; int flaga[20]; int flagb[20]; int x[20]; int y[20]; int n; void slove1() { int ans=n; for(int i=0;i<10;i++) x[i]=flaga[i],y[i]=flagb[i]; for(int i=0;i<=9;i++) { for(int j=i;j>=0;j--) { int minn=min(y[i],x[j]); ans-=minn; x[i]-=minn; y[i]-=minn; } } printf("%d\n",ans); } void slove2() { int ans=0; for(int i=0;i<10;i++) x[i]=flaga[i],y[i]=flagb[i]; for(int i=0;i<=9;i++) { for(int j=i+1;j<=9;j++) { int minn=min(x[i],y[j]); ans+=minn; x[i]-=minn; y[j]-=minn; } } printf("%d\n",ans); } int main() { scanf("%d",&n); scanf("%s%s",a,b); for(int i=0;i<n;i++) flaga[a[i]-'0']++,flagb[b[i]-'0']++; slove1(); slove2(); return 0; }
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 ton - 1.
Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.
Alyona is too small to deal with this task and asks you to help!
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).
Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".
5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5
Yes
No
Yes
Yes
Yes
No
In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
题意:给你n*m的一个矩阵;k个询问,取出l行-r行,找出是否有一列是非递减的序列;
思路:有点dp的思想,对于一列dp[i]=(a[i]>=a[i-1]? dp[i-1],i);
dp[i]表示从dp[i]行到当前行是非递减序列;
对于每一行取一个最小值minn,表示从minn行-i行开始能找到一个非递减的序列;
详见代码;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; vector<int>v[N]; vector<int>flag[N]; int minn[N]; int n,m; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { minn[i]=inf; v[i].push_back(0); for(int j=1;j<=m;j++) { int x; scanf("%d",&x); v[i].push_back(x); } } minn[1]=1; for(int i=0;i<=m;i++) flag[1].push_back(1); for(int i=2;i<=n;i++) { flag[i].push_back(0); for(int j=1;j<=m;j++) { if(v[i][j]>=v[i-1][j]) { flag[i].push_back(flag[i-1][j]); minn[i]=min(minn[i],flag[i][j]); } else { flag[i].push_back(i); minn[i]=min(minn[i],i); } } } int q; scanf("%d",&q); while(q--) { int l,r; scanf("%d%d",&l,&r); if(minn[r]<=l) printf("Yes\n"); else printf("No\n"); } return 0; }
Vasya is an administrator of a public page of organization "Mouse and keyboard" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.
The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).
Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.
The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of hashtags being edited now.
Each of the next n lines contains exactly one hashtag of positive length.
It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.
Print the resulting hashtags in any of the optimal solutions.
3
#book
#bigtown
#big
#b
#big
#big
3
#book
#cool
#cold
#book
#co
#cold
4
#car
#cart
#art
#at
#
#
#art
#at
3
#apple
#apple
#fruit
#apple
#apple
#fruit
Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold:
- at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character than bin the first position where they differ;
- if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal.
The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.
For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.
According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two.
题意:n个字符串,删掉最少的后缀使得n个字符串按字典序从小到大排序;
思路:从后往前贪心,对于当前要删的字符串进行二分,找到最长满足条件的字符串;
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=5e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; int n; string a[N]; string ans[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) cin>>a[i]; ans[n]=a[n]; for(int i=n-1;i>=1;i--) { string c=ans[i+1]; int l=0,r=a[i].size(),q; while(l<=r) { int mid=(l+r)>>1; string d=a[i].substr(0,mid); if(d<=c) { q=mid; l=mid+1; } else r=mid-1; } ans[i]=a[i].substr(0,q); } for(int i=1;i<=n;i++) cout<<ans[i]<<endl; return 0; }
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
- Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
- Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
- The total height of all rings used should be maximum possible.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Print one integer — the maximum height of the tower that can be obtained.
3
1 5 1
2 6 2
3 7 3
6
4
1 2 1
1 3 3
4 6 2
5 7 1
4
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
思路:线段树维护区间最大值;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; struct linetree { ll maxx[N*8]; void pushup(int pos) { maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]); } void build(int l,int r,int pos) { if(l==r) { maxx[pos]=0; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); } void update(int p,ll c,int l,int r,int pos) { if(l==r) { maxx[pos]=max(maxx[pos],c); return; } int mid=(l+r)>>1; if(p<=mid) update(p,c,l,mid,pos<<1); else update(p,c,mid+1,r,pos<<1|1); pushup(pos); } ll query(int L,int R,int l,int r,int pos) { if(L<=l&&r<=R) { return maxx[pos]; } int mid=(l+r)>>1; ll maxxx=0; if(L<=mid) maxxx=max(maxxx,query(L,R,l,mid,pos<<1)); if(R>mid) maxxx=max(maxxx,query(L,R,mid+1,r,pos<<1|1)); return maxxx; } }; linetree tree; int n; int s[N<<1],cnt; struct is { int a,b; ll h; bool operator <(const is &c)const { if(b!=c.b) return b<c.b; return a<c.a; } }a[N]; int getpos(int x) { int pos=lower_bound(s,s+cnt,x)-s; return pos+1; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%lld",&a[i].a,&a[i].b,&a[i].h); s[cnt++]=a[i].a; s[cnt++]=a[i].b; } sort(a+1,a+n+1); sort(s,s+cnt); cnt=unique(s,s+cnt)-s; tree.build(1,cnt+20,1); for(int i=1;i<=n;i++) { ll h=a[i].h; ll maxxx=tree.query(getpos(a[i].a)+1,getpos(a[i].b),1,cnt+20,1); //cout<<h<<" "<<maxxx<<endl; tree.update(getpos(a[i].b),maxxx+h,1,cnt+20,1); } printf("%lld\n",tree.query(1,cnt,1,cnt+20,1)); return 0; }