zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Educational Codeforces Round 89 (Rated for Div. 2) A Shovels and Swords B、Shuffle

for and Codeforces div round shuffle 89 Educational
2023-09-27 14:26:03 时间

题目链接:A、Shovels and Swords

 

题意:

你需要一个木棍和两个钻石可以造出来一把剑

你需要两个木棍和一个钻石可以造出来一把铁锹

你现在有a个木棍,b个钻石,问你最多可以造出来几件东西

 

题解:

分两种情况,第一种:

如果max(a,b)>=2*min(a,b),那么最多可以造出来min(a,b)件物品

 

第二种:

排除第一种情况后,假设我们最多造出来了x把铁锹,y把剑

2x+y<=a

x+2y<=b

两个式子相加得到:

3(x+y)<=a+b,即x+y<=(a+b)/3

 

或者你可以这样解释,我们刚开始先假设a>=b,因为最佳分配肯定是让剩余数量多的材料减去2,那么也就是a-=2,b-=1。一旦a<b,那就b-=2,a-=1。你会发现无论怎样a+b的数量都要大于3

 

 

代码:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<queue>
 6 #include<deque>
 7 #include<string.h>
 8 #include<map>
 9 #include <iostream>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 const int maxn=2e5+10;
14 int main()
15 {
16     int t;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int a,b,sum=0;
21         scanf("%d%d",&a,&b);
22         if(a<b) swap(a,b);
23         if(a>=2*b) printf("%d\n",b);
24         else
25         {
26             printf("%d\n",(a+b)/3);
27         }
28     }
29     return 0;
30 }
View Code

 

 

题目链接:B、Shuffle

 

题意:

给你一个n个长度的数组(下标从1开始),初始这个数组每一个下标对应的值都是0,只有下标为x那个位置对应的元素值为1。然后题目中给出来m个区间,你可以在区间[l,r]中挑选出来任意两个位置l<=a<=b<=r

然后你可以让下标为a和下标为b所对应的元素值交换。问你最后有多少位置可以变成1

 

题解:

那下面样例做例子:

3 3 2
2 3
1 2

那么v[1]=v[2]=0,v[3]=1;

在区间[2,3]中,我们可以让v[2],v[3]交换,这样的话v[2]就有可能为1,第三个区间[1,2],因为v[2]有可能为1,那么v[1]和v[2]交换之后,那么v[1]也有可能为1

所以答案就是3

 

模拟题,具体实现见代码:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<queue>
 6 #include<deque>
 7 #include<string.h>
 8 #include<map>
 9 #include <iostream>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 const int maxn=2e5+10;
14 int main()
15 {
16     int t;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int n,x,m;
21         scanf("%d%d%d",&n,&x,&m);
22         int start=x,last=x;
23         while(m--)
24         {
25             int a,b;
26             scanf("%d%d",&a,&b);
27             if(a<=start && b>=last)
28             {
29                 start=a;
30                 last=b;
31             }
32             else if(start<=a && last>=a)
33             {
34                 last=max(last,b);
35             }
36             else if(start<=b && last>=b)
37             {
38                 start=min(start,a);
39             }
40         }
41         printf("%d\n",last-start+1);
42     }
43     return 0;
44 }
View Code