King
64-bit integer IO format: %lld Java class name: Main
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct arc{ 18 int to,w; 19 arc(int x = 0,int y = 0){ 20 to = x; 21 w = y; 22 } 23 }; 24 vector<arc>g[510]; 25 int n,m,d[510],cnt[500]; 26 bool in[510]; 27 queue<int>q; 28 bool spfa(){ 29 for(int i = 0; i <= n; i++){ 30 d[i] = INF; 31 cnt[i] = 0; 32 in[i] = false; 33 g[n+1].push_back(arc(i,0)); 34 } 35 while(!q.empty()) q.pop(); 36 d[n+1] = 0; 37 cnt[n+1] = 1; 38 q.push(n+1); 39 in[n+1] = true; 40 while(!q.empty()){ 41 int u = q.front(); 42 q.pop(); 43 in[u] = false; 44 for(int i = 0; i < g[u].size(); i++){ 45 if(d[g[u][i].to] > d[u]+g[u][i].w){ 46 d[g[u][i].to] = d[u]+g[u][i].w; 47 if(!in[g[u][i].to]){ 48 in[g[u][i].to] = true; 49 if(++cnt[g[u][i].to] > n) return false; 50 q.push(g[u][i].to); 51 } 52 } 53 } 54 } 55 return true; 56 } 57 int main() { 58 int i,u,v,w; 59 char str[5]; 60 while(scanf("%d",&n),n){ 61 scanf("%d",&m); 62 for(i = 0; i < 500; i++) g[i].clear(); 63 for(i = 0; i < m; i++){ 64 scanf("%d %d %s %d",&u,&v,str,&w); 65 if(str[0] == 'g') 66 g[u+v].push_back(arc(u-1,-w-1)); 67 else g[u-1].push_back(arc(u+v,w-1)); 68 } 69 spfa()?puts("lamentable kingdom"):puts("successful conspiracy"); 70 } 71 return 0; 72 }