Codeforces Gym 2013-2014 Samara SAU ACM ICPC quarterfinal qualification algorithm design

Source: Internet
Author: User
Tags acos stdin cmath

Topic Address: http://codeforces.com/gym/100247


2014.10.27 Edition

26th training problem, today found a problem has a more efficient solution, with the prefix maximum and suffix maximum value


A. The power of the Dark Side time limit/test 2 seconds memory limit per test 256 megabytes input standard input OUTPU T standard output

On the Jedi tournament n Jedi are battling all other. Every Jedi has three parameters:strength, agility and intelligence. All parameters of the all Jedi are different. When two Jedi have a fight, the winner'll is the Jedi which has more parameters higher compared to the same parameters O F his opponent. For example, Jedi with parameters (5, 9,) defeats Jedi with parameters (2, 4) because of a and third .

Sith have a plan to turn one the Jedi to the dark side of the Force. It would give him a powerful skill that allows to swap some his parameters, maybe more than one time, before every. Sith wish their new apprentice to defeat all of the remaining Jedi.

Which Jedi can be used from Sith to leads their plan to success? Input

The contains the only integer n (1≤n≤200000)-the number of the Jedi.

Then n lines follow. Each of them contains three integers separated by Space:ai, bi and CI (1≤ai, Bi, ci≤109)-the parameters of the Jedi .

All AI, bi and CI are pairwise distinct. Output

In the I output one integer m-the number of the suitable Jedi.

In the second line output m integers-the numbers of this jedi-in the ascending order. Sample Test (s) input

4
5 9
2 4 8
7 3 6 11
1
Output
2
1 4
Input
4
4 1 5 8 9 2 6 7 3-
10
Output
3
2 3 4


First the optimal algorithm O (n) to solve the

Adjust every time you read it to ensure AI<BI<CI

Record the maximum number of people in front of this person AI, the maximum of BI, the maximum number of CI, and I'll make it the prefix max value.

Also, record suffix maximum value

If the person meets the requirements and only if

His maximum CI is greater than the maximum of the bi prefix from head to toe and is greater than the bi suffix maximum of one person from the end to the last

His secondary large value of BI is greater than the maximum AI prefix from head to top and greater than the AI suffix of one person from tail to last

The code is as follows:

