Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pic

A tourist hiked along the mountain range. The hike lasted for n days,during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike,meaning that the between any two consecutive days height changes by at most 1,i.e. for all i‘s from 1 to n?-?1 the inequality |hi?-?hi?+?1|?≤?1 holds.

At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover,the numbers in the notes Could have been distorted. Now the tourist wonders what Could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi?-?hi?+?1|?≤?1.

input
The first line contains two space-separated numbers,n and m (1?≤?n?≤?108,1?≤?m?≤?105) — the number of days of the hike and the number of notes left in the journal.

Next m lines contain two space-separated integers di and hdi (1?≤?di?≤?n,0?≤?hdi?≤?108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order,i.e. for all i from 1 to m?-?1 the following condition holds: di?<?di?+?1.

Output
If the notes aren‘t conTradictory,print a single integer — the maximum possible height value throughout the whole route.

If the notes do not correspond to any set of heights,print a single word ‘IMPOSSIBLE‘ (without the quotes).

Examples
input
8 2
2 0
7 0
Output
2
input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample,an example of a correct height sequence with a maximum of 2: (0,?0,?1,?2,?1).

In the second sample the inequality between h7 and h8 does not hold,thus the information is inconsistent.

题意:

游客出去玩了n天,每一天都记录自己在海拔多高,但是他的记录从小偷偷走了,只剩下m天的记录,
他知道每相邻的两天海拔高度相差不超过1,现在让你计算他最大可能能在多高。


思路:

根据读入的具体日期的高度差来判断是否合理并且查出最大的高度可能,

注意第一天和第n天并不是一定在0海拔。

细节见代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <iomanip>#define ALL(x) (x).begin(),(x).end()#define sz(a) int(a.size())#define all(a) a.begin(),a.end()#define rep(i,x,n) for(int i=x;i<n;i++)#define repd(i,n) for(int i=x;i<=n;i++)#define pii pair<int,int>#define pll pair<long long,long long>#define gbtb ios::sync_with_stdio(false),cin.tIE(0),cout.tIE(0)#define MS0(X) memset((X),sizeof((X)))#define MSC0(X) memset((X),'[+++]',sizeof((X)))#define pb push_back#define mp make_pair#define fi first#define se second#define eps 1e-6#define gg(x) getInt(&x)#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endlusing namespace std;typedef long long ll;ll gcd(ll a,ll b) {return b ? gcd(b,a % b) : a;}ll lcm(ll a,ll b) {return a / gcd(a,b) * b;}ll powmod(ll a,ll b,ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}inline voID getInt(int* p);const int maxn = 1000010;const int inf = 0x3f3f3f3f;/*** TEMPLATE CODE * * STARTS HERE ***/int n; int m;pii a[maxn];int main(){    //freopen("D:\common_text\code_stream\in.txt","r",stdin);    //freopen("D:\common_text\code_stream\out.txt","w",stdout);    gbtb;    cin >> n >> m;    repd(i,1,m)    {        cin >> a[i].fi >> a[i].se;    }    sort(a + 1,a + 1 + m);    m = unique(a + 1,a + 1 + m) - a - 1;    int isok = 1;    int x,y,cha;    repd(i,m - 1)    {        x = abs(a[i].se - a[i + 1].se);        y = a[i + 1].fi - a[i].fi;        // cout<<x<<" "<<y<<endl;        if (x > y)        {            isok = 0;            break;        }    }    int ans = 0;    if (isok)    {        repd(i,m - 1)        {            x = max(a[i].se,a[i + 1].se);            y = min(a[i].se,a[i + 1].se);            cha = a[i + 1].fi - a[i].fi - 1;            cha -= x - y;            ans = max(ans,x);            ans = max(ans,(cha + 1) / 2 + x);        }        x = n - a[m].fi + a[m].se;        ans = max(ans,x);        x = a[1].fi - 1 + a[1].se;        ans = max(ans,x);        cout << ans << endl;    } else    {        cout << "IMPOSSIBLE" << endl;    }    return 0;}inline voID getInt(int* p) {    char ch;    do {        ch = getchar();    } while (ch == ' ' || ch == '\n');    if (ch == '-') {        *p = -(getchar() - '0');        while ((ch = getchar()) >= '0' && ch <= '9') {            *p = *p * 10 - ch + '0';        }    }    else {        *p = ch - '0';        while ((ch = getchar()) >= '0' && ch <= '9') {            *p = *p * 10 + ch - '0';        }    }}
总结

以上是内存溢出为你收集整理的Tourist's Notes CodeForces - 538C (贪心)全部内容,希望文章能够帮你解决Tourist's Notes CodeForces - 538C (贪心)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Tourist&#39;s Notes CodeForces - 538C (贪心)_C_内存溢出

Tourist&#39;s Notes CodeForces - 538C (贪心)

Tourist&#39;s Notes CodeForces - 538C (贪心),第1张

概述A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pic

A tourist hiked along the mountain range. The hike lasted for n days,during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike,meaning that the between any two consecutive days height changes by at most 1,i.e. for all i‘s from 1 to n?-?1 the inequality |hi?-?hi?+?1|?≤?1 holds.

At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover,the numbers in the notes Could have been distorted. Now the tourist wonders what Could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi?-?hi?+?1|?≤?1.

input
The first line contains two space-separated numbers,n and m (1?≤?n?≤?108,1?≤?m?≤?105) — the number of days of the hike and the number of notes left in the journal.

Next m lines contain two space-separated integers di and hdi (1?≤?di?≤?n,0?≤?hdi?≤?108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order,i.e. for all i from 1 to m?-?1 the following condition holds: di?<?di?+?1.

Output
If the notes aren‘t conTradictory,print a single integer — the maximum possible height value throughout the whole route.

If the notes do not correspond to any set of heights,print a single word ‘IMPOSSIBLE‘ (without the quotes).

Examples
input
8 2
2 0
7 0
Output
2
input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample,an example of a correct height sequence with a maximum of 2: (0,?0,?1,?2,?1).

In the second sample the inequality between h7 and h8 does not hold,thus the information is inconsistent.

题意:

游客出去玩了n天,每一天都记录自己在海拔多高,但是他的记录从小偷偷走了,只剩下m天的记录,
他知道每相邻的两天海拔高度相差不超过1,现在让你计算他最大可能能在多高。


思路:

根据读入的具体日期的高度差来判断是否合理并且查出最大的高度可能,

注意第一天和第n天并不是一定在0海拔。

细节见代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <iomanip>#define ALL(x) (x).begin(),(x).end()#define sz(a) int(a.size())#define all(a) a.begin(),a.end()#define rep(i,x,n) for(int i=x;i<n;i++)#define repd(i,n) for(int i=x;i<=n;i++)#define pii pair<int,int>#define pll pair<long long,long long>#define gbtb ios::sync_with_stdio(false),cin.tIE(0),cout.tIE(0)#define MS0(X) memset((X),sizeof((X)))#define MSC0(X) memset((X),'',sizeof((X)))#define pb push_back#define mp make_pair#define fi first#define se second#define eps 1e-6#define gg(x) getInt(&x)#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endlusing namespace std;typedef long long ll;ll gcd(ll a,ll b) {return b ? gcd(b,a % b) : a;}ll lcm(ll a,ll b) {return a / gcd(a,b) * b;}ll powmod(ll a,ll b,ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}inline voID getInt(int* p);const int maxn = 1000010;const int inf = 0x3f3f3f3f;/*** TEMPLATE CODE * * STARTS HERE ***/int n; int m;pii a[maxn];int main(){    //freopen("D:\common_text\code_stream\in.txt","r",stdin);    //freopen("D:\common_text\code_stream\out.txt","w",stdout);    gbtb;    cin >> n >> m;    repd(i,1,m)    {        cin >> a[i].fi >> a[i].se;    }    sort(a + 1,a + 1 + m);    m = unique(a + 1,a + 1 + m) - a - 1;    int isok = 1;    int x,y,cha;    repd(i,m - 1)    {        x = abs(a[i].se - a[i + 1].se);        y = a[i + 1].fi - a[i].fi;        // cout<<x<<" "<<y<<endl;        if (x > y)        {            isok = 0;            break;        }    }    int ans = 0;    if (isok)    {        repd(i,m - 1)        {            x = max(a[i].se,a[i + 1].se);            y = min(a[i].se,a[i + 1].se);            cha = a[i + 1].fi - a[i].fi - 1;            cha -= x - y;            ans = max(ans,x);            ans = max(ans,(cha + 1) / 2 + x);        }        x = n - a[m].fi + a[m].se;        ans = max(ans,x);        x = a[1].fi - 1 + a[1].se;        ans = max(ans,x);        cout << ans << endl;    } else    {        cout << "IMPOSSIBLE" << endl;    }    return 0;}inline voID getInt(int* p) {    char ch;    do {        ch = getchar();    } while (ch == ' ' || ch == '\n');    if (ch == '-') {        *p = -(getchar() - '0');        while ((ch = getchar()) >= '0' && ch <= '9') {            *p = *p * 10 - ch + '0';        }    }    else {        *p = ch - '0';        while ((ch = getchar()) >= '0' && ch <= '9') {            *p = *p * 10 + ch - '0';        }    }}
总结

以上是内存溢出为你收集整理的Tourist's Notes CodeForces - 538C (贪心)全部内容,希望文章能够帮你解决Tourist's Notes CodeForces - 538C (贪心)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1211193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存