算法快排。

以前的我总以为排序就是指代冒泡排序。

直到写到了一题排序的题,提交答案直接给我时间超时间。。。后来再知道快速排序这个东西。

于是,我搞了一个下午才弄懂了这个算法(我是一个极水的人),所以来博客写上自己的思路巩固一下自己对着方面的认识。

快速排序:

简称快排,顾名思义,就是时间复杂度低,与冒泡排序相比之下,该算法更适合为一些长的没有序的数组进行排序。

思想:

我是先前设一个start   和end   分别记录数组的首尾。我们在随便选择一个作为比较。

我这里就把start作为比较,其实很简单,就是将比start大的放在他后面,比他小的放在他前面,这样进行一次调用就能将这个start的最终的位置决定,当然调用一次我们得到的依然是没有序的数组,所以要进行多次的调用,在这里又一个叫做分治算法(因为start最后的位置不在首尾,那么就把原先的数组一分为2,然后我们分别对左,右边进行快排。)

代码如下:

int quicksort(int a[],int start,int end)
{
int i,j,x=a[l];
i=start;           j=end;
if (start>=end)    

       return ;
while (start<end)
{
while (i<j&&a[j]>=x)    j--;
a[i]=a[j];
a[j]=x;
i++;
while (i<j&&a[i]<=x)    i++;
a[j]=a[i];
a[i]=x;
j--;
}
quicksort(a,start,i-1);
quicksort(a,i+1,end);     

}

*********************************该处是我后来补充**********************************

其实快排有头文件包含了,比如c++  的qsort(n,n+p)       但是我说的不是这个,下面的又一个方法

先附上代码:

#include<stdio.h>
#include<stdlib.h>                         //该有文件包含了qsort等需要的成分
int cmp(const void*a,const void*b)       //cmp 不是一定,可以改成其他,例如cmp改成mmp都是可以的,                                                                                   //   但是下面的 qsort中 的cmp也要相应变化

