SRM 458 Div 2

Source: Internet
Author: User
Tags bitset cmath

# Include <vector> <br/> # include <list> <br/> # include <map> <br/> # include <set> <br/> # include <deque> <br/> # include <stack> <br/> # include <bitset> <br/> # include <algorithm> <br/> # include <functional> <br/> # include <numeric> <br/> # include <utility> <br/> # include <sstream> <br/> # include <iostream> <br/> # include <iomanip> <br/> # include <cstdio> <br/> # include <cmath> <br/> # include <cstdlib> <br/> # includ E <ctime> <br/> # include <string> <br/> # include <cstring> <br/> using namespace STD; </P> <p> class <br/> desertification <br/> {<br/> Public: <br/> int desertarea (vector <string> Island, int T) <br/>{< br/> vector <string> P; <br/> If (T> = 20) <br/> T = 21; <br/> while (t --) <br/>{< br/> P = Island; <br/> for (INT I = 0; I <p. size (); I ++) <br/>{< br/> for (Int J = 0; j <p [0]. size (); j ++) <br/>{< br/> If (Island [I] [J] = 'D') <br/>{< br /> If (I> 0) <br/> P [I-1] [J] = 'D'; <br/> If (j> 0) <br/> P [I] [J-1] = 'D'; <br/> if (I + 1 <p. size () <br/> P [I + 1] [J] = 'D'; <br/> If (J + 1 <p [0]. size () <br/> P [I] [J + 1] = 'D '; </P> <p >}< br/> island = P; <br/>}< br/> int COUNT = 0; <br/> for (INT I = 0; I <p. size (); I ++) <br/>{< br/> for (Int J = 0; j <p [0]. size (); j ++) <br/>{< br/> If (P [I] [J] = 'D') <br/> count ++; <br/>}< br/> return count; <br/>}< br/> }; <br/>/* <br/> another idea is: Search directly, but nested layer-4 loops are required! <Br/> */

 

The first problem is that it is obviously a little slow to write, and then it is like this ..

At that time, I was wondering whether to adopt the second method!

This problem eventually fails because [0] is missing... Sorry .. This is the first time in history that rank1 has failed...

 

# Include <vector> <br/> # include <list> <br/> # include <map> <br/> # include <set> <br/> # include <deque> <br/> # include <stack> <br/> # include <bitset> <br/> # include <algorithm> <br/> # include <functional> <br/> # include <numeric> <br/> # include <utility> <br/> # include <sstream> <br/> # include <iostream> <br/> # include <iomanip> <br/> # include <cstdio> <br/> # include <cmath> <br/> # include <cstdlib> <br/> # includ E <ctime> </P> <p> using namespace STD; </P> <p> class bouncingbils <br/>{< br/> public: <br/> double expectedbounces (vector <int>, INT); <br/>}; </P> <p> double bouncingbils: expectedbounces (vector <int> X, int t) <br/>{< br/> int n = x. size (), ANS = 0, T, I, J, dir [99]; <br/> sort (X. begin (), X. end (); <br/> for (t = 0; t <(1 <n); t ++) <br/> {<br/> for (I = 0; I <n; I ++) <br/>{< br/> If (T & (1 <I) <br/> dir [I] = 1; <br /> Else dir [I] =-1; <br/>}< br/> for (I = 0; I <n; I ++) <br/> for (j = I + 1; j <n; j ++) <br/> If (dir [I] = 1 & dir [J] =-1 & T * 2> = x [J]-X [I]) ans ++; <br/>}< br/> return 1.0 * ANS/(1 <n ); <br/>}</P> <p>/* <br/> the above Code is indeed simplified! Clever Use of bit operations (T & (1 <I) <br/> the essence of this problem is that it does not need to be sorted !! <Br/> only one combination of directions can ensure collision! The expectation is obviously 0.25. <br/> the core code is only: <br/> int n = x. length, I, j; <br/> double ans = 0.0; <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> for (Int J = 0; j <n; j ++) <br/> {<br/> If (X [I] <X [J] & X [J]-X [I] <= 2 * t) <br/> ans + = 0.25; <br/>}< br/> return ans; <br/> */

The second question was answered quickly and then made .. Because I have been counting twice, the last result is 2 <size (). As a result, the person in the room is misled to go to Cha and then fails .. Is it too bad for me ..

 

Import Java. util. *; <br/> public class producttriplet <br/> {<br/> Public long func (INT Minx, int Maxx, int miny, int Maxy, int minz, int maxz) <br/>{< br/> If (minx! = 1) <br/> return func (1, Maxx, miny, Maxy, minz, maxz)-func (1, Minx-1, miny, Maxy, minz, maxz ); <br/> If (miny! = 1) <br/> return func (Minx, Maxx, 1, Maxy, minz, maxz)-func (Minx, Maxx, 1, miny-1, minz, maxz ); <br/> If (minz! = 1) <br/> return func (Minx, Maxx, miny, Maxy, 1, maxz)-func (Minx, Maxx, miny, Maxy, 1, minz-1 ); <br/> long x = Maxx, y = Maxy, Z = maxz, ANS = 0, x, y; <br/> for (x = 1; x * x <= Z & x <= x; X ++) <br/> {<br/> Y = math. min (z/x, y); <br/> If (Y> = x) <br/> ans + = (Y-x + 1 ); <br/>}< br/> for (y = 1; y * Y <= Z & Y <= y; y ++) <br/>{< br/> X = math. min (z/y, x); <br/> If (x> Y) <br/> ans + = (x-y ); <br/>}< br/> return ans; <br/>}< br/> Public long counttriplets (INT Minx, int Maxx, Int miny, int Maxy, int minz, int maxz) <br/>{< br/> long ans = func (Minx, Maxx, miny, Maxy, minz, maxz ); <br/> return ans; <br/>}< br/>/* </P> <p> A very delicate answer, the first idea is to convert a count with intervals into a single interval. <Br/> now, we have gone through an iterative process <br/> the second part is to find the answer (1, Maxx, 1, Maxy, 1, maxz! <Br/> the answer must be solved within O (n. This is divided into a group of x> Y, and a group of x <= y! </P> <p> for (x = 1; x * x <= Z & x <= x; X ++) <br/>{< br/> Y = math. min (z/x, y); <br/> If (Y> = x) <br/> ans + = (Y-x + 1 ); <br/>}< br/> */

At that time, I did not think of a method to reduce the complexity, So I simply did not write it at will. This problem is still very clever! Learning !!!

 

This is the first chance to take the money .. The result is a very low-level error! Sorry, this is already happening several times...

Code more and exercise yourself!

 

PS: This is the first time I used notepad to write code. I had to stick to it for so long using Zhang yuze's computer .. Good results .. Haha!

Contact Us

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.