Hello. I ' m Peter #include <cstdio> #include <iostream> #include <sstream> #include <cstring> #include <string> #include <cmath> #include <cstdlib> #include <algorithm> #include <functional> # include<cctype> #include <ctime> #include <stack> #include <queue> #include <vector> #
Include<set> #include <map> using namespace std;
typedef long Long LL;
typedef long double LD; #define Peter cout<< "I am Peter" <<endl #define INPUT freopen ("Data.txt", "R", stdin) #define Randin Srand ( unsigned int) time (NULL) #define INT (0x3f3f3f3f) *2 #define LL (0x3f3f3f3f3f3f3f3f) *2 #define GSIZE (a) (int) a.size () #def 
Ine Len (a) (int) strlen (a) #define Slen (s) (int) s.length () #define PB (a) push_back (a) #define CLR (a) memset (A,0,sizeof (a)) #define CLR_MINUS1 (a) memset (A,-1,sizeof (a)) #define CLR_INT (a) memset (A,int,sizeof (a)) #define CLR_TRUE (a) memset (A, True,sizeof (a)) #define CLR_FALSE (a) memset (A,false,sizeof (a)) #defINE Clr_queue (q) while (!q.empty ()) Q.pop () #define CLR_STACK (s) while (!s.empty ()) S.pop () #define REP (I, A, b) for (int i = A; I < b; i++) #define DEP (I, A, b) for (int i = A; i > b; i--) #define Repin (I, A, b) for (int i = A; I <= b; i++) #define De Pin (i, A, b) for (int i = A; I >= b; i--) #define PI ACOs ( -1.0) #define EPS 1e-6 #define MOD 1000000007 #define MAXN #d
Efine N 200100 #define M int n,a,b,c; struct Digit {int a,b,c;}
D[n];
int prefixmax[n][3];
int suffixmax[n][3];
int order[10],num_ans,ans[n];
    int main () {cin>>n;
        Repin (i,1,n) {scanf ("%d%d%d", &a,&b,&c);
        Order[1]=a,order[2]=b,order[3]=c;
        Sort (order+1,order+1+3);
        D[I].A=ORDER[1];
        D[I].B=ORDER[2];
    D[I].C=ORDER[3];
        } repin (I,1,n) {Prefixmax[i][0]=max (PREFIXMAX[I-1][0],D[I].A);
        Prefixmax[i][1]=max (PREFIXMAX[I-1][1],D[I].B);
    Prefixmax[i][2]=max (PREFIXMAX[I-1][2],D[I].C); } depin (i,n,1) {SuffIxmax[i][0]=max (SUFFIXMAX[I+1][0],D[I].A);
        Suffixmax[i][1]=max (SUFFIXMAX[I+1][1],D[I].B);
    Suffixmax[i][2]=max (SUFFIXMAX[I+1][2],D[I].C);
    } num_ans=0; Repin (i,1,n) {if (d[i].c>prefixmax[i-1][1] && d[i].b>prefixmax[i-1][0]) {if (D[i].c>suf
            FIXMAX[I+1][1] && d[i].b>suffixmax[i+1][0]) {ans[++num_ans]=i;
    }}} cout<<num_ans<<endl;
    BOOL First=true;
        Repin (I,1,num_ans) {if (a) first=false;
        else printf ("");
    printf ("%d", ans[i]);
printf ("\ n");

 }

The complexity of this method is O (Nlogn), although the complexity is high, it does not have a high efficiency, and space consumption is also big

However, this method uses a three-dimensional approach to become one-dimensional, which helps to improve algorithmic thinking.

Also, thank the Devil King for offering this idea to me 0.0


The three-dimensional into a one-dimensional, up to 6*10^5 so many numbers, and then from small to large sort, the title to ensure that each number 22 different

Of course, the array of structures is sorted, the structure stores the number, and the number corresponds to which person

Also, store two variables

One is, from 1 to this, the number of occurrences >=1 how many people

One is, from 1 to this, the number of occurrences >=2 how many people

Because a person has 3 variables, all occurrences are up to 3, and there must be 3.

When the number of occurrences is 3 o'clock, find the person in the record before the occurrence of 2 o'clock

If the number of >=2 before this position 3 is n and the number of occurrences before 2 that position is also n, the person is the person who is qualified for the >=1.

Sweep it, call 2 maps, and you'll know who the person is.


The code is as follows:

#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <
Algorithm> #include <map> using namespace std;
typedef long Long LL; #define LEN (a) (int) strlen (a) #define Slen (s) (int) s.length () #define Repin (I, A, b) for (int i = A; I <= b; i++) #defi
    NE maxn 600100 #define N 200100 struct Digit {int id,x;
int num1,num2;
}D[MAXN];
BOOL Comp (const Digit A,const Digit b) {return a.x<b.x;} int ans[n],num_ans;
int num_d;
int num1,num2,n,a,b,c;
map<int,int>apptime;
map<int,int>sp;
    int main () {cin>>n;
    int num_d=0,t;
        Repin (i,1,n) {scanf ("%d%d%d", &a,&b,&c);
        T=++num_d;
        D[t].id=i;
        D[t].x=a;
        T=++num_d;
        D[t].id=i;
        D[t].x=b;
        T=++num_d;
        D[t].id=i;
    D[t].x=c;
    Sort (d+1,d+1+num_d,comp);
    num1=num2=0;
    Apptime.clear (), sp.clear ();
    num_ans=0;
 Repin (i,1,num_d) {t=apptime[d[i].id];       T+=1;
            if (t==3) {int w=sp[d[i].id];
                if (d[w].num1==n && num2==n) {int t=++num_ans;
            Ans[t]=d[i].id;
            } else{if (t==1) num1+=1;
                else if (t==2) {num2+=1;
            Sp[d[i].id]=i;
            } apptime[d[i].id]=t;
            D[I].NUM1=NUM1;
        d[i].num2=num2;
    } if (num_ans!=0) sort (Ans+1,ans+1+num_ans);
    printf ("%d\n", Num_ans);
    BOOL First=true;
        Repin (I,1,num_ans) {if (a) first=false;
        else printf ("");
    printf ("%d", ans[i]);
printf ("\ n"); }


B. Similar Strings time limit/test 3 seconds memory limit per test 256 megabytes input standard input output standard Output

Let's call two strings similar if there exists a bijective mapping over characters, which, when applied to the characters Of the The "the", makes it equal to the second string. For example,«abacaba»and«tetatet»are similar strings, but«test»and«bear»-are not. Given the set of strings, find the number of pairs of similar strings. Input

The contains one integer n-the number of strings.

Next n lines contain non-empty strings of lowercase latin. The sum of lengths of strings does not exceed 106. Output

Output the only integer-the number of pairs of similar strings. Sample Test (s) input

4
Abacaba
tetatet
test
Bear
Output
1
Input
4
Jury
code
would
pass
Output
2
Input
4
your
code
wont
Pass
Output
3


This problem, with a map<vector<int>,int>m; Recording

Vector stores the number of occurrences of each letter of this string, expressed in numbers

Like Abcabcdea.

A is the first occurrence, the 0,b is 1,c for the 2,d is 3 E is 4

The corresponding vector should be 0 1 2 0 1 2 3 4 0

Since the letter is ' a '-' z ', the number in the vector does not exceed 25.

The vector is a hash, and correctly all the strings of the same type are mapped onto a vector, and the second value of the map indicates how many times the vector appears

This way each string looks up in the map, maintains ans, and then updates the map, the complexity of O (Nlogn) method, you can get the answer

The code is as follows:

Hello.
I ' m Peter. #include <cstdio> #include <iostream> #include <sstream> #include <cstring> #include <string > #include <cmath> #include <cstdlib> #include <algorithm> #include <functional> #include <cctype> #include <ctime> #include <stack> #include <queue> #include <vector> #include
<set> #include <map> using namespace std;
typedef long Long LL;
typedef long double LD; #define Peter cout<< "I am Peter" <<endl #define INPUT freopen ("Data.txt", "R", stdin) #define Randin Srand ( unsigned int) time (NULL) #define INT (0x3f3f3f3f) *2 #define LL (0x3f3f3f3f3f3f3f3f) *2 #define GSIZE (a) (int) a.size () #def 
Ine Len (a) (int) strlen (a) #define Slen (s) (int) s.length () #define PB (a) push_back (a) #define CLR (a) memset (A,0,sizeof (a)) #define CLR_MINUS1 (a) memset (A,-1,sizeof (a)) #define CLR_INT (a) memset (A,int,sizeof (a)) #define CLR_TRUE (a) memset (A, True,sizeof (a)) #define CLR_FALSE (a) memset (A,false,sizeof (a)) #deFine Clr_queue (q) while (!q.empty ()) Q.pop () #define CLR_STACK (s) while (!s.empty ()) S.pop () #define REP (I, A, b) for (int i = A; I < b; i++) #define DEP (I, A, b) for (int i = A; i > b; i--) #define Repin (I, A, b) for (int i = A; I <= b; i++) #define De Pin (i, A, b) for (int i = A; I >= b; i--) #define PI ACOs ( -1.0) #define EPS 1e-6 #define MOD 1000000007 #define MAXN 10
01000 #define N #define M int idx (char c) {return C ' a ';} char S[MAXN];
int n,num,apptime[30];
map<vector<int>,ll>m;
ll ans;
    int main () {//input;
    cin>>n;
    ans=0;
        Repin (i,1,n) {scanf ("%s", s);
        int Len=len (s);
        Repin (i,0,25) {apptime[i]=0;
        } num=0;
        vector<int>v;
        V.clear ();
            Rep (j,0,len) {int c=idx (s[j]);
            int t;
            if (apptime[c]==0) {t=apptime[c]=++num;
            else T=apptime[c];
        V.PB (t);
   } Ans+=m[v];     M[v]+=1;
} cout<<ans<<endl;
 }


C. Victor's seconds time limit/test 2 memory limit per test 256 megabytes input standard input output Standar D Output

Unacknowledged scientist Victor conducts a pseudoscientific of the relation between integers that cross his mind And the integer that is comes into his assistant ' s mind. He wrote the integers A1, ..., a which had crossed his mind. Then It turned up this integer s had come into his assistant ' s mind. Victor wants to determine how many consecutive non-empty sets of integers al, Al + 1, ..., AR (l≤r) have the sum Al + al + 1 + ... + ar = S. Input

The contains two integers separated by space:n and S (1≤n≤200000,-2 1014≤s≤2 1014)-the number of Integers which crossed Victor's mind and the integer that came to his assistant ' s mind.

The second line contains n integers separated by space: ai  (-ten

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.