{

return (*(int *)a-*(int *)b);             //  a-b是正序      b-a是倒叙   


int main()
{
int n,i,a[1010];
scanf ("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(int),cmp);
for (i=0;i<n;i++) 
printf ("%d ",a[i]);
return 0;

}

********************第三次编辑*******************结构体快排***************

有些时候,时常遇见有多组数据要进行排序,且对应关系不能错乱。  

例如:   名字     身高      体重     扣分     

假设我们要对其进行扣分排序,  扣分少的在前,扣分多的在后   ,但是又不能对   名字,升高,体重这些对应关系错乱。所以这里引用了结构体变量的快排序。

代码:

#include<iostream>
#include<cstdlib>
using namespace std;
struct ln
{
int data,score;
}ss[110];
int cmp(const void *a,const void *b)
{
return (* (struct ln *)a).data-(*(struct ln *)b).data;      //这里是对   ss[].data  进行排序,且关系不错乱。  
}
int main()
{
int n,i;
cin>>n;
for (i=0;i<n;i++)
{
cin>>ss[i].data>>ss[i].score;

qsort (ss,n,sizeof (struct ln),cmp);
for (i=0;i<n;i++)
{
cout <<ss[i].data<<" "<<ss[i].score<<endl;
}
return 0;

}

截图   :

     

上图n =3    

i=0       data=2             score=5

i=1       data=3             score=4

i=2       data=1             score=9

排序后变成:

i=0       data=1             score=9

i=1       data=2             score=5

i=2       data=3             score=4               可以看出  data  和score的对应关系并没有错乱。

如题:

It is my great honour to introduce myself to you here.My name is Aloysius Benjy Cobweb Dartagnan Egbert Felix Gaspar Humbert Ignatius Jayden Kasper Leroy Maximilian.As a storyteller, today I decide to tell you and others a story about the hero Huriyyah, and the monsters.

Once upon a time, citizens in the city were suffering from nn powerful monsters.They ate small children who went out alone and even killed innocent persons.Before the hero appeared, the apprehension had overwhelmed the people for several decades.For the good of these unfortunate citizens, Huriyyah set off to the forest which was the main lair of monsters and fought with nn fierce and cruel monsters.The health point of the ii-th monster was HP_iHPi​, and its attack value was ATK_iATKi​.

They fought in a cave through a turn-based battle.During each second, the hero Huriyyah was attacked by monsters at first, and the damage was the sum of attack values of all alive monsters.Then he selected a monster and attacked it.The monster would suffer the damage of kk (its health point would decrease by kk) which was the times of attacks it had been came under.That is to say, for each monster, the damage of the first time that Huriyyah attacked it was 11, and the damage of Huriyyah's second attack to this monster was 22, the third time to this monster was 33, and so on.If at some time, the health point of a monster was less than or equal to zero, it died.The hero won if all monsters were killed.

Now, my smart audience, can you calculate the minimum amount of total damages our hero should suffer before he won the battle?

Input Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 10^3103.

For each test case, the first line contains an integers n~(1\le n\le 10^5)n (1≤n≤105) which is the number of monsters. The ii-th line of the following nn lines contains two integers HP_iHPi​ and ATK_i~(1\le HP_i, ATK_i\le 10^5)ATKi​ (1≤HPi​,ATKi​≤105) which describe a monster.

We guarantee that the sum of nn in all test cases is up to 10^6106.

Output Format

For each test case, output a line containing Case #x: y, where xx is the test case number starting from 11, and yy is the minimum amount of total damages the hero should suffer.

样例输入

2
3
1 1
2 2
3 3
3
3 1
2 2
1 3

样例输出

Case #1: 19
Case #2: 14

题意: 某个怪兽第一次收到英雄的攻击的伤害是1,第二次是2,第三次是3(依次类推)。

输入 t  测试次数   n 怪兽数目 , 下面n行,每行两整数,hp(血条),atk(怪兽攻击力)

英雄受到的伤害是所有活着的怪兽攻击力的总和(怪兽先攻击),求英雄最小受伤是多少。

思路:  先把每只怪兽,需多少次数打死的次数求出 ,设time      然后对着  time/atk  的值,由小到大打死怪兽,受伤值就是答案

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
struct s{
    int hp,k,time;
    double x;
}monster[100005];
ll ans;/*        
int cmp(const void *a,const void *b)       //   用于qsort     stdlib 头文件中
{
    return ((s *)a)->x-((s*)b)->x; 
}*/
/**/        
bool cmp(const s &b,const s &c)       //用于 sort          algorithm头文件中
{
    if(b.x!=c.x) return b.x<c.x;
    return b.k>c.k;
}
int main()
{
    int T,cnt=0;
    ll sum;
    cin>>T;
    while (T--){
        int i,n;
        sum=0;
        ans=0;
        cin>>n;
        for (i=0;i<n;i++){
            cin>>monster[i].hp>>monster[i].k;
            sum+=monster[i].k; 
            double t=sqrt(2*monster[i].hp-1.0/4)-0.5;
             if (t-(int)t<1e-8)
                monster[i].time=(int)t;
            else 
                monster[i].time=(int)t+1;
            monster[i].x=monster[i].time*1.0/monster[i].k;
        }    
//        qsort(monster,n,sizeof(struct s),cmp);
        sort(monster,monster+n,cmp);
        for (i=0;i<n;i++){
            for (int j=monster[i].time;j;j--){
                ans+=sum;
    //            cout<<"sum="<<sum<<endl;    
            }
            sum-=monster[i].k;
        }
    /*    cout<<ans<<endl;*/
/*        for (i=0;i<n;i++){
            printf("monster[%d].hp=%d -> k=%d -> time=%d -> x=%lf\n",i,monster[i].hp,monster[i].k,monster[i].time,monster[i].x);
        }*/
        printf("Case #%d: %lld\n",++cnt,ans); 
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1204675546/article/details/79115148
今日推荐