惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
L
LangChain Blog
美团技术团队
N
Netflix TechBlog - Medium
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
博客园 - 司徒正美
爱范儿
爱范儿
D
DataBreaches.Net
月光博客
月光博客
U
Unit 42
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
腾讯CDC

Sehnsucht

记一次博客换图床的过程 观《花束般的恋爱》 读《活过》 东京 旅行篇 亲人逝去 我为什么想养鱼 防止AI爬取你的博客 「monthly 」博客重构 游戏全成就 好看的小说 人生的每一步都不会是浪费-2024年终 「weekly」星期五综合征 「weekly」听播客 杂谈 读《美丽新世界》 「weekly」社交媒体 恶性事件 新世界 「weekly」认知觉醒 美丽新世界 follow 「weekly」独立博客9问题 双十一买书 读《惊呆了!原来这就是社会学》 一次聊天与自我建设 2024-09月记 补番《relife》 树状数组简单理解 lvm简单使用 博客自动发布方案 某公众号废案 fail2ban基本使用 Podman 环境使用 Nginx Proxy Manager 最佳实践 2024-07同学聚会 vps使用podman部署freshrss Gitlab CICD 实践,思考与记录 装备升级:新的PC gitlab局域网搭建流程
2020牛客国庆集训派对day3 Leftbest
2020-10-03 · via Sehnsucht

题面

链接:https://ac.nowcoder.com/acm/contest/7830/A 来源:牛客网

题目描述 Jack is worried about being single for his whole life, so he begins to use a famous dating app. In this app, the user is shown single men/women’s photos one by one, and the user may choose between “yes” and “no”. Choosing “yes” means an invitation while choosing “no” means nothing. The photos would be shown one by one until the number of rest photos to be shown reaches zero. Of course, efficient and single Jack would always choose “yes”.

When viewing photos, Jack would have a “fake impression point” on every photo, which is not accurate. To calculate the “true impression point” of one photo, Jack would recall the “fake impression point” of every previous photo whose “fake impression point” is larger than this photo, and regard the smallest “fake impression point” of them as the “true impression point” of this photo. Jack would like to sum the “true impression point” of all photos as the outcome of his effort.

Note that if such a larger “fake impression point” does not exist, the “true impression point” of this photo is zero. 在这里插入图片描述

示例1 输入 4 2 1 4 3 输出 6

超时代码

#include<stdio.h>
int cb[100009];
int main()
{
	int n;
	scanf("%d",&n);
	{
		long long ans = 0;
		int min = 0;
		for(int i=0 ; i<n ; i++){
			scanf("%d",&cb[i]);
			min = 99991177;
			int qq = 0;
			for(int j=0 ; j<i ; j++){
				if(cb[j] > cb[i] && min > cb[j]){
					min = cb[j];
				}
			}
			if(min == 99991177){
				min = 0;
			}
			ans += min;
		}
		printf("%lld\n",ans);
	}
}

通过代码

#include<bits/stdc++.h>
using namespace std;
set<int>cb;
int main()
{
	int n;
	long long ans = 0;
	
	cb.clear();
	scanf("%d",&n);
	for(int i=0 ; i<n ; i++){
		int q;
		scanf("%d",&q);
		if(cb.upper_bound(q) != cb.end())
		ans += *cb.upper_bound(q);
		
		cb.insert(q);
	}
	printf("%lld\n",ans);
}

解释

使用STL中的set集合即可很快过题

头文件: #include<set>

简单用法:

  • clear()    删除set容器中的所有的元素
  • empty()     判断set容器是否为空
  • begin()    返回set容器的第一个元素的地址
  • size()     返回当前set容器中的元素个数
  • end()     返回指向容器最后一个数据单元+1的指针,如果我们要输出最后一个元素的值应该是 *(—c.end());

set中有这样一个函数:

  • upper_bound(int x)     返回容器中第一个比x大的数字的地址,找不到则返回end()

集合还有着元素不重复,并且元素自行升序排列的特点

题目思路: 每次输入的时候查找第一个比x大的数字,有则加到ans,无论有或者没有都加入集合,循环到结尾后输出ans即可