HDU 3305 Ice-sugar Gourd

本文介绍了一个关于如何公平分配两种水果制成的冰糖葫芦的问题。通过编程的方式解决将冰糖葫芦平均分成两份的挑战,确保每份含有相同数量的山楂和橘子。文章详细解释了解决方案的思路,并提供了完整的代码实现。

Ice-sugar Gourd

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 936    Accepted Submission(s): 329


Problem Description
Ice-sugar gourd, “bing tang hu lu”, is a popular snack in Beijing of China. It is made of some fruits threaded by a stick. The complicated feeling will be like a both sour and sweet ice when you taste it. You are making your mouth water, aren’t you?

I have made a huge ice-sugar gourd by two kinds of fruit, hawthorn and tangerine, in no particular order. Since I want to share it with two of my friends, Felicia and his girl friend, I need to get an equal cut of the hawthorns and tangerines. How many times will I have to cut the stick so that each of my friends gets half the hawthorns and half the tangerines? Please notice that you can only cut the stick between two adjacent fruits, that you cannot cut a fruit in half as this fruit would be no good to eat.
 

 

Input
The input consists of multiply test cases. The first line of each test case contains an integer, n(1 <= n <= 100000), indicating the number of the fruits on the stick. The next line consists of a string with length n, which contains only ‘H’ (means hawthorn) and ‘T’ (means tangerine).
The last test case is followed by a single line containing one zero.
 

 

Output
Output the minimum number of times that you need to cut the stick or “-1” if you cannot get an equal cut. If there is a solution, please output that cuts on the next line, separated by one space. If you cut the stick after the i-th (indexed from 1) fruit, then you should output number i to indicate this cut. If there are more than one solution, please take the minimum number of the leftist cut. If there is still a tie, then take the second, and so on.
 

 

Sample Input
4 HHTT 4 HTHT 4 HHHT 0
 

 

Sample Output
2 1 3 1 2 -1
 
 
开始用双重for循环,超时了,还是要用一个数组nh[i]来记录到第i个有多少个h
然后从前往后的结果
 
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 123456
int n,cnt_h,cnt_t;
char a[N];
int nh[N];

int main()
{
    while(~scanf("%d",&n))
    {
        getchar();
        if(n==0)break;
        
        cnt_h=0;
        for(int i=0;i<n;i++)
        {
            scanf("%c",&a[i]);
            if(a[i]=='H')cnt_h++;
            nh[i]=cnt_h;
        }
        if(n&1)
        {
            cout<<-1<<endl;
            continue;
        }
        if(cnt_h&1)
        {
            cout<<-1<<endl;
            continue;
        }
        if(nh[n/2-1]==cnt_h/2)
        {
            printf("1\n%d\n",n/2);
            continue;
        }
        for(int i=1;i<n;i++)
        {
            if(nh[n/2+i-1]-nh[i-1]==cnt_h/2)
            {
                printf("2\n%d %d\n",i,i+n/2);
            break;
            }

        }
    }
    return 0;
}

//freopen("1.txt", "r", stdin);
    //freopen("2.txt", "w", stdout);
//**************************************

 

转载于:https://www.cnblogs.com/wmxl/p/4676855.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值