题目
Dota2 Pro Circuit
*Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 440 Accepted Submission(s): 224
*
Problem Description
TI10 and ICPC World Finals 2020, which will be held earlier? Take a bet!
The International(TI) is the biggest and most prestigious event of Dota2 and is commonly held annually in August. The Dota2 teams should try to earn points to be eligible to compete in TI. There are two ways of earning points, first is by competing in the regional contests, second is by competing in the tournaments, where all teams are gathered to compete together and earn points according to their rank in the tournament. A team’s final score is the sum of scores from both the regional contests and tournaments.
Now that the regional contests have finished, there are n teams taking part, and the ith team has earned ai points from the regional contests. Also, the team that gets the ith rank in the tournament can gain bi points.
cyz is a huge fan of Dota2. So before the tournament starts, he will predict the final rank of all teams. cyz wants to know, for each team, what’s its best possible rank and its worst possible rank after the tournament finishes.
If a team has a final score equal to x, its rank is defined as one plus the number of teams with a strict higher score than it. For example, if the final score of four teams are 700,500,500,300 respectively, then their final ranks are 1,2,2,4, respectively.
Input
The first line contains a number T(1≤T≤20), denoting the number of test cases.
The first line of each test case contains one number n(1≤n≤5000), denoting the number of different teams that participate in the regional contests and tournaments.
The next line contains n integers a1,a2,…,an(0≤ai≤109), denoting the points of each team before the tournament starts.
Then follows one line containing n integers b1,b2,…,bn(0≤bn≤bn−1≤…≤b1≤109), where bi denotes the number of points a team would get if ranking ith in the tournament.
It is guaranteed that there are at most 8 cases with n>100.
Output
For each test case, output n lines, where the ith line contains two integers besti,worsti(1≤besti≤worsti≤n), denoting the best possible and worst possible rank a team would get after the tournament finishes.
Sample Input
2
3
5 10 8
5 2 1
2
5 6
4 4
Sample Output
2 3
1 2
1 3
2 2
1 1
Source
2021“MINIEYE杯”中国大学生算法设计超级联赛(9)
解释与代码
贪心+双指针
可以结合注释看
先把数据存入结构体然后排序
最好排名:给当前队伍w加上最高分pri[1],然后从左到右枚举,在不超过an的情况下,给尽可能的高分,pri数组设一个指针q
最坏排名:给当前队伍w加上最低分pri[n],和前面类似,pri数组还是放一个q做记录
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\n'
#define mll map<ll,ll>
#define msl map<string,ll>
#define mls map<ll, string>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x) for(auto& a : x)
#define pll pair<ll,ll>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define ini2(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define case ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define sc scanf
#define _ 0
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " << (x) << '\n')
#define TIE \
cin.tie(0);cout.tie(0);\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
return;
}
inline void write(long long x) {
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
putchar('\n');
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e4+10;
const ll N = 5;
int n, num;
struct Team {
int dat = 0, id;
}arr[maxn];
bool cmp(Team a,Team b) {
return a.dat<b.dat;
}
int pri[maxn], ans[maxn][2];
int maxi(int w) {
int an = arr[w].dat+pri[1], cnt = 0, q = 2, p = 1;
//an是当前加上最高分pri[1]
while(p<=n && q<=n){//在pri数组放一个指针q,p就是arr的下标
if(p==w) {//如果p==w,那就跳过,因为w用过了,pri的指针继续走
p++;
continue;
}
if(arr[p].dat+pri[q] <= an) {//如果还是小于那就arr下标一直走
p++;
cnt++;//用来记录符合的个数,最后return n-cnt
}
q++;//无论什么情况,pri下标一直走
}
return n-cnt;
}
int mini(int w) {
int q = 1, an=arr[w].dat+pri[n], ans = 1;
//an是当前加上最低分pri[n]
for (int i=1; i<=n; i++) {
if (i == w) continue;//同maxi
if (arr[i].dat > an) {//不用加分数就大了,加了肯定超过
ans++;
continue;
} else {//小于等于的情况
if (arr[i].dat+pri[q] > an) {
ans++;
q++;
}
}
}
return ans;
}
void solve(){
read(n);
for (int i=1; i<=n; i++) read(arr[i].dat), arr[i].id = i;
for (int i=1; i<=n; i++) read(pri[i]);//默认从大到小
sort(arr+1, arr+1+n, cmp);
for (int i=1; i<=n; i++) {
num = arr[i].id;
ans[num][0] = maxi(i);
ans[num][1] = mini(i);
}
for (int i=1; i<=n; i++)
cout<<ans[i][0]<<" "<<ans[i][1]<<endl;
}
int main()
{
// TIE;
#ifndef ONLINE_JUDGE
// freopen ("in.txt" , "r", stdin );
// freopen ("out.txt", "w", stdout);
#else
#endif
// solve();
case{solve();}
// case{cout<<"Case "<<Q<<":"<<endl;solve();}
return ~~(0^_^0);
}
参考:https://blog.csdn.net/weixin_51937688/article/details/119773816?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162960101216780255247477%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162960101216780255247477&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v29_ecpm-6-119773816.first_rank_v2_pc_rank_v29&utm_term=%E6%9D%AD%E7%94%B5%E5%A4%9A%E6%A0%A1+Dota2+Pro+Circuit&spm=1018.2226.3001.4187