SDNU_ACM_ICPC_2019_Winter_Practice_4th 题解

本文提供了ACM竞赛2019冬季训练第四场的多道题目解题思路,包括Frog Jumping、Disturbed People、Good Array、Cutting Out等题目。解题涉及数组、跳跃、贪心算法、二分查找等多种算法和策略。
摘要由CSDN通过智能技术生成

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.

代码:

#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.

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.

代码:

#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尽量小。

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≤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值