Hihocoder Programming Practice Game 45th __ Programming

Source: Internet
Author: User

Altogether four questions 1: Complementary two Yuan group

Time limit: 10000ms
Single Point time: 1000ms
Memory Limit: 256MB
Describe
given n integers two (X1, Y1), (X2, Y2), ... (XN, YN).

Please calculate how many of them to the two-tuple (xi, Yi) and (Xj, Yj) meet Xi + Xj = yi + Yj and I < J.

Input
The first row contains an integer n.

The following n rows are two integers xi and Yi per line.

For 70% of the data, 1≤n≤1000

For 100% of the data, 1≤n≤100000-1000000≤xi, yi≤1000000

Output
An integer that represents the answer.

Sample input
5
9 10
1 3
5 5
5 4
8 6
Sample output
2

When I first saw this problem, I did not seriously examine the topic and thought (1,2) (3,2) (3,2) that this situation was counted only once (should be recorded two times), find a way to remove the matching array, the result of a long delay, and then the first to do the next few questions, the end of the return to go directly to the literal meaning, after 70% (LTE). After the game turned over to a great God concise code, worship. This problem will be given the condition Xi + Xj = yi + Yj A little change to be (Xi-yi) + (Xj-yj) = 0, so the number of input to the difference is stored. Given the range of x and Y values, map can be used to find directly. Well, all the above can be thought of. The method of the great God in the storage of X-y, directly to the Y-x do add operations, eliminating the repeated traversal and lookup, very clever. (Note the value range of X,y, LL)

Code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
typedef long LL;

using namespace std;
Map<ll, ll> Res;

int main ()
{
    int N; cin >> N;

    ll cnt = 0;
    int  x, y;

    while (n--) {
        cin >> x >> y;
        CNT + + res[x-y];
        res[y-x]++;
    }


    cout << cnt << Endl;
    return 0;
}
Topic 2: Looking for tangents

Time limit: 10000ms
Single Point time: 1000ms
Memory Limit: 256MB
Describe
On a given plane n dots p1= (X1, Y1), p2= (X2, Y2), ... Pn= (XN, YN).

Please find two different points for Pi and PJ gratification: All other points are on the same side of Pi and PJ lines (can be on the line).

If there are multiple sets of answers that meet the criteria, you can output any set.

Input
The first row contains an integer n.

The following n rows contain two integers, Xi and Yi, per line.

For 50% of the data, 1≤n≤1000

For 100% of the data, 1≤n≤100000 0≤xi, yi≤1000000

Output
The output consists of two integers I and j separated by a space, paying attention to 1≤i, J≤n and I≠j.

Sample input
6
0 10
7 0
8 8
10 18
15 13
20 4
Sample output
5 6

Method: A line is determined by two points: first find the x/y coordinate maximum (the minimum can also be) as the first point, then calculate the remaining points and the slope of the point, take the absolute maximum point as the second point.

#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <sstream>

using namespace std;

int main ()
{
    int n; Cin >> N;

    int x[n], y[n];
    int m = 0, q = 0;
    for (int i=0; i<n; i++) {
        cin >> X[i] >> y[i];
        if (x[i]>x[m]) m = i;
    }
    float k = 0;
    for (int i=0; i<n; i++)
    {
        if (i==m) continue;
        if (X[i]==x[m]) {q = i; break;}
        float K1 = ABS (1.0* (Y[I]-Y[M))/(X[i]-x[m));
        if (K1 > k) {k = k1, q = i;}

    }

    if (M < q)   cout << m+1 << ' << q+1 << Endl;
    else cout << q+1 << ' << m+1 << Endl;

    return 0;
}
Topic 3:LR Questions

Time limit: 10000ms
Single Point time: 1000ms
Memory Limit: 256MB
Describe
Given two strings s and T, each string consists of ' L ', ' R ' and ' _ '. For example:

r__lr_r_l

In each operation, an ' r ' in S is exchanged with its right side (provided that this R is adjacent to the right); Alternatively, an ' l ' in S will be exchanged with its left neighbor ' (assuming that the left side of this L is ').

For example, moving the first L and moving the second R will get:

r__lr_r_l-> r_l_r_r_l-> r_l__rr_l

Please calculate the minimum number of times you can turn s to t.

Input
The first line contains a string s.

The second line contains a string T.

For 30% of the data, 1≤| s| = | T| ≤20

For 100% of the data, 1≤| s| = | T| ≤100000

Output
Outputting an integer represents the minimum number of operations.

If, however, s cannot be changed to T, Output-1.

Sample input
r__lr_r_l
r_l__rr_l
Sample output
2

