CodeForces - 830B(47/600)

本文介绍了一种基于特定规则的卡片排序算法,通过不断查找并移除当前牌堆中数值最小的卡片来实现排序过程。该算法详细说明了如何计算整个排序过程中查看卡片的总次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn’t know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, …, an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output
Print the total number of times Vasily takes the top card from the deck.

Example
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
Note
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.

不多比比照着题意模拟

#include<bits/stdc++.h>
using namespace std;
#define int long long 
struct p
{
    int dx,xb,djd;
    bool operator < (const p&a)const
    {
        if(dx==a.dx)return xb<a.xb;
        return dx<a.dx;
    }
};
p tu[100001];
int jg=0,xb[100001],zhi[100001],djd=0,gs[100001];
main()
{
    int n;
    cin>>n;
    for(int a=1;a<=n;a++)scanf("%I64d",&tu[a].dx);
    for(int a=1;a<=n;a++)tu[a].xb=a;
    sort(tu+1,tu+n+1);
    for(int a=1;a<=n;a++)
    {
        if(tu[a].dx==tu[a-1].dx)tu[a].djd=tu[a-1].djd;
        else tu[a].djd=tu[a-1].djd+1;
        djd=max(djd,tu[a].djd);
    }
//  for(int a=1;a<=n;a++)ceshi[a]=tu[a].djd;
    jg=1;
    for(int a=1,b=1;a<=djd;a++)
    {
        int bj=0;
        if(tu[b].xb>xb[jg])
        {
            gs[jg]++;
            xb[jg]=tu[b].xb;
            zhi[jg]=tu[b].dx;
        }
        else
        {
            jg++;
            gs[jg]++;
            xb[jg]=tu[b].xb;
            zhi[jg]=tu[b].dx;
            bj++;
        }
        b++;
        for(int c=1;b<=n;b++,c++)
        {
            if(tu[b].djd!=a)break;
            if(bj)
            {
                if(tu[b].xb>xb[jg-1])
                {
                    gs[jg-1]++;
                    xb[jg-1]=tu[b].xb;
                    zhi[jg-1]=tu[b].dx;
                }
                else
                {
                    gs[jg]++;
                    xb[jg]=tu[b].xb;
                    zhi[jg]=tu[b].dx;
                }
            }
            else
            {
                gs[jg]++;
                xb[jg]=tu[b].xb;
                zhi[jg]=tu[b].dx;
            }
        }
    }
    int daan=n;
    int tt=0;
    for(int a=1;a<=jg;a++)
    {
        tt+=gs[a];
        daan+=n-tt;
    }
    cout<<daan;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值