1418A. Buying Torches

这次A题,真心fo了(导致wa了我两次)
样例出错两次,数据出错一次。
讲一下我的思路吧。

  • 首先先明确至少需要多少个棍。Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图1 个火炬,至少需要$k ∗ y + k $ 个棍棍。
  • 其次要想,怎么从Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图2个棍,利用第一条贸易,变成 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图3个棍。我们可以先通过观察,假设 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图4

Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图5

可以发现每次加Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图6,也就是Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图7

所以,设Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图8是贸易一的次数。

Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图9%20%E2%88%97%20t%20m%20p%20%E2%88%92%20%3E%20k%20%E2%88%97%20y%20%2B%20k%0A#card=math&code=1%20%2B%20%28%20x%20%E2%88%92%201%20%29%20%E2%88%97%20t%20m%20p%20%E2%88%92%20%3E%20k%20%E2%88%97%20y%20%2B%20k%0A)

这里我为什么要用Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图10而不是$= $呢?

因为可能不能正好等于$k ∗ y + k $。
那就取

Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图11%E2%88%97tmp%3Ek%E2%88%97y%2Bk%0A#card=math&code=1%2B%28x%E2%88%921%29%E2%88%97tmp%3Ek%E2%88%97y%2Bk%0A)

Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图12 的值。

最后 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图13 再加上 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图14 次即可。

  1. #python
  2. for _ in range(int(input())):
  3. x,y,k=map(int,input().split())
  4. tmp=(k*y+k-1)//(x-1)
  5. if tmp*(x-1)<k*y+k-1:
  6. tmp+=1
  7. tmp+=k
  8. print(tmp)
  1. //C++
  2. #include<bits/stdc++.h>
  3. #define ms(a,b) memset(a,b,sizeof a)
  4. using namespace std;
  5. typedef long long ll;
  6. const int N = 1e5 + 100;
  7. ll _, n, m, a[N], i, j;
  8. void solve() {
  9. ll x, y, k;
  10. cin >> x >> y >> k;
  11. ll tmp = (k * y + k - 1) / (x - 1);
  12. if (tmp * (x - 1) < k * y + k - 1)tmp++;
  13. tmp += k;
  14. cout << tmp << endl;
  15. }
  16. int main() {
  17. //freopen("in.txt", "r", stdin);
  18. ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  19. cin >> _; while (_--) solve();
  20. }

再看看一下dalao的数学解法:

由于第二次交易是获取煤炭的唯一方法,因此我们显然需要进行 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图15 次第二次交易。 那么,我们需要进行多少次首次交易? 我们可以看到,为了最终得到足够的棒和煤,我们需要获得 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图16 棒(将 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图17 转换为煤,将 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图18 转换为棒)。 由于第一次交易实际上每次都给我们 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图19 个新的摇杆,因此我们需要进行 Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图20个第一次交易(对于不熟悉的人请参考下限和上限功能)。
有关实现细节,请注意,对于正整数Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图21Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图22Educational Codeforces Round 95 (Div. 2) A - B题题解(A题数据连错3次,搞人心态中) - 图23

  1. //C++实现
  2. #include <iostream>
  3. using namespace std;
  4. int main(){
  5. int t;
  6. cin>>t;
  7. while(t--){
  8. long long x,y,k;
  9. cin>>x>>y>>k;
  10. cout<<((y+1)*k+x-3)/(x-1)+k<<endl;
  11. }
  12. return 0;
  13. }

1418B. Negative Prefixes

模拟题,记录可以动的,从大到小排序。

  1. #python
  2. for _ in range(int(input())):
  3. n=int(input())
  4. lst=list(map(int,input().split()))
  5. s=list(map(int,input().split()))
  6. index=[]
  7. t=[]
  8. for i in range(n):
  9. if s[i]==0:
  10. index.append(i)
  11. t.append(lst[i])
  12. if len(t)==0:
  13. print(*lst)
  14. continue
  15. t.sort(reverse=True)
  16. for i in range(len(index)):
  17. lst[index[i]]=t[i]
  18. print(*lst)
  1. //c++
  2. #include <bits/stdc++.h>
  3. #define ll long long int
  4. using namespace std;
  5. int main()
  6. {
  7. int t=1;
  8. cin>>t;
  9. while(t--){
  10. int n,i,j=0;
  11. cin>>n;
  12. int a[n],b[n];
  13. vector<int>v;
  14. for(i=0;i<n;i++)cin>>a[i];
  15. for(i=0;i<n;i++)cin>>b[i];
  16. for(i=0;i<n;i++){if(b[i]==0){v.push_back(a[i]);}}
  17. sort(v.rbegin(),v.rend());
  18. for(i=0;i<n;i++){if(b[i]==0){a[i]=v[j];j++;}}
  19. for(i=0;i<n;i++)cout<<a[i]<<" ";
  20. cout<<endl;
  21. }
  22. }