zoukankan      html  css  js  c++  java
  • [2010山东ACM省赛] Ivan comes again!(set 的使用)

    Ivan comes again!

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

    The Fairy Ivan gave Saya three problems to solve (Problem F). After Saya finished the first problem (Problem H), here comes the second.
    This is the enhanced version of Problem H.
    There is a large matrix whose row and column are less than or equal to 1000000000. And there are three operations for the matrix:
    1)
    add: Mark an element in the matrix. The element wasn’t marked before it is marked.
    2)remove: Delete an element’s mark. The element was marked before the element’s mark is deleted.
    3)find: Show an element’s row and column, and return a marked element’s row and column, where the marked element’s row and column are larger than the showed element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1.
    Of course, Saya comes to you for help again.

    输入

    The input consists of several test cases.
    The first line of input in each test case contains one integer N (0<N200000), which represents the number of operations.
    Each of the next N lines containing an operation, as described above.
    The last case is followed by a line containing one zero.

    输出

    For each case, print the case number (1, 2 …) first. Then, for each “find” operation, output the result. Your output format should imitate the sample output. Print a blank line after each test case.

    示例输入

    4
    add 2 3
    find 1 2
    remove 2 3
    find 1 2
    
    0

    示例输出

    Case 1:
    2 3
    -1

    提示

     

    来源

     2010年山东省第一届ACM大学生程序设计竞赛


    解题思路:

    用set 可以很好的解决这道题,set中的元素输入的时候就自动从小到大排序,这里的元素是pair类型,如果pair中的第一个数相同,则按第二个数排序,这和题目要求就相符了。还用了lower_bound 和upper_bound这个函数。一开始犯了个错误,在find查询的时候也把当前输入的一对数插入到了set容器中,不能这样,因为find操作只是单单查询,如果插入了会对后面的操作产生干扰,错误。

    lower_bound(val): 返回容器中第一个值【大于或等于】val的元素的iterator位置。
    upper_bound(val): 返回容器中第一个值【大于】val的元素的iterator位置。
    代码:

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <set>
    #include <string>
    using namespace std;
    
    int main()
    {
        pair<int,int>p;
        int n;char str[10];
        int c=1;
        while(cin>>n&&n)
        {
            cout<<"Case "<<c++<<":"<<endl;
            set< pair<int,int> >s;
            while(n--)
            {
                scanf("%s",str);
                scanf("%d%d",&p.first,&p.second);
                if(str[0]=='a')
                    s.insert(p);
                else if(str[0]=='r')
                    s.erase(p);
                else if(str[0]=='f')
                {
                    set< pair<int,int> >::iterator it;
                    it=s.lower_bound(p);//找到set中第一个比p大的元素的位置,找不到则为s.end()
                    for(;it!=s.end();it++)
                    {
                        if(it->first>p.first&&it->second>p.second)//都大于才符合题意
                        {
                            cout<<it->first<<" "<<it->second<<endl;
                            break;
                        }
                    }
                    if(it==s.end())//找不到
                        cout<<-1<<endl;
                }
            }
            cout<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    Adding an instance to a MEF container
    IoC Containers with Xamarin
    AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parametersIllegal key size or default parameters
    How to install WP 8.0 SDK if WP 8.1 SDK is installed?
    Windows Phone SDK 8/8.1 官方下载
    安装wp8sdk 当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内。
    Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App
    How to Make Portable Class Libraries Work for You
    Inside Portable Class Libraries
    Route学习笔记之Area的Route注册
  • 原文地址:https://www.cnblogs.com/vivider/p/3697652.html
Copyright © 2011-2022 走看看