Bad Hair Day

题目描述:

Some of Farmer John’s N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows’ heads.

Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:

    =

= =
= - = Cows facing right -->
= = =
= - = = =
= = = = = =
1 2 3 4 5 6
Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow’s hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow’s hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

输入:

Line 1: The number of cows, N.
Lines 2…N+1: Line i+1 contains a single integer that is the height of cow i.

输出:

Line 1: A single integer that is the sum of c 1 through cN.

样例输入:

6
10
3
7
4
12
2

样例输出:

5

很经典的一个单调栈的使用,通过记录在该位置,能看到他的头发的牛的数量最终确定牛数

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	long long n;
	scanf("%lld",&n);
	long long gao;
	scanf("%lld",&gao);
	stack<long long> feng;
	feng.push(gao);
	long long ans=0;
	for(int i=1;i<n;i++)
	{
		scanf("%lld",&gao);
		while(!feng.empty()&&gao>=feng.top())
		{
			feng.pop();
		}
		ans+=feng.size();
		feng.push(gao);
	}
	printf("%lld\n",ans);
	return 0;
}

还是想要吐槽一下,有的时候翻译真的不是很好用

<think>我们原始SQL中,关于时间条件的部分在子查询d中:子查询d:selectaccount,sum(bad_day_total)bad_day_total,...fromt_bad_user_last7dwherestat_date='2025-05-01'--这里需要修改groupbyaccount现在要求将时间范围改为从2025年5月28日到2025年6月3日。因此,我们只需要将原来的固定日期条件改为BETWEEN条件即可:将:wherestat_date='2025-05-01'改为:wherestat_dateBETWEEN'2025-05-28'AND'2025-06-03'注意:由于我们是在子查询d中按account分组,并且对各个bad_day_*字段求和,而这里的时间范围已经变成了一周(7天),所以这个分组求和就是这一周内的总和。因此,修改后的完整SQL如下:</think> 要修改 SQL 的时间范围为 **2025年5月28日到2025年6月3日**,只需将原始语句中的固定日期条件: ```sql where stat_date = '2025-05-01' ``` 替换为范围条件: ```sql where stat_date BETWEEN '2025-05-28' AND '2025-06-03' ``` ### 修改后的完整 SQL 语句 ```sql SELECT region, COUNT(account), SUM(CASE WHEN bad_day_total > 0 THEN 1 ELSE 0 END), SUM(CASE WHEN bad_day_home > 0 THEN 1 ELSE 0 END), SUM(CASE WHEN bad_day_network > 0 THEN 1 ELSE 0 END), SUM(CASE WHEN bad_day_resource > 0 THEN 1 ELSE 0 END) FROM ( SELECT d.*, c.region FROM ( SELECT a.*, b.region FROM t447_total_user_attr_d a INNER JOIN tbl_invt_bb_user_addr_202505_1 b ON a.account = b.kdacct ) c INNER JOIN ( SELECT account, SUM(bad_day_total) bad_day_total, SUM(bad_day_home) bad_day_home, SUM(bad_day_network) bad_day_network, SUM(bad_day_resource) bad_day_resource FROM t_bad_user_last7d WHERE stat_date BETWEEN '2025-05-28' AND '2025-06-03' -- 修改核心时间范围 GROUP BY account ) d ON c.account = d.account ) AS final_data GROUP BY region; ``` ### 关键修改说明 1. **时间范围调整**: - 原条件:`stat_date = '2025-05-01'`(单日) - 新条件:`stat_date BETWEEN '2025-05-28' AND '2025-06-03'`(范围查询) 2. **聚合逻辑不变**: - `SUM(CASE...)` 统计各类型异常用户数量 - `COUNT(account)` 统计总用户数 - `GROUP BY region` 按区域分组统计[^1] ### 注意事项 - **日期格式**:确保日期格式 `'YYYY-MM-DD'` 与表中数据一致 - **性能优化**:若数据量过大,可在 `t_bad_user_last7d.stat_date` 添加索引 - **边界包含**:`BETWEEN` 包含起止日期(即包含 2025-05-28 和 2025-06-03) **动态时间范围实现参考**(如需要未来扩展): ```sql -- 动态计算过去30天 WHERE stat_date BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE -- 动态计算自然周 WHERE YEARWEEK(stat_date, 1) = YEARWEEK(CURRENT_DATE, 1) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值