Codeforces Round #515 (Div. 3)
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #define lson i<<1 #define rson i<<1|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define pb(x) push_back(x) #define enld endl #define mian main #define itn int #define prinft printf #pragma GCC optimize(2) //#pragma comment(linker, "/STACK:102400000,102400000") const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; /* 4 10 2 3 7 100 51 51 51 1234 1 100 199 1000000000 1 1 1000000000 */ int n,L,v,l,r; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); while(cin>>n) { while(n--) { cin>>L>>v>>l>>r; cout<<L/v-(r/v-(l-)/v)<<endl; } } }
A - Vova and Train
分区间讨论,不断更新右极值,注意边界相切的情况(1,2)(3,4)
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #define lson i<<1 #define rson i<<1|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define pb(x) push_back(x) #define enld endl #define mian main #define itn int #define prinft printf #pragma GCC optimize(2) //#pragma comment(linker, "/STACK:102400000,102400000") const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; /* 6 2 0 1 1 0 0 1 */ int n,r,a[MAXN]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); while(cin>>n>>r) { mem(a,); ; ; ; i<=n; ++i) { cin>>temp; if(temp) { a[++cnt]=i; } } temp=; ; ; -)<n||a[]-r+>||a[cnt]+r-<n) cout<<-<<endl; else { ; i<=cnt; ++i) { >temp+) { //cout<<temp<<' '<<a[i]-r+1<<endl; cout<<-<<endl; flag=; break; } if(temp>=n) { //k++; break; } <=temp+&&a[i+]-r+>temp+)||a[i]+r->=n) { //cout<<i<<endl; k++; temp=a[i]+r-; } //cout<<temp<<' '; } if(!flag) cout<<k<<endl; } } }
B - Heaters
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #define lson i<<1 #define rson i<<1|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define pb(x) push_back(x) #define enld endl #define mian main #define itn int #define prinft printf #pragma GCC optimize(2) //#pragma comment(linker, "/STACK:102400000,102400000") const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; /* 8 L 1 R 2 R 3 ? 2 L 4 ? 1 L 5 ? 1 */ int n; char c; int ind; int a[MAXN]; int main() { //std::ios::sync_with_stdio(false); //cin.tie(NULL); while(cin>>n) { ,r=; mem(a,); while(n--) { cin>>c>>ind; if(c=='L') { a[ind]=l--; } else if(c=='R') { a[ind]=r++; } else { //cout<<a[ind]<<' '<<a[l+1]<<' '<<l+1<<endl; cout<<min(a[ind]-(l+),r--a[ind])<<endl; } } } }
C - Books Queries
逆推+二分
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #define lson i<<1 #define rson i<<1|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define pb(x) push_back(x) #define enld endl #define mian main #define itn int #define prinft printf #pragma GCC optimize(2) //#pragma comment(linker, "/STACK:102400000,102400000") const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int n,m,k; int a[MAXN]; int b[MAXN]; bool check(int x) { mem(b,); ; ; i<=x; ++i) { ; for(int j=temp; j<=m; ++j) { if(b[j]+a[i]<=k) { flag=; b[j]+=a[i]; temp=j; break; } } if(!flag) return false; } return true; } int main() { //std::ios::sync_with_stdio(false); //cin.tie(NULL); while(cin>>n>>m>>k) { mem(a,),mem(b,); ; i--) cin>>a[i]; ,r=n+,mid,cnt=; while(cnt--) { mid=(l+r)/; if (check(mid)) l=mid; else r=mid; } cout<<mid<<endl; } /* while(cin>>n>>m>>k) { for(int i=n; i>=1; i--) cin>>a[i]; int x; cin>>x; cout<<check(x)<<endl; } */ }
D - Boxes Packing