HDU 3303 harmony forever (Mathematics)

Source: Internet
Author: User

Question link:

HDU: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3303

Problem descriptionwe believe that every inhabitant of this universe eventually will find a way to live together in harmony and peace; that trust, patience, kindness and will exist between every living being of this earth; people will find a way to appreciate and cooperate with each other instead of continuous bickering, arguing and fighting. harmony -- the stage of society so far people dream of and yet it seems so far away from now...

Fortunately, the method of unlocking the key to true harmony is just discovered by a group of philosophers. it is recorded on a strange meteorite which has just hit the Earth. you need to decipher the true meaning behind those seemingly random symbols... more precisely, you are to write a program which will support the following two kinds of operation on an initially empty set S:

B x: add number X to set S. The Kth command in the form of B x always happens at time K, and number X does not belong to set S before this operation.
A y: Of all the numbers in set S currently, find the one which has the minimum Remainder when divided by Y. in case a tie occurs, you shoshould choose the one which appeared latest in the input. report the time when this element is inserted.

It is said that if the answer can be given in the minimum possible time, true harmony can be achieved by human races. you task is to write a program to help us. inputthere are multiple test cases in the input file. each test case starts with one integer t where 1 <= T <= 40000. the following T lines each describe an operation, either in the form of ''B X" or ''a y "where 1 <= X, Y <= 500000.

T = 0 indicates the end of input file and shocould not be processed by your program. outputprint the result of each test case in the format as indicated in the sample output. for every line in the form of 'a y ", you shoshould output one number, the requested number, on a new line; output-1 if no such number can be found. separate the results of two successive inputs with one single blank line. sample Input
5 B 1 A 5 B 10 A 5 A 40 2 B 1 A 20
Sample output
Case 1: 1 2 1 Case 2: 1
Source2006 Asia Regional Shanghai


There are two operations for a set:

1. Add an element to the set;

2. One is to ask the smallest number of mod y in the current set. If there are multiple, the final element added to the output requires the time when they joined the set.

The Code is as follows:

(However, this code will talk to TLE on poj3145, probably because the data on HDU is weak, so it's time for me to try the line tree! The correct solution should be line segment tree)

//#pragma warning (disable:4786)#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <climits>#include <ctype.h>#include <queue>#include <stack>#include <vector>#include <utility>#include <deque>#include <set>#include <map>#include <iostream>#include <algorithm>using namespace std;const double eps = 1e-9;//const double pi = atan(1.0)*4;const double pi = 3.1415926535897932384626;#define INF 1e18//typedef long long LL;//typedef __int64 LL;int a[5000017], b[5000017];int main(){    char cc[7];    int t, tt;    int cas = 0;    int cont = 0;    while(scanf("%d",&t) && t)    {        int l = 0;        if(cont)            printf("\n");        cont++;        printf("Case %d:\n",++cas);        while(t--)        {            scanf("%s%d",cc,&tt);            if(cc[0] == 'B')            {                a[l++] = tt;            }            else if(cc[0] == 'A')            {                int flag = 1;                int minn = tt, f = 0;                for(int i = 0; i < l; i++)                {                    if(a[i]%tt <= minn)                    {                        flag = 0;                        minn = a[i]%tt;                        f = i+1;                    }                }                if(flag)                {                    printf("-1\n");                }                else                    printf("%d\n",f);            }        }        //   printf("\n");    }    return 0;}

HDU 3303 harmony forever (Mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.