Codeforces Round #515 (Div. 3)

本文解析了Codeforces平台上的四道编程题目,包括Vova and Train、Heaters、Books Queries及Boxes Packing,通过代码示例详细介绍了每题的解题思路与算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #515 (Div. 3)

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 #define lson i<<1
12 #define rson i<<1|1
13 #define LS l,mid,lson
14 #define RS mid+1,r,rson
15 #define mem(a,x) memset(a,x,sizeof(a))
16 #define gcd(a,b) __gcd(a,b)
17 #define ll long long
18 #define ull unsigned long long
19 #define lowbit(x) (x&-x)
20 #define pb(x) push_back(x)
21 #define enld endl
22 #define mian main
23 #define itn int
24 #define prinft printf
25 #pragma GCC optimize(2)
26 //#pragma comment(linker, "/STACK:102400000,102400000")
27 
28 const double PI = acos (-1.0);
29 const int INF = 0x3f3f3f3f;
30 const int EXP = 1e-8;
31 const int N = 1e5 + 5;
32 const int MOD = 1e9 + 7;
33 const int MAXN = 505;
34 
35 using namespace std;
36 
37 /*
38 4
39 10 2 3 7
40 100 51 51 51
41 1234 1 100 199
42 1000000000 1 1 1000000000
43 */
44 int n,L,v,l,r;
45 int main() {
46     std::ios::sync_with_stdio(false);
47     cin.tie(NULL);
48 
49     while(cin>>n) {
50         while(n--) {
51             cin>>L>>v>>l>>r;
52             cout<<L/v-(r/v-(l-1)/v)<<endl;
53         }
54     }
55 }
A - Vova and Train

 

分区间讨论,不断更新右极值,注意边界相切的情况(1,2)(3,4)

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 #define lson i<<1
12 #define rson i<<1|1
13 #define LS l,mid,lson
14 #define RS mid+1,r,rson
15 #define mem(a,x) memset(a,x,sizeof(a))
16 #define gcd(a,b) __gcd(a,b)
17 #define ll long long
18 #define ull unsigned long long
19 #define lowbit(x) (x&-x)
20 #define pb(x) push_back(x)
21 #define enld endl
22 #define mian main
23 #define itn int
24 #define prinft printf
25 #pragma GCC optimize(2)
26 //#pragma comment(linker, "/STACK:102400000,102400000")
27 
28 const double PI = acos (-1.0);
29 const int INF = 0x3f3f3f3f;
30 const int EXP = 1e-8;
31 const int N = 1e5 + 5;
32 const int MOD = 1e9 + 7;
33 const int MAXN = 2000;
34 
35 using namespace std;
36 
37 /*
38 6 2
39 0 1 1 0 0 1
40 */
41 int n,r,a[MAXN];
42 int main() {
43     std::ios::sync_with_stdio(false);
44     cin.tie(NULL);
45 
46     while(cin>>n>>r) {
47         mem(a,0);
48         int cnt=0;
49         int temp=0;
50         for(int i=1; i<=n; ++i) {
51             cin>>temp;
52             if(temp) {
53                 a[++cnt]=i;
54             }
55         }
56         temp=0;
57         int k=0;
58         int flag=0;
59         if(cnt*(r*2-1)<n||a[1]-r+1>1||a[cnt]+r-1<n) cout<<-1<<endl;
60         else {
61             for(int i=1; i<=cnt; ++i) {
62                 if(a[i]-r+1>temp+1) {
63                     //cout<<temp<<' '<<a[i]-r+1<<endl;
64                     cout<<-1<<endl;
65                     flag=1;
66                     break;
67                 }
68                 if(temp>=n) {
69                     //k++;
70                     break;
71                 }
72                 else if((a[i]-r+1<=temp+1&&a[i+1]-r+1>temp+1)||a[i]+r-1>=n) {
73                     //cout<<i<<endl;
74                     k++;
75                     temp=a[i]+r-1;
76                 }
77                 //cout<<temp<<' ';
78             }
79             if(!flag) cout<<k<<endl;
80         }
81     }
82 }
B - Heaters

 

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 #define lson i<<1
12 #define rson i<<1|1
13 #define LS l,mid,lson
14 #define RS mid+1,r,rson
15 #define mem(a,x) memset(a,x,sizeof(a))
16 #define gcd(a,b) __gcd(a,b)
17 #define ll long long
18 #define ull unsigned long long
19 #define lowbit(x) (x&-x)
20 #define pb(x) push_back(x)
21 #define enld endl
22 #define mian main
23 #define itn int
24 #define prinft printf
25 #pragma GCC optimize(2)
26 //#pragma comment(linker, "/STACK:102400000,102400000")
27 
28 const double PI = acos (-1.0);
29 const int INF = 0x3f3f3f3f;
30 const int EXP = 1e-8;
31 const int N = 1e5 + 5;
32 const int MOD = 1e9 + 7;
33 const int MAXN = 2e5+5;
34 
35 using namespace std;
36 
37 /*
38 8
39 L 1
40 R 2
41 R 3
42 ? 2
43 L 4
44 ? 1
45 L 5
46 ? 1
47 */
48 int n;
49 char c;
50 int ind;
51 int a[MAXN];
52 int main() {
53     //std::ios::sync_with_stdio(false);
54     //cin.tie(NULL);
55 
56     while(cin>>n) {
57         int l=0,r=1;
58         mem(a,0);
59         while(n--) {
60             cin>>c>>ind;
61             if(c=='L') {
62                 a[ind]=l--;
63             } else if(c=='R') {
64                 a[ind]=r++;
65             } else {
66                 //cout<<a[ind]<<' '<<a[l+1]<<' '<<l+1<<endl;
67                 cout<<min(a[ind]-(l+1),r-1-a[ind])<<endl;
68             }
69         }
70 
71     }
72 }
C - Books Queries

 

