# Zoj 3829 greedy thinking questions

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3829.

When I was doing this on site, I felt like I was thinking questions. I didn't have enough IQ to do it. I thought my teammates had a good IQ. They did, but they didn't come up with this question ......

Be confident at any time in the future .... I should think about it myself. This kind of question is to find the nature first, and then find the rule a little bit. If necessary, I will make a guess, and if I cannot give a counterexample, I think it is correct for the moment.

After one afternoon, I found that it was still a tragedy. At night, I referred to two questions.

Http://blog.csdn.net/keshuai19940722/article/details/40039975

In fact, we can at least summarize the rule:
1. Must be num (*) <num (number)

2. It is legal that * is followed by all numbers. That is to say, if you need to switch *, you can change * to the last, that is, to switch * to the last digit, it costs 1 at a time.

3. Because one exchange consumes 1 and one insertion consumes 1, if rule 1 is not met, you can insert it first, and because Rule 2, so insert all the numbers at the beginning to the beginning, and use the stack to simulate the suffix expression verification process. If the number is missing, the last number is switched to the current * position, rule 2. no asterisks are missing, because continuous numbers can be treated as the same number.

The three above are enough to solve the problem.

`# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; const int maxn = 1000 + 50; # define Cl (A, B) memset (a, B, sizeof (A) # define ll long # define ull unsigned long # define in (s) freopen (S, "r", stdin) char STR [maxn], Sta [maxn * 10]; int POS [maxn * 10]; int Len, NUMA, numb, TP, postp; void Init () {TP = postp = 0; NUMA = numb = 0; // scanf ("% s", STR); Len = strlen (STR);} ll solve () {(INT I = 0; I <Len; I ++) {If (STR [I] = '*') NUMA ++; else {numb ++; pos [postp ++] = I ;}} if (NUMA = 0) return 0; // *** special sentence ll ans = 0; TP = max (NUMA + 1-numb, 0); // if there are many numbers, you can always combine them. // Add the number ans = (LL) TP at the beginning; for (INT I = 0; I <Len; I ++) {If (STR [I] = '*') {If (TP> = 2) TP --; else {STR [POS [postp-1] = '*'; postp --; TP ++; ans ++; // exchange without emphasizing adjacent} else TP ++ ;} if (ANS = 0 & STR [len-1]! = '*') Ans ++; // return ans;} int main () {// In ("k.txt"); int ncase; scanf ("% d ", & ncase); While (ncase --) {Init (); printf ("% LLD \ n", solve ();} return 0 ;}`

Zoj 3829 greedy thinking questions

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.