【PAT】PAT_1171_Replacement_Selection

题目分析

根据题意,每趟在N个数字中遍历,最终选择框内会保留M个数字,这M个数字中一定是本次遍历中能取到的最大的M个数字。
设这M数字中的最大值为r_MAX,最小值为r_MIN。则任意数t可以根据以下规则分类:

  1. index(t) <= index(r_MIN) 一定会被输出

  2. index(r_MIN)<index(t)<=index(r_MAX)

     a.  t>r_MIN 则t属于M个数字中的一个,本轮输出
    
     b.  t<r_MIN 则t 属于下一轮遍历
    
  3. index(t)>index(r_MAX) ,此时所需要取的M个数字已经取满,等待下一轮遍历。

综合上面的分类:

每轮输出的数字一定是该轮中最大的M个数字,该轮M个数字中最小的数字前面的所有数字。

以上分析实践后,发现有问题,代码采用模拟方式

代码

参考资料

#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int A = 100010;
int NUM[A];

int main()
{
    int N, M;
    vector<int> current, next;
    priority_queue<int, vector<int>, greater<int>> q;
    scanf("%d %d", &N, &M);
    for (int i = 0; i < N; i++) {
        scanf("%d", &NUM[i]);
    }
    int index = 0, count = 0,last;
    for (; index < M; index++) q.push(NUM[index]);
    while (count != N) {
        last = q.top();
        current.push_back(last);
        q.pop();
        count++;
        if (index < N) {
            if (last < NUM[index]) {
                q.push(NUM[index++]);
            }
            else {
                next.push_back(NUM[index++]);
            }
        }
        if (q.empty()) {
            //输出当前
            for (int i = 0; i < current.size(); i++) {
                if (i != 0)printf(" ");
                printf("%d", current[i]);
            }
            printf("\n");
            current.clear();
            //
            for (int i = 0; i < next.size(); i++) {
                q.push(next[i]);
            }
            next.clear();
            
        }
    }
}```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Notepad++ v8 new features, enhancements & bug-fixes: 1. Add Dark Mode. 2. ARM64 build is available. 3. One button to build them all - build Notepad++ with Scintilla static lib and Boost RegExpr headers. 4. Add new Fluent UI icons for toolbar. 5. Add new feature "Distraction Free Mode" (Menu "View->Distraction Free Mode"). 6. Add new API NPPM_ADDTOOLBARICON_FORDARKMODE for dark mode. 7. Add 1 click action (SHIFT-click) on close button to close all tabs in dockable panel. 8. Add ability of changing select text foreground color (optional). 9. Allow Replace to stop after replacement (optional). 10. Fix append extension feature not working in save dialog. 11. Add ability to reverse line order. 12. Add ability to style only current instance of text. 13. Fix duplicated entries in Auto-Completion pop-up window. 14. Fix Python Function List not showing functions in some circumstance. 15. Enhance Folder as Workspace performance while adding/removing files in bulk. 16. Add Ada, Fortran, Fortran77 & Haskell in function lists. 17. Improve performance of "Open all" command in Search results. 18. Add "Copy Pathnames" command to Search results context menu. 19. Catch regex search exceptions and show exception message. 20. Add MarkAll Preference settings for case and word. 21. Fix regression: Handle "Default Directory" setting correctly in Open/Save File Dialog. 22. Fix a special character in UTF16 file crash issue 23. Add "Append extension" checkbox to Save As dialog. 24. Fix Copy command in Search result is available as there's no selection. 25. Add padding ability in the edit zone. 26. Make new tab name translatable. 27. Improve character case handling in RegEx. 28. Fix dragged out UDL file is not applied to UDL in the new instance. 29. Add command line parameter for adding specified string to app title bar. 30. Fix Auto-Completion ignoring case issue. 31. Fix "Match Whole Word" option being enabled in RegEx Search. 32. Fix sort with column key selection that appears af

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值