CF #167 div3

A. Array with Odd Sum

link
You are given an array a consisting of n integers.

In one move, you can choose two indices 1≤i,j≤n such that i≠j and set ai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose i and j and replace ai with aj).

Your task is to say if it is possible to obtain an array with an odd (not divisible by 2) sum of elements.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2000) — the number of test cases.

The next 2t lines describe test cases. The first line of the test case contains one integer n (1≤n≤2000) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤2000), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑n≤2000).

Output
For each test case, print the answer on it — “YES” (without quotes) if it is possible to obtain the array with an odd sum of elements, and “NO” otherwise.

统计奇数个数f,偶数个数n-f。能达到奇数个f则YES

#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
typedef double db;
typedef unsigned long long ull;
#define rs rt<<1|1
#define Ls rt<<1
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
const LL MOD=1e9+7;
const int maxn=3510;
const LL linf=1e18;
const int inf=0x3f3f3f3f;
LL qpow(LL a,LL b){return b?((b&1)?a*qpow(a*a%MOD,b>>1)%MOD:qpow(a*a%MOD,b>>1))%MOD:1;}
LL qpow(LL a,LL b,LL c){return b?((b&1)?a*qpow(a*a%MOD,b>>1)%c:qpow(a*a%MOD,b>>1))%c:1;}
inline int ird()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline LL lrd()
{
    register LL x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
int gcd(int a,int b)
{
     return (b>0)?gcd(b,a%b):a;
}
 
int a[maxn],n,ans;
int main(){
    int t=ird();
    while(t--){
        n=ird();
        ans=0;
        int f=0;
        for(int i=1;i<=n;i++){
            a[i]=ird();
            ans+=a[i];
            if(a[i]%2==1)
                f++;
        }
        if(f%2!=0||(n-f>0&&f>0))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

B. Food Buying

link
Mishka wants to buy some food in the nearby shop. Initially, he has s burles on his card.

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s, buy food that costs exactly x burles and obtain ⌊x10⌋ burles as a cashback (in other words, Mishka spends x burles and obtains ⌊x10⌋ back). The operation ⌊ab⌋ means a divided by b rounded down.

It is guaranteed that you can always buy some food that costs x for any possible value of x.

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19 burles then the maximum number of burles he can spend is 21. Firstly, he can spend x=10 burles, obtain 1 burle as a cashback. Now he has s=10 burles, so can spend x=10 burles, obtain 1 burle as a cashback and spend it too.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

The next t lines describe test cases. Each test case is given on a separate line and consists of one integer s (1≤s≤109) — the number of burles Mishka initially has.

Output
For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

LL a[maxn],n,ans;
int main(){
    int t=ird();
    while(t--){
        n=lrd();
        LL ans=n;
        while(n/10!=0){
            ans+=n/10;
            int tt=n;
            n%=10;
            n+=tt/10;
        }
        cout<<ans<<endl;
    }
    return 0;
}

C. Yet Another Walking Robot

:link
There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.

Each of these characters corresponds to some move:

‘L’ (left): means that the robot moves from the point (x,y) to the point (x−1,y);
‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y);
‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1);
‘D’ (down): means that the robot moves from the point (x,y) to the point (x,y−1).
The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn’t want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye), then after optimization (i.e. removing some single substring from s) the robot also ends its path at the point (xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot’s path such that the endpoint of his path doesn’t change. It is possible that you can’t optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string s).

Recall that the substring of s is such string that can be obtained from s by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of “LURLLR” are “LU”, “LR”, “LURLLR”, “URL”, but not “RR” and “UL”.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The next 2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of the robot’s path. The second line of the test case contains one string s consisting of n characters ‘L’, ‘R’, ‘U’, ‘D’ — the robot’s path.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot’s path doesn’t change, print -1. Otherwise, print two integers l and r such that 1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1 should be minimum possible. If there are several answers, print any of them.

寻找最小轮回的下标。
统计LR、UD贡献值su、sr。map遍历位置更新贡献起始节点。题目要求min所以每次贡献值最后更新为当前值。当两位置贡献值相等时则相减为0,形成一轮回。

char s[maxn];
int n;
int main(){
    int t=ird();
    while(t--){
        n=ird();
        int ans=maxn,sr=0,su=0,f1=0,f2=0;
        scanf("%s",s+1);
        map< int,map<int,int> > m;
        m[0][0]=0;
        for(int i=1;i<=n;i++){
            if(s[i]=='R')
                sr++;
            else if(s[i]=='L')
                sr--;
            else if(s[i]=='U')
                su++;
            else
                su--;
            if(((sr==0&&su==0)||m[sr][su]!=0)&&ans>i-m[sr][su]){
                ans=i-m[sr][su];
                f2=i;
                f1=m[sr][su]+1;
            }
            m[sr][su]=i;
        }
        if(f1&&f2){
            cout<<f1<<" "<<f2<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}

D. Fight with Monsters

link
There are n monsters standing in a row numbered from 1 to n. The i-th monster has hi health points (hp). You have your attack power equal to a hp and your opponent has his attack power equal to b hp.

You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 0.

The fight with a monster happens in turns.

You hit the monster by a hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
Your opponent hits the monster by b hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.
You have some secret technique to force your opponent to skip his turn. You can use this technique at most k times in total (for example, if there are two monsters and k=4, then you can use the technique 2 times on the first monster and 1 time on the second monster, but not 2 times on the first monster and 3 times on the second monster).

Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.

Input
The first line of the input contains four integers n,a,b and k (1≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent’s attack power and the number of times you can use the secret technique.

The second line of the input contains n integers h1,h2,…,hn (1≤hi≤109), where hi is the health points of the i-th monster.

Output
Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

水,别读错题。
每次到下一个monster还是从a开始

int aa[maxn];
int n,a,b,k;
int main(){
    n=ird();
    a=ird();
    b=ird();
    k=ird();
    int ans=0;
    for(int i=1;i<=n;i++){
        int t=ird();
        aa[i]=(t+a+b-1)%(a+b)/a;
    }
    sort(aa+1,aa+1+n);
    for(int i=1;i<=n;i++){
        if(aa[i]<=k){
            ans++;
            k-=aa[i];
        }
        else
            break;
    }
    cout<<ans<<endl;
    return 0;
}
int main(){
    n=ird();
    a=ird();
    b=ird();
    k=ird();
    int ans=0;
    for(int i=1;i<=n;i++){
        int t=ird();
        aa[i]=t%(a+b)/a;
        if(t%(a+b)==0)
            aa[i]+=(a+b)/a+((a+b)%a!=0);
        else
            aa[i]+=(t%(a+b)%a!=0);
        aa[i]--;
    }
    sort(aa+1,aa+1+n);
    for(int i=1;i<=n;i++){
        if(aa[i]<=k){
            ans++;
            k-=aa[i];
        }
        else
            break;
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值