赞
踩
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:
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.
代码:
- #include<bits/stdc++.h>
- using namespace std;
- #define maxn 110
- #define up(i,x,y) for(int i=x;i<=y;i++)
- #define down(i,x,y) for(int i=x;i>=y;i--)
- typedef long long ll;
- int main()
- {
- int t;cin>>t;
- while(t--)
- {
- int a,b,k;scanf("%d %d %d",&a,&b,&k);
- ll ans=0;
- if(k%2==0)cout<<1LL*(a-b)*k/2<<endl;
- else cout<<1LL*(a-b)*(k/2)+a<<endl;
- }
- }
题解:最初在坐标的0位置,先向右a个单位,再向左b个单位,共持续k个回合。
数学题目,分奇偶讨论一下就可以了,注意不要爆int.
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.
代码:
- #include<bits/stdc++.h>
- using namespace std;
- #define maxn 110
- #define up(i,x,y) for(int i=x;i<=y;i++)
- #define down(i,x,y) for(int i=x;i>=y;i--)
- typedef long long ll;
- int a[maxn];
- int main()
- {
- int n;cin>>n;
- up(i,1,n)cin>>a[i];
- int res=0;
- up(i,2,n-1)
- {
- if(a[i]==0&&a[i-1]==1&&a[i+1]==1)
- {
- res++;a[i+1]=0;
- }
- }
- cout<<res<<endl;
- }
题解:输入一个n,再输入n个数代表房子灯的亮灭。如果此时a[i]==0&&a[i-1]==1&&a[i+1]==1代表被打扰,res就要++;
贪心,如果此时这个房子的人被打扰了,那么为了消除影响,应该使下一个将要访问的房子置0,因为这样才能使ans尽量小。
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:
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≤
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。