题目来源:码蹄集
Python代码实现:
from collections import defaultdict
n, t = map(int, input().split())
t *= 2
x = [0] * (n+1)
v = [0] * (n+1)
cnt = defaultdict(int)
for i in range(1, n+1):
x[i], v[i] = map(int, input().split())
x[i] *= 2
cnt[x[i]] += 1
if cnt[x[i]] > 1:
exit(1)
while t > 0:
for i in range(1, n+1):
cnt[x[i]] -= 1
x[i] += v[i]
cnt[x[i]] += 1
for i in range(1, n+1):
if cnt[x[i]] > 1:
v[i] *= -1
t -= 1
for i in range(1, n+1):
print(x[i] // 2, v[i] * (cnt[x[i]] == 1))
C++代码实现:
原文链接:https://blog.51cto.com/u_15745546/5950626
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int i,j,k,n,m,t,x[1005],v[1005];
unordered_map<int,int> cnt;
int main() {
ios::sync_with_stdio(0);
cin>>n>>t;t*=2;
for(i=1;i<=n;i++){
cin>>x[i]>>v[i];x[i]*=2;
cnt[x[i]]++;
if(cnt[x[i]]>1)return 1;
}
while(t--){
for(i=1;i<=n;i++){
cnt[x[i]]--;
x[i]+=v[i];
cnt[x[i]]++;
}
for(i=1;i<=n;i++){
if(cnt[x[i]]>1)v[i]*=-1;
}
}
for(i=1;i<=n;i++){
cout<<x[i]/2<<' '<<v[i]*(cnt[x[i]]==1)<<'\n';
}
}