C. Removing Columns
https://codeforc.es/contest/496/problem/C
从前往后标记那些前面已经确定字典序合法的行,对于字典序不合法又没被标记的直接删除该列。
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #define IO std::ios::sync_with_stdio(0) 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 #define pa pair<int,int> 6 using namespace std; 7 #define ll long long 8 #define mk make_pair 9 #define pb push_back 10 #define se second 11 #define fi first 12 #define ls o<<1 13 #define rs o<<1|1 14 ll mod=998244353; 15 const int N=1e2+5; 16 int n,m; 17 char s[N][N]; 18 int vis[N]; 19 int main(){ 20 IO; 21 cin>>n>>m; 22 for(int i=1;i<=n;i++){ 23 cin>>s[i]+1; 24 } 25 int ans=0; 26 for(int i=1;i<=m;i++){ 27 int f=0; 28 for(int j=2;j<=n;j++){ 29 if(s[j][i]<s[j-1][i]&&!vis[j])f=1; 30 } 31 if(f)ans++; 32 else{ 33 for(int j=2;j<=n;j++){ 34 if(s[j][i]>s[j-1][i])vis[j]=1; 35 } 36 } 37 38 } 39 cout<<ans<<endl; 40 }
D. Tennis Game
https://codeforc.es/contest/496/problem/D
预处理各自胜场数对应的编号,枚举t,从前往后判断每轮是由谁赢下,
结合最后一个场是谁赢的谁就要赢判断是否合法就做完了。
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #define IO std::ios::sync_with_stdio(0) 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 #define pa pair<int,int> 6 using namespace std; 7 #define ll long long 8 #define mk make_pair 9 #define pb push_back 10 #define se second 11 #define fi first 12 #define ls o<<1 13 #define rs o<<1|1 14 ll mod=998244353; 15 const int N=1e5+5; 16 int n; 17 int aid[N],bid[N]; 18 int as[N],bs[N]; 19 vector<pa>ans; 20 int main(){ 21 IO; 22 cin>>n; 23 int h1=0,h2=0; 24 for(int i=1;i<=n;i++){ 25 int x; 26 cin>>x; 27 if(x==1){ 28 h1++; 29 aid[h1]=i; 30 } 31 else{ 32 h2++; 33 bid[h2]=i; 34 } 35 as[i]=h1; 36 bs[i]=h2; 37 } 38 for(int t=1;t<=n;t++){ 39 int p=0,w1=0,w2=0,w3=0; 40 while(p<n){ 41 int c1=as[p],c2=bs[p]; 42 if(c1+t>h1&&c2+t>h2)break; 43 if(c1+t<=h1&&c2+t<=h2){ 44 if(aid[c1+t]<bid[c2+t]){ 45 w1++; 46 w3=1; 47 p=aid[c1+t]; 48 } 49 else{ 50 w2++; 51 w3=2; 52 p=bid[c2+t]; 53 } 54 } 55 else if(c1+t<=h1){ 56 w1++; 57 w3=1; 58 p=aid[c1+t]; 59 } 60 else{ 61 w2++; 62 w3=2; 63 p=bid[c2+t]; 64 } 65 } 66 if(p==n){ 67 if(w3==1&&w1>w2)ans.pb(mk(w1,t)); 68 if(w3==2&&w2>w1)ans.pb(mk(w2,t)); 69 } 70 } 71 sort(ans.begin(),ans.end()); 72 cout<<ans.size()<<endl;; 73 for(auto tmp :ans){ 74 cout<<tmp.fi<<" "<<tmp.se<<endl; 75 } 76 }
E. Distributing Parts
https://codeforc.es/contest/496/problem/E
贪心。
把每个演员分配给音乐,在满足演员的r2>=音乐的r1的情况下,取l1尽量小的音乐。
用set和struct搞一搞很方便,注意struct里的x和id都必须安排顺序,否则insert时会失败。
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #define IO std::ios::sync_with_stdio(0) 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 #define pa pair<int,int> 6 using namespace std; 7 #define ll long long 8 #define mk make_pair 9 #define pb push_back 10 #define se second 11 #define fi first 12 #define ls o<<1 13 #define rs o<<1|1 14 ll mod=998244353; 15 const int N=1e5+5; 16 int n,m; 17 struct node{ 18 int l,r,k,id; 19 bool operator <(const node &t)const{ 20 return r<t.r; 21 } 22 }a[N],b[N]; 23 bool cmp(node n1,node n2){ 24 return n1.r<n2.r; 25 } 26 struct point{ 27 int x,id; 28 point(int x,int id):x(x),id(id){} 29 bool operator <(const point &t)const{ 30 if(x==t.x)return id<t.id; 31 return x<t.x; 32 } 33 }; 34 set<point>s; 35 int d[N]; 36 int main(){ 37 IO; 38 cin>>n; 39 for(int i=1;i<=n;i++){ 40 cin>>a[i].l>>a[i].r; 41 a[i].id=i; 42 } 43 cin>>m; 44 for(int i=1;i<=m;i++){ 45 cin>>b[i].l>>b[i].r>>b[i].k; 46 b[i].id=i; 47 } 48 sort(a+1,a+1+n); 49 sort(b+1,b+1+m); 50 int ans=0; 51 int j=1; 52 for(int i=1;i<=m;i++){ 53 while(j<=n&&a[j].r<=b[i].r){ 54 s.insert(point(a[j].l,a[j].id)); 55 j++; 56 } 57 set<point>iter it; 58 for(int k=1;k<=b[i].k;k++){ 59 it=s.lower_bound(point(b[i].l,-1)); 60 if(it==s.end())break; 61 point tmp=*it; 62 d[tmp.id]=b[i].id; 63 s.erase(tmp); 64 ans++; 65 } 66 } 67 if(ans<n)cout<<"NO"<<endl; 68 else{ 69 cout<<"YES"<<endl; 70 for(int i=1;i<=n;i++){ 71 cout<<d[i]<<" "; 72 } 73 } 74 } 75 /* 76 3 77 1 2 78 1 2 79 1 2 80 2 81 1 2 1 82 1 2 2 83 */