首页 > ACM题库 > HDU-杭电 > hdu 2275 Kiki & Little Kiki 1[解题报告]C++
2014
01-04

hdu 2275 Kiki & Little Kiki 1[解题报告]C++

Kiki & Little Kiki 1

问题描述 :

Kiki is considered as a smart girl in HDU, many boys fall in love with her! Now, kiki will finish her education, and leave school, what a pity! One day, zjt meets a girl, who is like kiki very much, in the campus, and calls her little kiki. Now, little kiki want to design a container, which has two kinds of operation, push operation, and pop operation.
Push M:
  Push the integer M into the container.
Pop M:
  Find the maximal integer, which is not bigger than M, in the container. And pop it from the container. Specially, for all pop operations, M[i] is no bigger than M[i+1].
Although she is as smart as kiki, she still can’t solve this problem! zjt is so busy, he let you to help little kiki to solve the problem. Can you solve the problem?

输入:

The input contains one or more data sets. At first line of each input data set is an integer N (1<= N <= 100000) indicate the number of operations. Then N lines follows, each line contains a word (“Push” or “Pop”) and an integer M. The word “Push” means a push operation, while “Pop” means a pop operation. You may assume all the numbers in the input file will be in the range of 32-bit integer.

输出:

The input contains one or more data sets. At first line of each input data set is an integer N (1<= N <= 100000) indicate the number of operations. Then N lines follows, each line contains a word (“Push” or “Pop”) and an integer M. The word “Push” means a push operation, while “Pop” means a pop operation. You may assume all the numbers in the input file will be in the range of 32-bit integer.

样例输入:

9
Push 10
Push 20
Pop 2
Pop 10
Push 5
Push 2
Pop 10
Pop 11
Pop 19
3
Push 2
Push 5
Pop 2

样例输出:

No Element!
10
5
2
No Element!

2

题意:n个操作

Push 入容器

Pop弹出一个 满足<=该数的最大的数(若没有输出No Element!)

开始用set打了一遍wrong了,这里入容器的数是有重复的,所以用multiset

 

#include<stdio.h>
#include<set>
using namespace std;
multiset<int>ss;
multiset<int>::iterator p,q;
int main(){
    int n,t; char c[5];
    while(~scanf("%d",&n)){
        ss.clear();
        while(n--){
            scanf("%s %d",c,&t);
            if(c[1]=='u')ss.insert(t);
            else {
				if(ss.size()==0 || *ss.begin() > t){printf("No Element!\n");continue;}
				ss.insert(t);
				q=ss.find(t);//返回第一个等于t的迭代器
				p=q;	p++;
				if(p!=ss.end() && *p==t)t=*p;
				else {p--;p--;t=*p;}
				printf("%d\n",t);
				ss.erase(p); ss.erase(q);
            }
        }
        printf("\n");
    }
    return 0;
}
/*
7
Push 2
Push 5
Pop 2
Pop 3
Pop 4
Pop 5
Pop 6

*/

解题转自:http://blog.csdn.net/acmmmm/article/details/10023763


  1. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。