POJ 1364 King (differential constraint) (medium)

Source: Internet
Author: User
Tags integer numbers

King
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11028 Accepted: 4040

Description

Once, in one kingdom, there is a queen and that Queen was expecting a baby. The Queen prayed: ' If my child is a son and if only he is a sound king. ' After nine months she is born, and indeed, she gave birth to a nice son.
Unfortunately, as it used to happen in royal families, the son is a little retarded. After many years of study he were able just to add integer numbers and to compare whether the result are greater or less tha n a given integer number. In addition, the numbers had to being written in a sequence and he is able to sum just continuous subsequences of the Sequen Ce.

The old King is very unhappy of his son. But he is ready-to-make everything-to-enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to being presented in a form of a finite sequence of integer numbers and the decision about it would is done by stating an integer constraint (i.e. an Upper or lower limit) for the sum of that sequence. In the This is there is at least some hope, his son would is able to make some decisions.

After the old king died, the young king began to reign. But very soon, a lot of the people became very unsatisfied with he decisions and decided to dethrone him. They tried to does it by proving, decisions were wrong.

Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems is in the form of subsequences Si = {aSi, asi+1, ..., asi+ni} of a sequence S = {A1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + asi+1 + ... + asi+ni of each subsequence Si an I Nteger constraint Ki (i.e. asi + asi+1 + ... + asi+ni < Ki or asi + asi+1 + ... + asi+ni > Ki resp.) and declared th ESE constraints as his decisions.

After a while he realized this some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to him advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the King and write a program this decides whether such a sequence exists or not.

Input

The input consists of blocks of lines. Each block except the last corresponds to one set of problems and King's decisions about them. The first line of the block there be integers n, and m where 0 < n <= is length of the sequence S and 0 < M <= is the number of subsequences Si. Next m lines contain particular decisions coded in the form of Quadruples si, ni, oi, ki, where Oi represents operator ; (coded as GT) or operator < (coded as LT) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy if such a sequence does not exist. Otherwise It contains text lamentable kingdom. There is no line in the output corresponding to the last ' null ' block of the input.

Sample Input

4 2 GT 2 LT 0 GT 0 Lt 00

Sample Output

Lamentable kingdomsuccessful Conspiracy

Idea: The only difficulty with this problem is to read the question. I startedi.e. ASI + asi+1 + ... + asi+ni < Ki or aSi + asi+1 + ... + asi+ni > Ki resp. Wrong, can't push out the formula. Later looked at others the correct test instructions only, knew is very simple thing. Here copy wangjian8006 's test instructions below:

Now suppose there's a sequence like this, s={a1,a2,a3,a4...ai...at}
Which ai=a*si, in fact this sentence can be ignored without looking
Now give an inequality that makes Ai+a (i+1) +a (i+2) +...+a (i+n) <ki or Ai+a (i+1) +a (i+2) +...+a (i+n) >ki
First, we give two numbers representing the number of s sequences, and how many inequalities there are.
Inequalities can be described like this
Given four parameters the first number I can represent the ordinal of the sequence, and then give N, so that the front two numbers can be described as Ai+a (i+1) +...a (i+n), that is, from I to n continuous and, then give a symbol and a Ki
When the symbol for GT stands for ' > ', the symbol for LT stands for ' < '
Then the sample can represent
1 2 GT 0
A1+a2+a3>0
2 2 lt 2
A2+a3+a4<2
Finally asked you whether all inequalities meet the conditions, if the output lamentable kingdom, do not meet the output successful conspiracy, here to pay attention to, do not reverse the

Problem solving: A typical differential constraint, it is easy to introduce constraint inequalities

First set Si=a1+a2+a3+...+ai

Then according to the sample can be drawn
s3-s0>0---->s0-s3<=-1
S4-S1<2---->s4-s1<=1
Because the condition of the differential constraint is less than or equal, we will ki-1 can get an equals sign
Then the general formula can be expressed as
A B GT c
S[a-1]-s[a+b]<=-ki-1
A B LT c
S[a+b]-s[a-1]<=ki-1

Then, based on the differential constraint, these forward edges are added
GT: <a+b,a-1>=-ki-1
LT: <a-1,a+b>=ki-1
Then according to Bellman_ford to determine whether there is no negative ring can be
If a negative ring is present, the sequence does not satisfy all inequalities.


Code:
212k0ms#include<iostream> #include <cstring> #include <cstdio>using namespace std; #define Maxx 105struct edge{int u,v,w;}    Edge[maxx];int n,m;int Dist[maxx];bool Bellman_ford () {memset (dist,0,sizeof (Dist)); for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (dist[edge[j].u]+edge[j].w<dist[edge[        J].V]) DIST[EDGE[J].V]=DIST[EDGE[J].U]+EDGE[J].W;    }} for (int j=0;j<m;j++) {if (DIST[EDGE[J].U]+EDGE[J].W&LT;DIST[EDGE[J].V]) return false; } return true;    int main () {int S,n,k;char o[3];        while (cin>>n,n) {cin>>m;            for (int i=0;i<m;i++) {scanf ("%d%d%s%d", &s,&n,&o,&k);                if (o[0]== ' G ') {edge[i].u=n+s;                edge[i].v=s-1;            Edge[i].w=-k-1;                } else {edge[i].u=s-1;                Edge[i].v=s+n; EdgE[i].w=k-1;        }} if (!bellman_ford ()) cout<< "Successful Conspiracy" <<endl;    else cout<< "Lamentable Kingdom" <<endl; }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1364 King (differential constraint) (medium)

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.