一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第26天,点击查看活动详情。
题目描述
Problem - M. Bottle Arrangements
Gabriella has been instructed to organize a renowned wine tasting event which will be attended by m critics. On display, there will be n different varieties of wine, each of which can either be a red wine or a white wine.
The wines will come in n bottles arranged in a line on the table, and, for convenience, each critic will sip from a contiguous interval of bottles: that is, hee wants to taste exactly ri red wines and wi white wines.
Gabriella has yet to choose how many bottles of red wine and white wine there will be, and in what order they will appear. Help her find an arrangement (that is, a sequence of n bottles of either red or white wine) that satisfies the requests of all the critics, or state that no such arrangement exists.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The descriptions of the t test cases follow.
The first line of each test case contains two integers n and m (1≤n≤100, 1≤m≤100) — the number of bottles of wine and the number of critics.
Each of the next m lines contains two integers ri and wi (0≤ri,wi≤100, ri+wi≥1) — the number of red and white wines that the i-th critic wants to taste.
Output
For each test case, if at least one solution exists, print a string of length n made up of the characters R and W, where the j-th character (1≤j≤n) denotes the type of the wine in the j-th bottle of the arrangement (R for red and W for white). If there are multiple solutions, print any.
If no solution exists, print the string IMPOSSIBLE.
Example
input
3
5 3
1 0
3 2
2 2
4 3
2 1
1 1
0 3
3 2
0 2
0 3
output
RWRRW
IMPOSSIBLE
WWW
Note
In the first test case, there are n=5 bottles of wine to be arranged and m=3 critics. The arrangement RWRRW satisfies the requests of all three critics. Indeed:
- the first critic can choose the interval [3,3], which contains exactly one bottle of red wine (note that [1,1] and [4,4] are other valid choices);
- the second critic can choose the interval [1,5], which contains 33 bottles of red wine and 22 bottles of white wine;
- the third critic can choose the interval [2,5], which contains 22 bottles of red wine and 22 bottles of white wine.
问题解析
这题是说,有n瓶酒,这些酒有两种品种,白或红,有多少红多少白你决定反着两者加一起要等于n,有m个人来喝酒,他们只喝一个区间内(这个区间你定)的酒,这个区间要有a个红,b个白,问你能不能把这些酒按顺序摆好,使得满足所有人的要求,不能就输出impossible,能就输出任意一种方法。
先记录下每个人要和的红酒最大数量,和白酒最大数量,如果这两个数量相加大于n,那肯定不能满足所有人。不然,我们直接把这些酒挨近摆就可以了,前面都是白后面都是红,或者前面都是红后面都是白也可以,这样必然能满足所有人要求。要注意,这两个值相加不一定等于n,所有我们摆的时候,某个品种要多摆一点,直到等于n。
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n';
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1e6 + 50;
int main()
{
int t;
cin >> t;
while (t--)
{
map<int, PII>mymap;
ll n, m, a = 0, b = 0;
cin>>n>>m;
vector<PII>v(m);
for (int i = 0; i < m; i++)
{
cin >> v[i].first >> v[i].second;
a = max(a, v[i].first);
b = max(b, v[i].second);
mymap[v[i].first + v[i].second] = v[i];
}
if (a + b > n)
{
cout << "IMPOSSIBLE" << endl;
continue;
}
string s;
for (int i = 0; i < a; i++)s += 'R';
for (int i = 0; i < b; i++)s += 'W';
for (int i = s.size(); i < n; i++)s += 'W';
cout << s << endl;
}
return 0;
}