F. Mentors

F. Mentors
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In BerSoft
n
programmers work, the programmer
i
is characterized by a skill
r
i
.

A programmer
a
can be a mentor of a programmer
b
if and only if the skill of the programmer
a
is strictly greater than the skill of the programmer
b

(
r
a
>
r
b
)
and programmers
a
and
b
are not in a quarrel.

You are given the skills of each programmers and a list of
k
pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer
i
, find the number of programmers, for which the programmer
i
can be a mentor.

Input
The first line contains two integers
n
and
k

(
2

n

2

10
5
,
0

k

min
(
2

10
5
,
n

(
n

1
)
2
)
)
— total number of programmers and number of pairs of programmers which are in a quarrel.

The second line contains a sequence of integers
r
1
,
r
2
,

,
r
n

(
1

r
i

10
9
)
, where
r
i
equals to the skill of the
i
-th programmer.

Each of the following
k
lines contains two distinct integers
x
,
y

(
1

x
,
y

n
,
x

y
)
— pair of programmers in a quarrel. The pairs are unordered, it means that if
x
is in a quarrel with
y
then
y
is in a quarrel with
x
. Guaranteed, that for each pair
(
x
,
y
)
there are no other pairs
(
x
,
y
)
and
(
y
,
x
)
in the input.

Output
Print
n
integers, the
i
-th number should be equal to the number of programmers, for which the
i
-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.

Examples
inputCopy
4 2
10 4 10 15
1 2
4 3
outputCopy
0 0 1 2
inputCopy
10 4
5 4 1 5 4 3 7 1 2 5
4 6
2 1
10 8
3 5
outputCopy
5 4 0 5 3 3 9 0 2 5
Note
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int b[200010],n,m,i;
struct node
{
   int p,ans;
}a[200010];
bool cmp(int x,int y)
{
    return x<y;
}
void searchp()
{
    //int n,m;//这个地方,全局变量啊全局变量全局变量,如果再重新定义一遍,就只能在这个函数里用了,在主函数里相当于这里没有输入M,怪不得输不进去矛盾关系
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i].p);
        b[i]=a[i].p;
    }
    sort(b+1,b+1+n,cmp);
    for(i=1;i<=n;i++)
    {
        struct node k=a[i];
        a[i].ans=lower_bound(b+1,b+1+n,k.p)-b-1;
    }
}
int main()
{
    searchp();
    while(m--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(a[x].p<a[y].p)
        {
            a[y].ans--;
        }
        else if(a[y].p<a[x].p)
        {
            a[x].ans--;
        }
    }
    for(i=1;i<=n;i++)
    {
        printf("%d ",a[i].ans);
    }
    return 0;
}

题意:就是说找领导人,只能是能力大的人领导能力小的人,相等都不行,所以先把能力排好序,在二分查找找出每个人能管多少个人,二分查找就是先把要找的数排好序放大一个数组里,然后对这个数组进行操作,决定是用upper_bound还是lower_bound,对这个题来说,如果能力相等,这个是不能领导的,所以要减1,然后查找出有几个可以领到的小跟班,最后再处理矛盾关系,有矛盾关系的那就能力大的人少一个小跟班,最后输出能力;
小结:就是考的二分查找
以下为转载:
这里写图片描述

这里写图片描述

from PyQt6.QtCharts import QChartView, QBarSet, QBarSeries, QChart, QBarCategoryAxis, QValueAxis from PyQt6.QtCore import QPoint, Qt from PyQt6.QtGui import QColor, QCursor, QFont from PyQt6.QtWidgets import QWidget, QHBoxLayout, QToolTip class BarChartWidget(QWidget): """封装柱状图组件""" def __init__(self, parent=None): super().__init__(parent) self.mentors = ["导师A", "导师B", "导师C", "导师D", "导师E"] self.completed_data = [6, 4, 8, 4, 5] # 已完成数据 self.pending_data = [2, 3, 1, 5, 3] # 未完成数据 self.initUI() self.initConnections() def initUI(self): """初始化图表界面""" layout = QHBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) # 创建图表组件 self.chart = self._create_chart() self.chart_view = QChartView(self.chart) self.chart_view.setMouseTracking(True) layout.addWidget(self.chart_view) def initConnections(self): """初始化信号连接""" # 连接悬停事件(绑定双数据集) self.bar_series.hovered.connect(lambda state, index, barset: self.show_tooltip(state, index, barset) ) def _create_chart(self): """创建并配置图表""" # 创建双数据集 self.completed_set = QBarSet("已完成") self.completed_set.append(self.completed_data) self.completed_set.setColor(QColor("#4285F4")) self.pending_set = QBarSet("未完成") self.pending_set.append(self.pending_data) self.pending_set.setColor(QColor("#FBBC05")) # 创建柱状图系列 self.bar_series = QBarSeries() self.bar_series.append(self.completed_set) self.bar_series.append(self.pending_set) self.bar_series.setBarWidth(0.4) self.bar_series.setLabelsVisible(True) self.bar_series.setLabelsFormat("@value") self.bar_series.setLabelsPosition(QBarSeries.LabelsPosition.LabelsCenter) # 创建图表 chart = QChart() chart.addSeries(self.bar_series) # 配置X轴 self.axis_x = QBarCategoryAxis() self.axis_x.append(self.mentors) chart.addAxis(self.axis_x, Qt.AlignmentFlag.AlignBottom) self.bar_series.attachAxis(self.axis_x) # 配置Y轴 max_value = max(max(self.completed_data), max(self.pending_data)) * 1.2 self.axis_y = QValueAxis() self.axis_y.setRange(0, max_value) self.axis_y.setLabelFormat("%.0f") chart.addAxis(self.axis_y, Qt.AlignmentFlag.AlignLeft) self.bar_series.attachAxis(self.axis_y) return chart def show_tooltip(self, state, index, barset): """显示/隐藏提示框""" if state: # 获取当前柱形所属的数据集 if barset == self.completed_set: completed = self.completed_set.at(index) pending = self.pending_set.at(index) else: completed = self.completed_set.at(index) pending = self.pending_set.at(index) tooltip_text = f"导师: {self.mentors[index]}\n已完成: {completed}\n未完成: {pending}" # 定位到鼠标正下方 tooltip_pos = QCursor.pos() + QPoint(0, 15) QToolTip.setFont(QFont("Arial", 10)) QToolTip.showText(tooltip_pos, tooltip_text) else: QToolTip.hideText() 设置 BarChartWidget的高度
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值