逆推+二分

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 #define lson i<<1
12 #define rson i<<1|1
13 #define LS l,mid,lson
14 #define RS mid+1,r,rson
15 #define mem(a,x) memset(a,x,sizeof(a))
16 #define gcd(a,b) __gcd(a,b)
17 #define ll long long
18 #define ull unsigned long long
19 #define lowbit(x) (x&-x)
20 #define pb(x) push_back(x)
21 #define enld endl
22 #define mian main
23 #define itn int
24 #define prinft printf
25 #pragma GCC optimize(2)
26 //#pragma comment(linker, "/STACK:102400000,102400000")
27 
28 const double PI = acos (-1.0);
29 const int INF = 0x3f3f3f3f;
30 const int EXP = 1e-8;
31 const int N = 1e5 + 5;
32 const int MOD = 1e9 + 7;
33 const int MAXN = 2e5+10;
34 
35 using namespace std;
36 
37 int n,m,k;
38 int a[MAXN];
39 int b[MAXN];
40 
41 bool check(int x) {
42     mem(b,0);
43     int temp=1;
44     for(int i=1; i<=x; ++i) {
45         int flag=0;
46         for(int j=temp; j<=m; ++j) {
47             if(b[j]+a[i]<=k) {
48                 flag=1;
49                 b[j]+=a[i];
50                 temp=j;
51                 break;
52             }
53         }
54         if(!flag)
55             return false;
56     }
57     return true;
58 }
59 
60 int main() {
61     //std::ios::sync_with_stdio(false);
62     //cin.tie(NULL);
63 
64     while(cin>>n>>m>>k) {
65         mem(a,0),mem(b,0);
66         for(int i=n; i>=1; i--)
67             cin>>a[i];
68         int l=m-1,r=n+1,mid,cnt=200;
69         while(cnt--) {
70             mid=(l+r)/2;
71             if (check(mid))
72                 l=mid;
73             else
74                 r=mid;
75         }
76         cout<<mid<<endl;
77     }
78     /*
79     while(cin>>n>>m>>k) {
80         for(int i=n; i>=1; i--)
81             cin>>a[i];
82         int x;
83         cin>>x;
84         cout<<check(x)<<endl;
85     }
86     */
87 }
D - Boxes Packing

转载于:https://www.cnblogs.com/chunibyo/p/9834707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值