试题 C: 最大降雨量#(构造)
本题总分:10 分
【问题描述】
由于沙之国长年干旱,法师小明准备施展自己的一个神秘法术来求雨。
这个法术需要用到他手中的 49 张法术符,上面分别写着 1 至 49 这 49 个
数字。法术一共持续 7 周,每天小明都要使用一张法术符,法术符不能重复使用。
每周,小明施展法术产生的能量为这周 7 张法术符上数字的中位数。法术
施展完 7 周后,求雨将获得成功,降雨量为 7 周能量的中位数。
由于干旱太久,小明希望这次求雨的降雨量尽可能大,请大最大值是多少?
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
34
证明:无法找到比34更优的方案了。如下图,假设每周选的数已经排好序
第一周: x x x x x x x ( x1,x2,x3,x4,x5,x6 满足 x1 第二周: x x x x x x x
第三周: x x x x x x x
第四周: x x x x x x x
第五周: x x x x x x x
第六周: x x x x x x x
第七周: x x x x x x x
则标记红色的是每周的中位数:
第一周: x x x x x x x
第二周: x x x x x x x
第三周: x x x x x x x
第四周: x x x x x x x
第五周: x x x x x x x
第六周: x x x x x x x
第七周: x x x x x x x
此时不看第几周,每一行里:红色的x右边的x必然比x大。
假设上面的七行按行按照x由小到大重新排序后得到
x x x x1 x x x
x x x x2 x x x
x x x x3 x x x
x x x x4 x x x
x x x x5 x x x
x x x x6 x x x
x x x x7 x x x
则x1
问题是x4最大能取到多少呢?注意到上图中x4右下角(如下图)的元素都应比x4大。。(共15个)
因此答案为49-15=34
x x x x x x x
x x x x x x x
x x x x x x x
x x x x4 x x x
x x x x x x x
x x x x x x x
x x x x x x x
#include
#include
using namespace std;
int n = 30, m = 50;
string mp[35];
bool vis[35][55];
int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; // D L R U 字典序
//int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; // U R D L
struct node {
int x, y, step;
string str;
node (int _x, int _y, int _step, string _str) {
x = _x;
y = _y;
step = _step;
str = _str;
}
};
int main() {
for (int i = 0; i < n; i++) {
cin >> mp[i];
}
string s = "";
queue q;
q.push(node(0, 0, 0, ""));
vis[0][0] = true;
while (!q.empty()) {
node now = q.front();
q.pop();
//cout << now.x << " " << now.y << endl;
if (now.x == n-1 && now.y == m-1) {
cout << now.step << endl;
cout << now.str << endl;
}
for (int i = 0; i < 4; i++) {
int tx = now.x + dir[i][0];
int ty = now.y + dir[i][1];
if (tx<0 || tx>=n || ty<0 || ty>=m || vis[tx][ty] || mp[tx][ty]=='1') {
continue;
}
if (i == 0) {
s = "D";
} else if (i == 1) {
s = "L";
} else if (i == 2) {
s = "R";
} else {
s = "U";
}
/*if (i == 0) {
s = "U";
} else if (i == 1) {
s = "R";
} else if (i == 2) {
s = "D";
} else {
s = "L";
}*/
vis[tx][ty] = true;
q.push(node(tx, ty, now.step+1, now.str+s));
}
}
return 0;
}/*
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
186
UUUURRDRRRRRRRURRRUUULUURUUUUUUUUUUUURURURRDDDRRRRUUUURURRRRRDRRRURRUUURRRRDDRDDDDDDDDLLLDDDDRRRRDDLLLDDDDLLDDDLDDRRDRRDRDRRRURURRRRURURUULLLUURRUURUULUUULLUULLLULUUULUURRRRRRRRRUUUUUURR
UUUURRDRRRRRRURRRRUUULUURUUUUUUUUUUUURUURRRDRRDDRRUUUURURRRRRRURRDRRUUURRRRDDRDDDDDDDDLLLDDDDRRRRDDLLLDDDDLLDDDLDDRRDRRDRDRRRUURRRRRUURRUULLLUURRUURUULUUULLUULLLULUUULUURRRRRRRRRUUUUUURR
*/
/*2019蓝桥杯C语言A组 外卖店优先级
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
*/
#include
#include
#include
#define maxn 100000
using namespace std;
int b[maxn]={0}; //b存放每个店的订单数量
int book[maxn]={0}; //判断外卖店的优先级在过程中有没有大于过5,如果有,赋值为1
int book2[maxn]={0}; //判断某个时刻哪个外卖店有订单,如果有,则为1
struct waimai
{
int ts;
int id;
}a[maxn];
bool cmp(waimai a,waimai b)
{
return a.ts0)
b[l]--;
else
b[l]=0;
}
}
if(b[x]>5)
{
book[x]=1;
}
memset(book2,0,sizeof(book2));
}
/*for(int i=1;i<=n;i++)
{
if(book[i]==0&&b[i]>5)
count++;
if(book[i]==1&&b[i]>3)
count++;
printf("%d\n",b[i]);
}*/
//printf("店铺数量为:");
printf("%d\n",count);
return 0;
}
#include
using namespace std;
int main(){
int n,a[100001];
bool book[1000001]={0};//标记数组
cin>>n;
for(int i=0;i>a[i];
}
book[a[0]]=1;
for(int i=1;i
/*
2019第十届蓝桥杯省赛C++A组 I题糖果
*/
#include
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define Mst(a,b) memset(a,(b),sizeof(a))
using namespace std;
int dp[1<<20]; //二进制状态压缩
int a[105];
int main()
{
//freopen("in.txt","r",stdin);
int m,n,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
set v; //记录n包中所有糖果的种类数
memset(dp,0x3f,sizeof(int)*(1<200) continue; //用已有的状态去更新加上第i包糖果后的状态
int to = a[i]|state;
dp[to] = min(dp[to],1+dp[state]);
}
}
printf("%d\n",dp[ed]);
}
return 0;
}
另有一种解答:
#include
using namespace std;
int dp[1000005];
int n,m,k;
int s[105];//代表第i包糖果
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i