670C - Cinema - CodeForces Solution


implementation sortings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 2e5 + 5;
int n, m;
int a[maxn], b[maxn], c[maxn];
int sum[maxn * 3];
int cnt, mm;
int arr[maxn * 3], num[maxn * 3];
void discrete() {//离散化
    sort(arr + 1, arr + cnt + 1);
    for (int i = 1; i <= cnt; i++) {
        if (i == 1 || arr[i] != arr[i - 1])
            num[++mm] = arr[i];
    }
}
int query(int x) {//二分查找x的位置
    return lower_bound(num + 1, num + mm + 1, x) - num;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {//将所有电影和人涉及的语言放进一个数组,排序并离散化
        scanf("%d", &a[i]);
        arr[++cnt] = a[i];
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &b[i]);
        arr[++cnt] = b[i];
    }
    for (int i = 1; i <= m; i++) {
        scanf("%d", &c[i]);
        arr[++cnt] = c[i];
    }
    discrete();//离散化
    for (int i = 1; i <= n; i++) {
        int id = query(a[i]);//统计每种语言的人的数量
        ++sum[id];
    }
    int bmax = -1, cmax = -1, ans = 0;
    for (int i = 1; i <= m; i++) {//选择满足题目要求的电影
        int x = query(b[i]);
        int y = query(c[i]);
        if (sum[x] > bmax) {//优先考虑让很高兴的人最多
            bmax = sum[x], cmax = sum[y];
            ans = i;
        }
        else {
            if (sum[x] == bmax && sum[y] > cmax) {//如果答案不唯一、则在此前提下再让比较高兴的人最多
                bmax = sum[x], cmax = sum[y];
                ans = i;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}
  	  				 				 	      			   	 	


Comments

Submit
0 Comments
More Questions

13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum