G - Not so Mobile

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.

The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is Wl×Dl = Wr×Dr where Dl is the left distance, Dr is the right distance, Wl is the left weight and Wr is the right weight.

In a more complex mobile the object may be replaced by a sub-mobile, as
shown in the next figure. In this case it is not so straightforward to
check if the mobile is balanced so we need you to write a program that,
given a description of a mobile as input, checks whether the mobile is
in equilibrium or not.

Input

The
input begins with a single positive integer on a line by itself
indicating the number of the cases following, each of them as described
below. This line is followed by a blank line, and there is also a blank
line between two consecutive inputs.

The input is composed of several lines, each containing 4 integers
separated by a single space. The 4 integers represent the distances of
each object to the fulcrum and their weights, in the format:
Wl Dl Wr Dr

If Wl or Wr
is zero then there is a sub-mobile hanging from that end and the
following lines define the the sub-mobile. In this case we compute the
weight of the sub-mobile as the sum of weights of all its objects,
disregarding the weight of the wires and strings. If both Wl and Wr are zero then the following lines define two sub-mobiles: first the left then the right one.

Output

For
each test case, the output must follow the description below. The
outputs of two consecutive cases will be separated by a blank line.

Write `YES‘ if the mobile is in equilibrium, write `NO‘ otherwise.

Sample Input

1

0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2

Sample Output

  YES

一道巧妙使用引用巧妙解答的题目。有点小思维难度。是一道不错的题目。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 const int INF=0x5fffffff;
18 const double EXP=1e-8;
19 const int mod=100000007;
20 const int MS=10005;
21
22 bool solve(int &w)
23 {
24     int w1,d1,w2,d2;
25     bool b1=true,b2=true;
26     cin>>w1>>d1>>w2>>d2;
27     if(!w1)
28         b1=solve(w1);
29     if(!w2)
30         b2=solve(w2);
31     w=w1+w2;
32     return b1&&b2&&(w1*d1==w2*d2);
33 }
34
35 int main()
36 {
37     int T,W;
38     cin>>T;
39     while(T--)
40     {
41         if(solve(W))
42             cout<<"YES"<<endl;
43         else
44             cout<<"NO"<<endl;
45         if(T)
46             cout<<endl;
47     }
48     return 0;
49 }
时间: 2024-11-06 01:16:55

G - Not so Mobile的相关文章

windows下nginx的安装及使用方法入门

nginx功能之一可以启动一个本地服务器,通过配置server_name和root目录等来访问目标文件 一. 下载 http://nginx.org/ 下载后解压 二. 修改配置文件 nginx配置文件在 nginx-1.8.0\conf\nginx.conf http { gzip on; #静态文件 server { listen 80; server_name static.cnblog.com; location / { root G:/source/static_cnblog_com;

Satisfying memory ordering requirements between partial reads and non-snoop accesses

A method and apparatus for preserving?memory?ordering in a cache coherent link based interconnect in light of partial and non-coherent?memory?accesses is herein described. In one embodiment, partial?memory?accesses, such as a partial read, is impleme

Cache coherence protocol

A cache coherence protocol facilitates a distributed cache coherency conflict resolution in a multi-node system to resolve conflicts at a home node. FIELD The invention relates to high-speed point-to-point link networks. More particularly, the invent

SQL SERVER解析Json

外包的项目,有很多信息存储在JSON中,无论是查询还是修改信息都十分麻烦.找了一些实用的SQL Function去解析,并附修改例子. 使用过程: 1. 需要在SQL新建自定义类型 table: Hierarchy; 2. 返回table: Hierarchy的自定义Function:parseJSON; 3 .根据需求新建返回值为NVARCHAR(MAX)的: To_Json或To_XML自定义Function. 最后结果如下: --Update Json and Return JsonDEC

[转]An introduction to OAuth 2.0 using Facebook in ASP.NET Core

本文转自:http://andrewlock.net/an-introduction-to-oauth-2-using-facebook-in-asp-net-core/ This is the next post in a series on authentication and authorisation in ASP.NET Core. In this post I look in moderate depth at the OAuth 2.0 protocol as it pertain

PatentTips - SNMP firewall

BACKGROUND OF THE INVENTION [0001] The present invention relates to communications and, more particularly, to systems and methods for exchanging small network management protocol (SNMP) messages in large complex networks. [0002] Managing and monitori

The WebSocket Protocol

[Docs] [txt|pdf] [draft-ietf-hybi-t...] [Diff1] [Diff2] [Errata] Updated by: 7936 PROPOSED STANDARD Errata Exist Internet Engineering Task Force (IETF) I. Fette Request for Comments: 6455 Google, Inc. Category: Standards Track A. Melnikov ISSN: 2070-

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

jquery mobile 带参数跳转收集(紧个人使用,测试完会补全)

//临时存储 var TempCache = { cache:function(value){ localStorage.setItem("EasyWayTempCache",value); }, getCache:function(){ return localStorage.getItem("EasyWayTempCache"); }, setItem:function(key,value){ localStorage.setItem(key,value); }