当前位置:   article > 正文

SDNU_ACM_ICPC_2019_Winter_Practice_4th 题解_the first line contains the only integer tt (1≤t≤1

the first line contains the only integer tt (1≤t≤100001≤t≤10000), the nu

A - Frog Jumping

A frog is currently at the point 00 on a coordinate axis OxOx. It jumps by the following algorithm: the first jump is aa units to the right, the second jump is bbunits to the left, the third jump is aa units to the right, the fourth jump is bbunits to the left, and so on.

Formally:

  • if the frog has jumped an even number of times (before the current jump), it jumps from its current position xx to position x+ax+a;
  • otherwise it jumps from its current position xx to position x−bx−b.

Your task is to calculate the position of the frog after kk jumps.

But... One more thing. You are watching tt different frogs so you have to answer ttindependent queries.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of queries.

Each of the next tt lines contain queries (one query per line).

The query is described as three space-separated integers a,b,ka,b,k (1≤a,b,k≤1091≤a,b,k≤109) — the lengths of two types of jumps and the number of jumps, respectively.

Output

Print tt integers. The ii-th integer should be the answer for the ii-th query.

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 110
  4. #define up(i,x,y) for(int i=x;i<=y;i++)
  5. #define down(i,x,y) for(int i=x;i>=y;i--)
  6. typedef long long ll;
  7. int main()
  8. {
  9. int t;cin>>t;
  10. while(t--)
  11. {
  12. int a,b,k;scanf("%d %d %d",&a,&b,&k);
  13. ll ans=0;
  14. if(k%2==0)cout<<1LL*(a-b)*k/2<<endl;
  15. else cout<<1LL*(a-b)*(k/2)+a<<endl;
  16. }
  17. }

题解:最初在坐标的0位置,先向右a个单位,再向左b个单位,共持续k个回合。

数学题目,分奇偶讨论一下就可以了,注意不要爆int.

B - Disturbed People

There is a house with nn flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of nninteger numbers a1,a2,…,ana1,a2,…,an, where ai=1ai=1 if in the ii-th flat the light is on and ai=0ai=0 otherwise.

Vova thinks that people in the ii-th flats are disturbed and cannot sleep if and only if 1<i<n1<i<n and ai−1=ai+1=1ai−1=ai+1=1 and ai=0ai=0.

Vova is concerned by the following question: what is the minimum number kk such that if people from exactly kk pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number kk.

Input

The first line of the input contains one integer nn (3≤n≤1003≤n≤100) — the number of flats in the house.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (ai∈{0,1}ai∈{0,1}), where aiaiis the state of light in the ii-th flat.

Output

Print only one integer — the minimum number kk such that if people from exactly kkpairwise distinct flats will turn off the light then nobody will be disturbed.

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 110
  4. #define up(i,x,y) for(int i=x;i<=y;i++)
  5. #define down(i,x,y) for(int i=x;i>=y;i--)
  6. typedef long long ll;
  7. int a[maxn];
  8. int main()
  9. {
  10. int n;cin>>n;
  11. up(i,1,n)cin>>a[i];
  12. int res=0;
  13. up(i,2,n-1)
  14. {
  15. if(a[i]==0&&a[i-1]==1&&a[i+1]==1)
  16. {
  17. res++;a[i+1]=0;
  18. }
  19. }
  20. cout<<res<<endl;
  21. }

题解:输入一个n,再输入n个数代表房子灯的亮灭。如果此时a[i]==0&&a[i-1]==1&&a[i+1]==1代表被打扰,res就要++;

贪心,如果此时这个房子的人被打扰了,那么为了消除影响,应该使下一个将要访问的房子置0,因为这样才能使ans尽量小。

C - Good Array

Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1,3,3,7]a=[1,3,3,7] is good because there is the element a4=7a4=7 which equals to the sum 1+3+31+3+3.

You are given an array aa consisting of nn integers. Your task is to print all indices jj of this array such that after removing the jj-th element from the array it will be good (let's call such indices nice).

For example, if a=[8,3,5,2]a=[8,3,5,2], the nice indices are 11 and 44:

  • if you remove a1a1, the array will look like [3,5,2][3,5,2] and it is good;
  • if you remove a4a4, the array will look like [8,3,5][8,3,5] and it is good.

You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.

Input

The first line of the input contains one integer nn (2≤n≤

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/745242
推荐阅读
相关标签
  

闽ICP备14008679号