题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3981
题目分析:
所有的队伍都在周长为m的圆桌上,由机器人送气球,从起点s开始绕着圆桌转,如果在某个座位上的a队伍A题的时间为b,气球送到的时间为t,则不开心值加t-b;
队伍a在时间b过题时的不开心值为x = (a-s-b%m)%m,这也是他们等待气球的时间;
首先将机器人的起点s设为1;
将每个过题点等待的时间存下之后由小到大排序;
遍历p次依次使每个过题点的等待时间为0,即使每次都使s后移x,那么排在x及其之后的等待时间减少x,排在其之前的增加m-x;
找出最小的不开心总值。
代码:
#include<bits/stdc++.h> using namespace std; const int MAX = 100010; long long p[MAX]; long long d[MAX]; int main() { int t; long long n, m, c, a, b; scanf("%d", &t); while(t--) { scanf("%lld %lld %lld", &n, &m, &c); long long ans = 0, temp = 0; for(int i = 0; i < n; i++) { scanf("%lld", &p[i]); } for(int i = 0; i < c; i++) { scanf("%lld %lld", &a, &b); d[i] = (p[a-1] - 1 - b%m + m)%m; ans += d[i]; } sort(d, d+c); temp = ans; for(int i = 0; i < c; i++) { // cout<<d[i]<<endl; // cout<<temp - c*d[i] + i*m<<endl; ans = min(ans, temp - c*d[i] + i*m); } cout<<ans<<endl; } return 0; }