Educational Codeforces Round 61 (Div.2)
A.(c1=0&&c3>0)||(c1!=c4)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 6 typedef long long ll; 7 using namespace std; 8 9 int c1,c2,c3,c4; 10 11 int main(){ 12 freopen("a.in","r",stdin); 13 freopen("a.out","w",stdout); 14 scanf("%d%d%d%d",&c1,&c2,&c3,&c4); 15 if ((!c1 && c3) || c1!=c4) puts("0"); else puts("1"); 16 return 0; 17 }
B.每次免费的显然应该是第n-q+1大的那个。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 6 typedef long long ll; 7 using namespace std; 8 9 const int N=300010; 10 int n,m,x,a[N]; 11 ll sm; 12 13 int main(){ 14 scanf("%d",&n); 15 rep(i,1,n) scanf("%d",&a[i]),sm+=a[i]; 16 sort(a+1,a+n+1); scanf("%d",&m); 17 rep(i,1,m) scanf("%d",&x),cout<<sm-a[n-x+1]<<endl; 18 return 0; 19 }
C.正难则反,先计算所有区间的并集sm,再计算最少要使多长区间不被覆盖ans,答案即sm-ans。
考虑枚举区间i,j,那么不涂这两个区间会使b[i]+b[j]+c[i][j]的区间不被覆盖。其中b[i]为只能由第i个区间覆盖的区间长度,c[i][j]为只能被i,j两个区间覆盖的区间长度。先O(nq)预处理出b,c,再O(q^2)计算答案即可。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 6 typedef long long ll; 7 using namespace std; 8 9 const int N=5010; 10 int n,m,l,r,sm,ans,x[N],y[N],b[N],mp[N][N]; 11 12 int main(){ 13 scanf("%d%d",&n,&m); ans=n+1; 14 rep(i,1,m){ 15 scanf("%d%d",&l,&r); 16 rep(j,l,r){ 17 if (y[j]) x[j]=y[j]=-1; 18 if (x[j] && !y[j]) y[j]=i; 19 if (!x[j]) x[j]=i; 20 } 21 } 22 rep(i,1,n){ 23 if (x[i]) sm++; 24 if (x[i]>0 && !y[i]) b[x[i]]++; 25 if (x[i]>0 && y[i]>0) mp[x[i]][y[i]]++,mp[y[i]][x[i]]++; 26 } 27 rep(i,1,m) rep(j,1,m) if (i!=j) ans=min(ans,b[i]+b[j]+mp[i][j]); 28 printf("%d\n",sm-ans); 29 return 0; 30 }
D.二分答案,每次找到最先降到0以下的那个同学充电。
1 #include<queue> 2 #include<cstdio> 3 #include<iostream> 4 #include<algorithm> 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 6 typedef long long ll; 7 using namespace std; 8 9 const int N=200010; 10 int n,k; 11 ll mx,a[N],b[N]; 12 struct P{ ll a,b,r; }; 13 bool operator <(const P &a,const P &b){ return a.r>b.r; } 14 priority_queue<P>Q; 15 16 bool chk(ll mid){ 17 while (!Q.empty()) Q.pop(); 18 rep(i,1,n) Q.push((P){a[i],b[i],a[i]/b[i]}); 19 rep(t,1,k){ 20 P x=Q.top(); Q.pop(); 21 if (x.a/x.b+1<t) return 0; 22 if (x.a/x.b+1>=k) return 1; 23 Q.push((P){x.a+mid,x.b,(x.a+mid)/x.b}); 24 } 25 return 1; 26 } 27 28 int main(){ 29 ios::sync_with_stdio(0); 30 cin>>n>>k; 31 rep(i,1,n) cin>>a[i]; 32 rep(i,1,n) cin>>b[i],mx=max(mx,b[i]); 33 ll L=0,R=(k-1)*mx+1; 34 while (L<R){ 35 ll mid=(L+R)>>1; 36 if (chk(mid)) R=mid; else L=mid+1; 37 } 38 if (L>=(k-1)*mx+1) puts("-1"); else cout<<L<<endl; 39 return 0; 40 }