Always complicate the simple problem, even want to use DP, do the problem amount is too little. Refer to the simple and graceful code of a great God as follows:

#include <iostream>
#include <vector>
using namespace std;
int main () {
    string s, t; cin>>s>>t;
    Vector<int> SS, TT;
    for (int i=0;i<int (s.size ()); i++) if (S[i]!= ' _ ') ss.push_back (i);
    for (int i=0;i<int (t.size ()); i++) if (T[i]!= ' _ ') tt.push_back (i);
    if (Ss.size ()!= tt.size ()) return Cout<<-1<<endl, 0;
    int ans = 0;
    for (int i=0;i< (ss.size ()); i++) {
        int sx = Ss[i], TX = Tt[i];
        if (S[SX]!= t[tx]) return Cout<<-1<<endl, 0;
        if (s[sx] = = ' L ' && SX < TX ' return Cout<<-1<<endl, 0;
        if (s[sx] = = ' R ' && SX > TX) return cout<<-1<<endl, 0;
        Ans + abs (SX-TX);
    }
    cout<<ans<<endl;
    return 0;
}

Topic 4: Inference size relationship
Time limit: 20000ms
Single Point time: 2000ms
Memory Limit: 256MB
Describe
There are n integers A1, A2, ... An, now we know the information of M bar about these n integers. Each piece of information is:

Ai < AJ or ai = AJ

Little hi i hope you can process this information sequentially from the first message. Once you can infer the size relationship between A1 and an, stop immediately.

The output first infers the relationship between A1 and an when dealing with the number of articles. If all m messages are processed or not, the size relationship between A1 and an is not known, output-1.

To ensure that M-piece information is not contradictory.

Input
The first row contains two integers n and M.

The following m lines contain one message per line AI < AJ or ai = aj.

For 30% of the data, 1≤n≤1000, 1≤m≤10000

For 100% of the data, 1≤n≤100000, 1≤n≤1000000

Output
An integer that represents the answer.

Sample input
5 8
A1 < A3
A3 < A2
A3 < A4
A5 < A2
A1 < A4
A1 < A2
A5 < A1
A5 < A3
Sample output
7

A reference to the answer of a great God, well, read it. Knowledge Points: Two-point lookup, DFS

#include <iostream> #include <cstdio> #include <vector> #include <cstring> using namespace std;
const int MAXN = 1e6 + 10;
int X[MAXN], OP[MAXN], Y[MAXN];

int n, m;
int FA[MAXN]; int find (int x) {return fa[x] = = x X:fa[x] = find (Fa[x]);} void Union (int x, int y) {x = find (x), y = Find (
    y);
Fa[x] = y;
int VIS[MAXN];
Vector<int> G[MAXN];
        void Dfs (int. x) {for (int i = 0; i < g[x].size (); i++) {int to = G[x][i];
        if (Vis[to]) continue;
    Vis[to] = 1, DFS (to);
    } bool Check (int mid) {for (int i = 1; I <= n; i++) Fa[i] = i, g[i].clear ();
    for (int i = 1; I <= mid; i++) if (op[i] = = 0) Union (x[i), y[i]);
        for (int i = 1; I <= mid; i++) {if (op[i) = = 1) g[find (X[i])].push_back (Find (y[i));
    if (op[i] = = 1) g[find (Y[i])].push_back (Find (x[i));
    for (int i = 1; I <= n; i++) vis[i] = 0;
    Vis[find (1)] = 1;
    DFS (find (1)); if (Vis[find (n)]) returnTrue
    for (int i = 1; I <= n; i++) vis[i] = 0;
    Vis[find (n)] = 1;
    DFS (Find (n));
    if (Vis[find (1)]) return true;
return false;
    int ID (char * s) {int L = strlen (s);
    int ret = 0;
    for (int i = 0; i < L; i++) ret = ret * + (S[i]-' 0 ');
return ret;
    int main (void) {scanf ("%d%d", &n, &m);
        for (int i = 1; I <= m i++) {char a[11], b[11], c[11];
        scanf ("%s%s%s", A, B, c);
        cout << a << ' << b << ' << c << Endl;
        X[I] = ID (A + 1), y[i] = ID (c + 1);
        cout << x[i] << ' << y[i] << Endl;
        if (b[0] = = ' > ') op[i] = 1;
    if (b[0] = = ' < ') op[i] =-1;
    } if (!check (m)) puts ("-1");
        else {int L = 1, r = m;
            while (L < r) {int mid = L + (r-l)/2;
            if (check (mid)) R = Mid;
        else L = mid +1;
    printf ("%d\n", R); } return 0; }

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.