Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11720 | Accepted: 4278 |
Description
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.
The old king was very unhappy of his son. But he was 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 be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions.
After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his 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 was 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 integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions.
After a while he realized that 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 his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
Input
Output
Sample Input
4 2 1 2 gt 0 2 2 lt 2 1 2 1 0 gt 0 1 0 lt 0 0
Sample Output
lamentable kingdom successful conspiracy
*题目大意:
* 已知一个序列a[1], a[2], ......, a[n],给出它的若干子序列以及对该子序列的
* 约束条件,例如a[si], a[si+1], a[si+2], ......, a[si+ni],且a[si]+a[si+1]
* +a[si+2]+......+a[si+ni] < or > ki。求是否存在满足以上m个要求的数列。
* 是则输出“lamentable kingdom”,否则输出“successful conspiracy”。
*解题思路:
* s[a] + s[a+1] + …… + s[b] < c 可以转化成前n项和sum[b] - sum[a - 1] < c,
* 为了能用Bellman_Ford,即将< 转化成 <= ,sum[b] - sum[a - 1] <= c - 1。
* 转化为最长路或者最短路来判断是否有解都可以。
*解题感想:
* 注意题目求约束的最大,或者最小时,就要判断此刻要用的是最短路(<=),还是最长路(>=)。
分析:设 dis[i]表示 a[1]--a[i-1] 累加和,则有
dis[a+b] - dis[a-1] > x or < x <=> dis[a+b] - dis[a-1] >= x+1<=> dis[a-1] - dis[a+b] <= -x-1
or dis[a+b] - dis[a-1] <= x-1. 再求最短路并判环。
代码如下:
法一(bellman 算法)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define N 120
#define inf 0x3f3f3f3f
int head[N], vis[N], dis[N], judge[N], k, n;
struct r
{
int u, v, w, next;
}p[N];
void add(int u, int v, int w)
{
p[k].u=u;
p[k].v=v;
p[k].w=w;
p[k].next=head[u];
head[u]=k++;
}
bool bellman()
{
int i, j;
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
for(j=0;j<k;j++){
if(dis[p[j].v]>dis[p[j].u]+p[j].w)
dis[p[j].v]=dis[p[j].u]+p[j].w;
}
}
for(i=0;i<k;i++){//判环
if(dis[p[i].v]>dis[p[i].u]+p[i].w)
return true;
}
return false;
}
int main()
{
#ifdef OFFLINE
freopen("t.txt","r",stdin);
#endif
int i, j, m, a, b, x;
char s[3];
while(~scanf("%d",&n)&&n){
scanf("%d",&m);
k=0;
memset(head,-1,sizeof(head));
while(m--){
scanf("%d%d%s%d",&a,&b,s,&x);
if(s[0]=='g')
add(a+b,a-1,-x-1);//dis[a+b]-dis[a-1]>x -> dis[a-1]-dis[a+b]<=-x-1
else
add(a-1,a+b,x-1);//dis[a+b]-dis[a-1]<x -> dis[a+b]-dis[a-1]<=x-1
}
if(bellman())
puts("successful conspiracy");
else
puts("lamentable kingdom");
}
return 0;
}
法二(spfa 判环)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define N 120
#define inf 0x3f3f3f3f
int head[N], vis[N], dis[N], judge[N], k, n;
struct r
{
int u, v, w, next;
}p[N * 10];//注意数组开大点!
void add(int u, int v, int w)
{
p[k].v = v;
p[k].w = w;
p[k].next = head[u];
head[u] = k++;
}
bool spfa()
{
int i, j;
memset(vis, 0, sizeof(vis));
memset(judge, 0, sizeof(judge));
memset(dis, inf, sizeof(dis));
queue<int >q;
q.push(0);
vis[0] = 1;
judge[0] = 1;
dis[0] = 0;
while (!q.empty()){
int a = q.front(); q.pop();
vis[a] = 0;
for (i = head[a]; i + 1; i = p[i].next){
if (dis[p[i].v] > dis[a] + p[i].w){//最短路
dis[p[i].v] = dis[a] + p[i].w;
if (!vis[p[i].v]){
q.push(p[i].v);
vis[p[i].v] = 1;
if (++judge[p[i].v] > n + 2)//(n+2) 个点!
return true;
}
}
}
}
return false;
}
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
int i, j, k, m, a, b, x;
char s[3];
while (~scanf("%d", &n) && n){
scanf("%d", &m);
k = 0;
memset(head, -1, sizeof(head));
while (m--){
scanf("%d%d%s%d", &a, &b, s, &x);
if (s[0] == 'g')//dis[x]==dis[1]--dis[x-1]
add(a + b + 1, a, -x - 1);
//dis[a+b+1]-dis[a]>x -> dis[a]-dis[a+b+1]<=-x-1
else
add(a, a + b + 1, x - 1);
//dis[a+b+1]-dis[a]<x -> dis[a+b+1]-dis[a]<=x-1
}
for (i = 1; i <= n + 1; i++)
add(0, i, 0);//添加 0 为超级源点
if (spfa())
puts("successful conspiracy");
else
puts("lamentable kingdom");
}
return 0;
}