题目1369:字符串的排列

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100
using namespace std;
int main()
{
    int length;
    char str[MAX];
    while(gets(str)){
    length = strlen(str);
    sort(str, str + length);
    puts(str);
    while (next_permutation(str, str + length))
    {
        puts(str);
    }
    }
    return 0;
}
/**************************************************************
    Problem: 1369
    User: LiuJ
    Language: C++
    Result: Accepted
    Time:70 ms
    Memory:1020 kb
****************************************************************/

#include <stdio.h>
 
int n;
int d[100001];
int tl, tr;
 
int partition(int l, int r)
{
    int p = d[l];
    while (l < r)
    {
        while (l < r && d[r] >= p)
            r--;
        d[l] = d[r];
        while (l < r && d[l] <= p)
            l++;
        d[r] = d[l];
    }
    d[l] = p;
    return l;
}
 
void quick_select(int l, int r, int k)
{
    int p = partition(l, r);
    if (p == k)
        return;
    if (tl != -1 && tr != -1 && tl == tr)
        return;
    if (p < k)
    {
        tl = d[p];
        quick_select(p+1, r, k);
    }
    else
    {
        tr = d[p];
        quick_select(l, p-1, k);
    }
}
 
int main()
{
    int i;
    while (scanf("%d", &n) != EOF)
    {
        for (i = 0; i < n; i++)
            scanf("%d", &d[i]);
 
        tl = tr = -1;
        quick_select(0, n-1, (n-1)/2);
        int tot = 0;
        int t = d[(n-1)/2];
        for (i = 0; i < n; i++)
            tot += (d[i] == t);
        if (tot > n/2)
            printf("%d\n", t);
        else
            puts("-1");
    }
    return 0;
}
 
/**************************************************************
    Problem: 1370
    User: cust123
    Language: C++
    Result: Accepted
    Time:860 ms
    Memory:1404 kb
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值