Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> vpii(m);
REP(i, m){
cin >> vpii[i].first >> vpii[i].second;
}
int ans = n+1;
int count = 0;
int beg=0;
FOR(i, 1, n + 1){
REP(j, m){
if (vpii[j].first == i){
count++;
if (count == 1)
beg = i;
}
if (vpii[j].second == i){
count--;
if (count == 0)
ans += (i - beg) * 2;
}
}
}
cout << ans << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
typedef pair<int,int> ii;
int N,m,par[1010];
int find(int x) {
if( x == par[x] ) return x;
return par[x] = find(par[x]);
}
void unit(int x,int y) {
x = find(x), y = find(y);
if( x != y ) par[x] = y;
}
int main(){
cin >> N >> m;
int cost = N+1,c,d;
rep(i,N+2) par[i] = i;
rep(i,m){
cin >> c >> d;
REP(j,c,d) unit(j,j+1);
}
map<int,int> group;
REP(i,1,N+1) ++group[find(i)];
for(map<int,int>::iterator it = group.begin(); it != group.end(); it++ ){
if( it->second <= 1 ) continue;
cost += ( it->second - 1 ) * 2;
}
cout << cost << endl;
return 0;
}
/********
* 1000 *
********/
/*********************
* go on to the next *
*********************/
/**************
* thank you! *
**************/
/**************
* help me... *
**************/ |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <cstring>
using namespace std;
void solve()
{
cin.tie(0);
ios::sync_with_stdio(false);
bool needToMoveThreeTimes[1001];
memset(needToMoveThreeTimes, 0, sizeof(needToMoveThreeTimes));
int N, m;
cin >> N >> m;
for (int i = 0; i < m; ++i)
{
int c, d;
cin >> c >> d;
for (int j = c; j < d; ++j)
{
needToMoveThreeTimes[j] = true;
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
{
if (needToMoveThreeTimes[i])
{
ans += 2;
}
}
cout << ans + N + 1 << endl;
}
int main()
{
solve();
return(0);
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, m; cin >> N >> m;
vector<int> F(N+1, 1);
for(int i = 0; i < m; ++i) {
int c, d; cin >> c >> d;
for(int j = c; j < d; ++j) F[j] = 3;
}
cout << accumulate(F.begin(), F.end(), 0) << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // ACM-ICPCアジア地区予選2014 C. Shopping
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int N, m;
while(cin >> N >> m){
vector< pair<int,int> > p(m);
for(int i=0;i<m;i++) cin >> p[i].first >> p[i].second;
sort(p.begin(), p.end());
int idx = 0;
int res = N+1;
while(idx < m){
int b = p[idx].first, e = p[idx].second;
while(idx < m && p[idx].first < e){
e = max(e, p[idx].second);
++idx;
}
res += 2*(e-b);
}
cout << res << endl;
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
int n,m,a,b,ans=1;
int c[1001];
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d %d",&a,&b);
c[a]++;
c[b]--;
}
for(int i=1;i<=n;i++){
c[i]+=c[i-1];
ans+=(c[i]?3:1);
}
printf("%d\n",ans);
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<algorithm>
#define f first
#define s second
#define inf 1000000001
using namespace std;
int main()
{
int N,m,c,d;
vector<pair<int,int> > V;
cin>>N>>m;
for(int i=0;i<m;i++){
cin>>c>>d;
V.push_back(make_pair(c,d));
}
sort(V.begin(),V.end());
V.push_back(make_pair(inf,inf));
for(int i=0;i<V.size()-1;i++){
if(V[i+1].f<=V[i].s && V[i].s<=V[i+1].s){
V[i].s=V[i+1].s;
V.erase(V.begin()+i+1);
i--;
}
if(V[i].f<=V[i+1].f && V[i+1].s <=V[i].s){
V.erase(V.begin()+i+1);
i--;
}
}
/*
cout<<endl;
for(int i=0;i<V.size();i++)cout<<V[i].f<<" "<<V[i].s<<" "<<i<<endl;
*/
int ans=N+1;
for(int i=0;i<V.size();i++){
ans+=2*(V[i].s-V[i].f);
}
cout<<ans<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <algorithm>
#include <iostream>
#include <vector>
#define push push_back
#define sz size()
#define s second
#define f first
using namespace std;
typedef pair<int,int> P;
int main(){
int N,m,c,d,ans=0;
vector<int> v2;
vector<P> v;
cin>>N>>m;
for(int i=0;i<m;i++) cin>>c>>d,v.push(make_pair(c,d));
sort(v.begin(),v.end());
if(v.sz!=0)
for(int i=0;i<v.sz-1;i++)
if(v[i+1].f<=v[i].s){
v[i].s=max(v[i].s,v[i+1].s);
v.erase(v.begin()+i+1);
i--;
}
for(int i=0;i<v.sz;i++) v2.push(v[i].s-v[i].f+1),ans+=v2[i];
ans=N+1-ans;
for(int i=0;i<v2.sz;i++) ans+=v2[i]*3-2;
cout<<ans<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
long start = System.currentTimeMillis();
long fin = System.currentTimeMillis();
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
int N = sc.nextInt();
int m = sc.nextInt();
int[] c = new int[N + 1];
for (int i = 0; i < m; i++) {
c[sc.nextInt()]++;
c[sc.nextInt()]--;
}
int pos = 0;
int cnt = 0;
int lost = 0;
int left = 0;
while (++pos != N + 1) {
cnt++;
if (c[pos] > 0) {
lost += c[pos];
if (left == 0) left = pos;
} else if (c[pos] < 0) {
lost += c[pos];
if (lost == 0) {
cnt += (pos - left) * 2;
left = 0;
}
}
}
System.out.println(++cnt);
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(char[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (k <= a[i])
int lower_bound(int a[], int k) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (k <= a[mid])
r = mid;
else
l = mid;
}
// r = l + 1
return r;
}
// find minimum i (k < a[i])
int upper_bound(int a[], int k) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (k < a[mid])
r = mid;
else
l = mid;
}
// r = l + 1
return r;
}
long gcd(long a, long b) {
return a % b == 0 ? b : gcd(b, a % b);
}
long lcm(long a, long b) {
return a * b / gcd(a, b);
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // 基本テンプレート
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
#define fprintf(...) void(0)
int imos[1010];
signed main() {
int N, M; cin >> N >> M;
for(int i=0; i<M; i++) {
int l, r; cin >> l >> r;
imos[l]++;
imos[r]--;
}
for(int i=0; i<N+3; i++) {
imos[i+1] += imos[i];
}
int ans = N+1, last = -1;
for(int i=0; i<N+3; i++) {
if(imos[i+1] > 0 && imos[i] == 0) {
last = i+1;
}
if(imos[i+1] == 0 && imos[i] > 0) {
ans += (i+1 - last) * 2;
fprintf(stderr, "add: [%lld, %lld)\n", last, i+1);
}
}
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int n, m;
cin >> n >> m;
vector<pair<int,int>> cds;
for(int i = 0; i < m; i++) {
int c, d;
cin >> c >> d;
cds.push_back(make_pair(c,d)); // visits c after d (> c)
}
sort(cds.begin(), cds.end());
// overlap -> rewind at onece
int len = n + 1;
int c = -1, d = -1;
for(int i = 0; i < m; i++) {
if(d < cds[i].first) { // do the previous overlap
len += 2 * (d - c);
c = cds[i].first;
d = cds[i].second;
} else { // overlapping
d = max(d, cds[i].second);
}
}
len += 2 * (d - c);
cout << len << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include "bits/stdc++.h"
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m; cin >> n >> m;
vector<int> imos(n + 10,0);
for (int i = 0; i < m; i++) {
int c, d; cin >> c >> d;
if (c < d) {
imos[c]++;
imos[d]--;
}
}
for (int i = 0; i < n; i++) {
imos[i + 1] += imos[i];
}
int res = n + 1;
for (int i = 0; i < n; i++) {
if (imos[i]) res += 2;
}
cout << res << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int N, m;
cin >> N >> m;
vector<int> st(N + 2);
for (int i = 0, c, d; i < m; i++) {
cin >> c >> d;
st[c]++;
st[d]--;
}
int res = N + 1;
for (int i = 1; i <= N; i++) {
st[i] += st[i - 1];
if (st[i]) {
res += 2;
}
}
cout << res << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define llong long long
using namespace std;
signed main(){
int n,m; cin >> n >> m;
vector<pair<int, int> > b(m);
for(int i = 0; i < m; i++)cin >> b[i].first >> b[i].second;
sort(ALL(b));
vector<int> a(n+1,0);
for(auto e : b){
a[e.first] += 1;
a[e.second] += -1;
}
for(int i = 1; i < n+1; i++){
a[i] += a[i-1];
}
// for(auto e : a)cerr << e << " ";
// cerr << endl;
bool flag = false;
int cnt = 0;
int ans = n+1;
for(auto e : a){
if(flag && !e){
flag = false;
ans += 2*cnt;
cnt = 0;
// cerr << cnt << endl;
}
else if(e){
cnt++;
if(!flag)flag = true;
}
}
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
using lint = long long;
using ldouble = long double;
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> ps(m);
for (auto& p : ps) cin >> p.first >> p.second;
ps.emplace_back(n + 1, n + 1);
sort(ps.begin(), ps.end());
int ans = n + 1;
int l = 0, r = 0;
for (auto p : ps) {
if (r <= p.first) {
ans += (r - l) * 2;
l = r = p.first;
}
r = max(r, p.second);
}
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.BitSet;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int N = sc.nextInt();
int M = sc.nextInt();
BitSet bs = new BitSet(N + 1);
for (int i = 0; i < M; ++i) {
int C = sc.nextInt();
int D = sc.nextInt();
bs.set(C, D);
}
System.out.println(N + 1 + bs.cardinality() * 2);
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(long long i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int main(){
ll n,m; cin>>n>>m;
vector<ll> istwice(n+2, 0);
rep(i,m){
ll c,d; cin>>c>>d;
istwice[c]++;
istwice[d]--;
}
rep(i, n+1) istwice[i+1]+=istwice[i];
ll res=n+1;
rep(i, n+2){
if(istwice[i]>0) res+=2;
}
cout<<res<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
vector<int>v(N + 1);
while (M--) {
cin >> L >> R;
v[L]++;
v[R]--;
}
for (int i = 1; i <= N; i++) {
v[i] += v[i - 1];
}
int ans = N + 1;
for (int i = 1; i <= N; i++) {
if (v[i])ans += 2;
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define rep(i, n) for(int i = 0; i < n; i++)
#define repx(i, a, n) for(int i = a; i < n; i++)
#define loop while(1)
#define lli long long int
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <utility>
#include <set>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> cnt(n + 1, 0);
int l, r;
rep (i, m) {
cin >> l >> r;
repx(j, l, r) cnt[j] += 1;
}
int ans = 0;
rep (i, n + 1) {
if (cnt[i] > 0) ans += 3;
else ans += 1;
}
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
int L,n;
cin>>L>>n;
vi in(L);
rep(i,n){
int a,b;
cin>>a>>b;
in[a-1]++;
in[b-1]--;
}
rep(i,L-1)in[i+1]+=in[i];
int out=L+1;
rep(i,L)if(in[i])out+=2;
cout<<out<<endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
using namespace std;
int main(){
int n, m;
std::cin >> n >> m;
vector<bool> visit(n + 1, false);
for (int i = 0; i < m; i++) {
int l, r;
std::cin >> l >> r;
for (int j = l; j < r; j++) {
visit[j] = true;
}
}
int ans = n + 1;
for (int i = 0; i < visit.size(); i++) {
ans += 2*visit[i];
}
std::cout << ans << std::endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int N, m;
scanf("%d %d", &N, &m);
int s[1024] = {0};
for (int i = 0; i < m; i++){
int a, b;
scanf("%d %d", &a, &b);
s[a]++;
s[b]--;
}
int ans = 0;
for (int i = 0; i <= N;){
if (s[i] == 0){
ans++;
i++;
}
else {
int rui = 0;
int tmp = 0;
while (rui + s[i] != 0){
rui += s[i];
tmp++;
i++;
}
i++;
ans += 3 * tmp + 1;
}
}
printf("%d\n", ans);
return (0);
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#include<string>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007;
#define INF (1<<28)
#define int long long
#define double long double
#define floot10 cout<<fixed<<setprecision(10)
#define G 1
int l[2000];
signed main(){
int n,m;
cin>>n>>m;
lp(i,m){
int a,b;
cin>>a>>b;
l[b]--;
l[a]++;
}
int ans=0;
lp(i,n+1){
if(i!=0) l[i]+=l[i-1];
ans++;
if(l[i]!=0) ans+=2;
}
cout<<ans<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}
int N,M;
int lef[1111];
int dp[1111];
signed main(){
cin>>N>>M;
N++;
fill_n(lef,1111,1<<30);
rep(i,M){
int c,d;
cin>>c>>d;
c--;d--;
chmin(lef[d],c);
}
fill_n(dp,1111,1<<30);
dp[0]=0;
reps(i,1,N+1){
int most_lef=i;
for(int j=i-1;j>=0;j--){
chmin(most_lef,lef[j+1]);
chmin(dp[i],dp[j]+(i-j)+(i-most_lef)*2);
}
}
cout<<dp[N]<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )
#define All(v) v.begin(), v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;
int N, m;
vector<Pii> d;
unordered_map<int, int> dp[1001][1001];
int dfs(int pre, int now, int n) {
if ( n == m ) {
return (now-pre + (N+1)-pre);
}
if ( dp[pre][now][n] > 0 ) return dp[pre][now][n];
int ret = dfs(min(pre, d[n].sc), d[n].fr, n+1) + (d[n].fr-now);
ret = min(ret, dfs(d[n].sc, d[n].fr, n+1) + (now-pre + d[n].fr-pre));
return dp[pre][now][n] = ret;
}
signed main() {
cin >> N >> m;
Rep(i, m) {
int c, b;
cin >> c >> b;
d.pb(Pii(b, c));
}
sort(All(d));
if ( m == 0 ) cout << N+1 << endl;
else cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
int main(void) {
cin.tie(0); ios::sync_with_stdio(false);
int N,m; cin >> N >> m;
vector<int> c(m),d(m);
vector<int> flag(N+1);
for(int i = 0; i < m;i++){
cin >> c[i] >> d[i];
for(int j = c[i]; j < d[i];j++){
flag[j] = true;
}
}
int cnt = accumulate(flag.begin(),flag.end(),0);
ll ans = cnt*3 + (N+1-cnt);
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int N,m;
cin >> N >> m;
vector<int> is(N+1,1);
for(int i=0; i<m; i++){ // 500
int c,d;
cin >> c >> d;
if(c < d)
for(int j=c; j<d; j++){ // 1000
is[j] = 3;
}
}
int ans = 0;
for(int i=0; i<=N; i++){
ans += is[i];
}
cout << ans << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.*;
import java.util.*;
public class Main{
int N;
int M;
public void solve(){
N = nextInt();
M = nextInt();
int[] memo = new int[N + 1];
for(int i = 0; i < M; i++){
memo[nextInt()]++;
memo[nextInt()]--;
}
int ans = 0;
int cnt = 0;
for(int i = 0; i <= N; i++){
cnt += memo[i];
ans++;
if(cnt > 0) ans+=2;
}
out.println(ans);
}
private static PrintWriter out;
public static void main(String[] args){
out = new PrintWriter(System.out);
new Main().solve();
out.flush();
}
public static int nextInt(){
int num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(;i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10 + (c - '0');
}
return minus ? -num : num;
}
public static long nextLong(){
long num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(;i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10l + (c - '0');
}
return minus ? -num : num;
}
public static String next(){
int c;
while(!isAlNum(c = read())){}
StringBuilder build = new StringBuilder();
build.append((char)c);
while(isAlNum(c = read())){
build.append((char)c);
}
return build.toString();
}
private static byte[] inputBuffer = new byte[1024];
private static int bufferLength = 0;
private static int bufferIndex = 0;
private static int read(){
if(bufferLength < 0) throw new RuntimeException();
if(bufferIndex >= bufferLength){
try{
bufferLength = System.in.read(inputBuffer);
bufferIndex = 0;
}catch(IOException e){
throw new RuntimeException(e);
}
if(bufferLength <= 0) return (bufferLength = -1);
}
return inputBuffer[bufferIndex++];
}
private static boolean isAlNum(int c){
return '!' <= c && c <= '~';
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(u) begin(u),end(u)
#define PB push_back
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)
typedef long long int ll;
const int INF = (1<<30) - 1;
const double EPS = 1e-9;
const int MOD = 1000000007;
int N, M;
//g++ -std=c++0x -msse4.2 -O3
//#include <bits/stdc++.h>
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//cout.precision(16);
//cout.setf(ios::fixed);
cin >> N >> M;
vector<pair<int, int>> v;
REP(i, M){
int c, d; cin >> c >> d;
v.PB(make_pair(c, d));
}
sort(v.begin(), v.end());
int ans = N + 1;
int i = 0;
while(i<M){
int s = i;
int m = v[i].second;
while(i+1<M && v[i+1].first <= m){
i++;
m = max(m, v[i].second);
}
ans += (m - v[s].first) * 2;
i++;
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
namespace {
template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
if (vs.empty()) return os << "[]";
os << "[" << vs[0];
for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
return os << "]";
}
int N, M;
vector< pair<int, int> > P;
void input() {
cin >> N >> M;
P.resize(M);
for (int i = 0; i < M; i++) {
cin >> P[i].first >> P[i].second;
}
}
void solve() {
if (M == 0) {
cout << N + 1 << endl;
return;
}
sort(P.begin(), P.end());
int ans = N + 1;
int l = P[0].first, r = P[0].second;
int k = 1;
while (true) {
int a = P[k].first, b = P[k].second;
if (l <= b && a <= r) {
l = min(l, a);
r = max(r, b);
k++;
} else {
ans += 2 * (r - l);
l = a;
r = b;
k++;
}
if (k == M) {
ans += 2 * (r - l);
break;
}
}
cout << ans << endl;
}
}
int main() {
input(); solve();
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
int MOD = 1000000007;
int main() {
int N, m; cin >> N >> m;
vector<int> c(m), d(m);
for (int j = 0; j < m; j++) cin >> c[j] >> d[j];
vector<int> a(N + 2), b(N + 2);
for (int j = 0; j < m; j++) {
a[c[j]]++;
b[d[j]]++;
}
int l = -1, x = 0, ans = 0;
for (int i = 0; i <= N + 1; i++) {
ans++;
if (a[i] > 0) {
if (l == -1) l = i;
x += a[i];
}
x -= b[i];
if (x == 0 && l != -1) {
ans += (i - l) * 2;
l = -1;
}
}
cout << ans - 1 << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
while True:
n,m = LI()
if m == 0:
rr.append(n+1)
break
a = [LI() for _ in range(m)]
s = set()
for c,d in a:
s |= set(list(range(c,d)))
rr.append(n+1+len(s)*2)
break
return '\n'.join(map(str,rr))
print(main())
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;
bool memo[1010];
int main(void){
int n,m;
cin >> n >> m;
rep(i,m){
int c,d;
cin >> c >> d;
if(c<d) range(j,c,d)memo[j]=true;
}
int ans=n+1;
rep(i,n) ans+=2*memo[i];
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | /**
In the name of Allah, the Most Gracious, the Most Merciful.
**/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
pair < int, int > ara[1005];
vector < pair < int, int > > ve;
int n, m, c, d, ans;
int main() {
#ifdef _OFFLINE_
freopen("in.txt", "r", stdin);
#endif /// _OFFLINE_
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> ara[i].first >> ara[i].second;
}
sort(ara, ara+m);
// for(int i=0; i<m; i++) {
// cerr << ara[i].first << " " << ara[i].second << "\n";
// }
if( !m ) {
cout << n + 1 << "\n";
exit(0);
}
int l = 0, r = 0;
for(int i=0; i<m; i++) {
if( r >= ara[i].first ) {
r = max(r, ara[i].second);
}
else if( r < ara[i].first ) {
if( l && r ) {
ve.push_back({l, r});
}
l = ara[i].first;
r = ara[i].second;
}
if( i == m-1 ) {
if( l && r ) {
ve.push_back({l, r});
}
}
}
// cerr << endl;
//
// for(int i=0; i<ve.size(); i++) {
// cerr << ve[i].first << " " << ve[i].second << "\n";
// }
int len = ve.size(), pre = 0;
for(int i=0; i<len; i++) {
ans += ve[i].first - pre;
ans += (ve[i].second - ve[i].first) * 3;
pre = ve[i].second;
}
ans += (n+1) - pre;
cout << ans << "\n";
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>
using namespace std;
struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
typedef long long ll;
int const inf = 1<<29;
int main() {
int N, m; cin >> N >> m;
int v[1111] = {};
rep(i, m) {
int c, d; cin >> c >> d;
REP(x, c, d) {
v[x] ++;
}
}
int ans = N+1;
rep(i, 1111) {
if(v[i]) {
ans += 2;
}
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | //hikaku site donnyoku ni otosu ippanntekina teku
#include <iostream>
using namespace std;
int n, m;
int c[500], d[500];
int imos[1002];
int main() {
cin >> n >> m;
int i;
for (i = 0; i < n + 2; i++) imos[i] = 0;
for (i = 0; i < m; i++) {
cin >> c[i] >> d[i];
imos[c[i]]++;
imos[d[i]]--;
}
for (i = 1; i < n + 2; i++) imos[i] += imos[i - 1];
int len = 0;
int ans = n + 1;
for (i = 0; i < n + 2; i++) {
if (imos[i]) {
len++;
}
else {
ans += 2 * len;
len = 0;
}
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <stack>
using namespace std;
int n,m;
int a[1050];
int main()
{
for (int i = 0; i < 1050; i++)
a[i]=-1;
cin>>n>>m;
for (int i = 0; i < m; i++){
int c,d;
cin>>c>>d;
a[c]=std::max(d,a[c]);
}
long long dist=0;
for (int i = 0; i <= n; i++,dist++){
//cout<<i<<' '<<dist<<endl;
if(a[i]==-1)continue;
int from=i;
int to = a[i];
for (int j = i+1; j <= n; j++){
if(a[j]>to) to = a[j];
else if(j==to){
dist+=(to-from)*3;
i=to;
break;
}
}
}
cout<<dist<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
int main() {
int n,m;
cin>>n>>m;
vector<int> rev(n+2);
REP(i,m) {
int c,d;
cin>>c>>d;
for(int j=c;j<d;++j) rev[j]=1;
}
cout<<(n+1+count(begin(rev),end(rev),1)*2)<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
for(int i=0;i<(int)(s.size());++i) {
cout << s[i];
if(i == (int)(s.size())-1) cout << endl;
else cout << " ";
}
}
int main() {
cin.tie(0);
cout << fixed << setprecision(10);
int n, m; cin >> n >> m;
if(m == 0) {
cout << n + 1 << endl;
return 0;
}
vector<pair<int, int>> p(m);
for(int i=0;i<m;++i) {
cin >> p[i].first >> p[i].second;
}
sort(p.begin(), p.end());
vector<pair<int, int>> v;
int l = p[0].first, r = p[0].second;
for(int i=1;i<m;++i) {
if(p[i].first <= r) {
l = min(l, p[i].first);
r = max(r, p[i].second);
} else {
v.push_back({l, r});
l = p[i].first, r = p[i].second;
}
}
v.push_back({l, r});
int ans = n + 1;
for(int i=0;i<(int)(v.size());++i) {
ans += 2 * (v[i].second - v[i].first);
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)
const int N = 1111;
int main(){
int n,k;
cin>>n>>k;
int masu[N]={0};
rep(i,k){
int a,b;
cin>>a>>b;
reps(j,a,b){
masu[j]=1;
}
}
int sum = 0;
rep(i,N){
sum += masu[i];
}
printf("%d\n",n+sum*2+1);
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps=1e-8;
int main() {
int n, m; cin >> n >> m;
int chk[1001] = {};
rep(i, m) {
int s, t; cin >> s >> t; chk[s]++; chk[t]--;
}
Rep(i, 1, n + 1) {
chk[i] += chk[i - 1];
}
int cnt = 0;
rep(i, n + 1) {
if (chk[i]) {
cnt += 3;
}
else {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | N,m = map(int, input().split())
again = set()
for i in range(m):
a,b = map(int, input().split())
for k in range(a,b):
again.add(k)
r = N+1 + 2*(len(again))
print(r)
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = I()
return l
def LIR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = LI()
return l
def SR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = S()
return l
def LSR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = LS()
return l
sys.setrecursionlimit(1000000)
mod = 1000000007
#A
def A():
s = input()
c = I()
a = eval(s)
b = int(s[0])
n = len(s)
if n == 1:
if int(s[0]) == c:
print("U")
else:
print("I")
quit()
key = 0 if s[1] == "+" else 1
for i in range(2,n):
if not i%2:
if key:
b *= int(s[i])
else:
b += int(s[i])
else:
key = 0 if s[i] == "+" else 1
if a == b:
if a == c:
print("U")
else:
print("I")
elif a == c:
print("M")
elif b == c:
print("L")
else:
print("I")
return
#B
def B():
n,m = LI()
t = [0 for i in range(2*n+3)]
for i in range(m):
a,b = LI()
t[2*a] += 1
t[2*b+1] -= 1
for i in range(2*n+1):
t[i+1] += t[i]
ans = n+1
s = 0
key = 1
for i in t:
if i > 0:
if key:
key = 0
s += 1
ans += 1
else:
key = 1
ans -= s
print(ans)
return
#C
def C():
return
#D
def D():
return
#E
def E():
return
#F
def F():
return
#G
def G():
return
#H
def H():
return
#I
def I_():
return
#J
def J():
return
#Solve
if __name__ == "__main__":
B()
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef pair<int,int> P;
vector<P> vc;
int main(){
int N,M;
cin>>N>>M;
rep(i,M){
int a,b;
cin>>a>>b;
vc.pb(P(a,b));
}
sort(all(vc));
if(M==0){
cout<<N+1<<endl;
return 0;
}
int l=vc[0].fs,r=vc[0].sc;
int ans=N+1;
rep(i,M){
if(r<vc[i].fs){
ans+=2*(r-l);
l=vc[i].fs,r=vc[i].sc;
}else{
chmax(r,vc[i].sc);
}
}
ans+=2*(r-l);
cout<<ans<<endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=1e3;
const int inf=1e6;
int rq[N+2],mn,vis[N+2];
vector<int>adj[N+2];
void dfs(int node)
{
mn=min(mn,node);
if(vis[node])return ;
vis[node]=1;
for(auto x:adj[node])
dfs(x);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v;cin>>u>>v;
adj[v].push_back(u);
}
for(int i=n;i>=1;i--)
{
mn=inf;
dfs(i);rq[i]=mn;
}
int ans=n+1;
for(int i=n;i>=1;i--)
{
mn=rq[i];
for(int j=i;j>=max(mn,1);j--)
{
mn=min(mn,rq[j]);
}
ans+=(i-mn)*2;
i=mn;
}
cout<<ans<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
#define N 1001
using namespace std;
int main(){
int n,m,c,d,l,r,flag,sum;
int V[N]={0};
cin >> n >> m;
for(int i=0;i<m;i++) cin >> c >> d,V[c]=max(V[c],d);
l=r=flag=sum=0;
for(int i=0;i<=n;i++){
if(flag&&V[i]!=0) r=max(r,V[i]);
if(V[i]!=0&&!flag) l=i,r=V[i],flag=1;
if(flag&&r==i) sum+=3*(r-l),flag=0;
if(!flag) sum++;
}
cout << sum << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,x,y) for(int i=(x);i<y;i++)
struct S {
int c, d;
};
int main()
{
int N, m;
cin >> N >> m;
vector<S> data(m);
rep(i, 0, m) {
cin >> data[i].c >> data[i].d;
}
while (true) {
bool flag = false;
rep(i, 0, m) {
if (data[i].c == 0 && data[i].d == 0) {
continue;
}
rep(j, i + 1, m) {
if (data[j].c == 0 && data[j].d == 0) {
continue;
}
if (data[i].d > data[j].c&&data[i].c < data[j].d) {
data[i].c = min(data[i].c, data[j].c);
data[i].d = max(data[i].d, data[j].d);
data[j].c = 0;
data[j].d = 0;
flag = true;
}
}
}
if (flag == false) {
break;
}
}
int ans = N + 1;
rep(i, 0, m) {
ans += (data[i].d - data[i].c) * 2;
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int N, m;
cin >> N >> m;
vector<pair<int, int>> cd(m);
int c, d;
for(int i = 0; i < m; i++){
cin >> c >> d;
cd[i] = make_pair(c, d);
}
sort(cd.begin(), cd.end());
int ans = N + 1;
int start = 0;
int end = 0;
for(int i = 0; i < m; i++){
auto temp = cd[i];
if(temp.first < end){
end = max(end, temp.second);
}
else{
ans += (end - start) * 2;
start = temp.first;
end = temp.second;
}
}
ans += (end - start) * 2;
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;
int main(){
int L, n;
while(cin >> L >> n) {
vector<pair<int, int>> vp(n);
REP(i, n) {
int a, b;
cin >> a >> b;
vp[i] = make_pair(a, b);
}
sort(vp.begin(), vp.end());
long long ans = L + 1;
if(n == 0) {
cout << ans << endl;
continue;
}
int begin = vp[0].first, end = vp[0].second;
for(int i = 1; i < n; i++) {
if(vp[i].first <= end) {
end = max(end, vp[i].second);
} else {
ans += 2 * (end - begin);
begin = vp[i].first;
end = vp[i].second;
}
}
ans += 2 * (end - begin);
cout << ans << endl;
}
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
class Union_Find_Tree{
public:
vi p,r,s;
Union_Find_Tree(int n){
p=r=vi(n);
s=vi(n,1);
for(int i=0;i<n;i++) p[i]=i;
}
int Par(int x){
if(p[x]==x) return x;
return p[x]=Par(p[x]);
}
int Size(int x){return s[Par(x)];}
bool Unite(int x,int y){
x=Par(x);
y=Par(y);
if(x==y) return 0;
if(r[x]<r[y]){
p[x]=y;
s[y]+=s[x];
}
else{
p[y]=x;
s[x]+=s[y];
if(r[x]==r[y]) r[x]++;
}
return 1;
}
bool Same(int x,int y){return Par(x)==Par(y);}
};
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
vp a(m);
for(int i=0;i<m;i++){
cin>>a[i].first>>a[i].second;
}
while(1){
bool B=1;
for(int i=0;i<m&&B;i++) for(int j=i+1;j<m&&B;j++) {
int l=a[i].first,r=a[i].second,L=a[j].first,R=a[j].second;
if(max(l,L)<min(r,R)){
a.erase(a.begin()+j);
a.erase(a.begin()+i);
a.push_back({min(l,L),max(r,R)});
m--;
B=0;
}
}
if(B) break;
}
int res=n+1;
for(int i=0;i<m;i++){
res+=2*(a[i].second-a[i].first);
}
cout<<res<<endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import static java.lang.Math.*;
public class Main {
class Pair implements Comparable<Pair> {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
return this.b - p.b;
}
}
void run() {
// input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
if (m == 0) {
System.out.println(n + 1);
return;
}
Pair[] pair = new Pair[m];
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
pair[i] = new Pair(a, b);
}
Arrays.sort(pair);
// debug
// for (int i = 0; i < m; i++) {
// System.out.println(pair[i].a + " " + pair[i].b);
// }
// System.out.println();
Map<Integer, Integer> res = new TreeMap<Integer, Integer>();
int key = pair[m - 1].b;
int val = pair[m - 1].a;
for (int i = m - 2; i >= 0; i--) {
if (val <= pair[i].b) {
val = min(val, pair[i].a);
key = max(key, pair[i].b);
} else {
// ??????????????????
if (res.containsKey(key)) {
val = min(val, res.get(key));
}
res.put(key, val);
key = pair[i].b;
val = pair[i].a;
}
}
res.put(key, val);
// debug
// for (Integer k : res.keySet()) {
// System.out.println(k + " " + res.get(k));
// }
int count = 0;
int pos = 0;
for (Integer k : res.keySet()) {
int back = res.get(k);
count += ((k - pos) + (k - back) * 2);
pos = k;
}
count += (n - pos + 1);
System.out.println(count);
}
void run1() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Pair[] pair = new Pair[m];
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
pair[i] = new Pair(a, b);
}
Arrays.sort(pair);
int[] mark = new int[n];
for (int i = 0; i < m; i++) {
int s = pair[i].b - 1;
int e = pair[i].a - 1;
for (int j = e; j <= s; j++) {
mark[j] = 1;
}
}
int count = 0;
int s = 0;
for (int i = 0; i < n; i++) {
if (mark[i] == 0) {
count += s;
} else {
s++;
}
}
}
public static void main(String[] args) {
new Main().run();
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
os<<'[';
rep(i,a.size()) os<<(i?" ":"")<<a[i];
return os<<']';
}
int main()
{
for(int n,m;cin>>n>>m && n|m;){
vi sum(n+2);
rep(i,m){
int a,b; cin>>a>>b;
sum[a]++,sum[b]--;
}
rep(i,n+1) sum[i+1]+=sum[i];
int res=0;
rep(i,n+1) res+=(sum[i]?3:1);
cout<<res<<endl;
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<cmath>
#include<functional>
#include<algorithm>
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
struct Range{
int s,e;
Range(){}
Range(int s,int e):s(s),e(e){}
bool operator<(const Range &r)const{return e<r.e;}
};
vector<Range> merge_range(vector<Range> r){
vector<Range> res;
sort(r.begin(),r.end());
for(int i=0;i<(int)r.size();i++){
int mi=r[i].s;
while(!res.empty()&&r[i].s<=res.back().e){
mi=res.back().s;
res.pop_back();
}
res.push_back(Range(min(mi,r[i].s),r[i].e));
}
return res;
}
int main(){
int n,m;
Range r[500];
cin>>n>>m;
rep(i,m)cin>>r[i].s>>r[i].e;
int ans=n+1;
vector<Range> vr(merge_range(vector<Range>(r,r+m)));
rep(i,vr.size()){
ans+=(vr[i].e-vr[i].s)*2;
}
cout<<ans<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool arr[1010];
int main(){
int N,M,c,d;
cin >> N >> M;
for(int i = 0 ; i < M ; i++){
cin >> c >> d;
for(int j = c ; j < d ; j++){
arr[j] = true;
}
}
cout << N+2*count(arr,arr+N,1)+1 << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include "bits/stdc++.h"
using namespace std;
int rui[1010];
int main()
{
int n,m;
int ans=0;
cin>>n>>m;
ans+=n+1;
fill(rui,rui+1010,0);
for(int i=0;i<m;i++)
{
int c,d;
cin>>c>>d;
if(c<d)
{
rui[c]++;
rui[d]--;
}
}
for(int i=1;i<1010;i++)
rui[i]=rui[i]+rui[i-1];
//累積和が1以上の区間を見る
int i=0;
while(i<n+1)
{
if(rui[i]>0)
{
int l=i;
while(rui[i]>0)
i++;
int r=i;
ans+=(r-l)*2;
}
i++;
}
cout<<ans<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; cin >> x;
#define int2(x, y) Int(x); Int(y);
#define int3(x, y, z) Int(x); int2(y, z);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)
//int dxy[5] = {0, 1, 0, -1, 0};
// cmd
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int2(n, m);
vi yui(n+1, 0);
rep(i, m) {
int2(a, b);
for (int j = a; j < b; j++) {
yui[j] = 1;
}
}
int ans = n+1;
rep(i, yui.size()) {
ans += 2 * yui[i];
}
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;
int main(){
ll n,m;
cin>>n>>m;
if(m==0){cout<<n+1<<endl; return 0;}
vector<pll> a(m);
for(auto &I:a){cin>>I.F>>I.S;}
sort(a.begin(),a.end());
vector<pll> A;
A.push_back(a[0]);
for(int i=1;i<m;i++){
if(a[i].S>A.back().S){A.push_back(a[i]);}
}
vector<vector<ll>> dp(A.size()+1,vector<ll>(A.size()+1,E));
dp[0][0]=0;
for(int i=1;i<=A.size();i++){
for(int t=0;t<i;t++){
dp[i][t]=dp[i-1][t];
}
for(int t=0;t<i;t++){
dp[i][i]=min(dp[i][i],dp[i-1][t]+(A[i-1].S-A[t].F)*2);
}
}
cout<<dp[A.size()][A.size()]+n+1<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#define PB push_back
using namespace std;
int main(void)
{
int vis[1024];
int n,m;
cin >> n >> m;
for(int i=0;i<n+5;++i) vis[i]=i;
for(int i=0;i<m;++i){
int left,right;
cin >> left >> right;
if(vis[left]<right)
vis[left]=right;
}
for(int i=0;i<n+5;++i){
if(vis[i+1]<vis[i]) vis[i+1]=vis[i];
}
int answer=n+1;
for(int pos=0;pos<=n;++pos){
while(vis[pos]>pos){
answer+=(vis[pos]-pos)*2;
pos=vis[pos];
}
}
cout << answer << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Shopping solver = new Shopping();
solver.solve(1, in, out);
out.close();
}
static class Shopping {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
Set<Integer> se = new HashSet<>();
for (int i = 0; i < M; i++) {
int c = in.nextInt() - 1;
int d = in.nextInt() - 1;
for (int j = c; j < d; j++) {
se.add(j);
}
}
int ans = 0;
for (int i = 0; i <= N; i++) {
if (se.contains(i)) {
ans += 2;
}
ans++;
}
out.println(ans);
}
}
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<utility>
//#include<>
using namespace std;
#define rep(i,j) for(i=0;i<j;(i)++)
int main()
{
int N,m;
cin >>N>>m;
int i;
pair<int,int> restricts[500],restricts_d_c[500];//(ci,di)と(di,ci)
rep(i,m){
cin>>restricts[i].first>>restricts[i].second;
restricts_d_c[i].first=restricts[i].second;
restricts_d_c[i].second=restricts[i].first;
}
sort(restricts,restricts+m);
sort(restricts_d_c,restricts_d_c+m);
int line[1002]={};
line[0]=1;
i=0;
int start,end,j;
while(i<m){
start=restricts[i].first;
end=restricts[i].second;
i++;
for(;i<m;i++)
if(restricts[i].first<=end && end<restricts[i].second)
end=restricts[i].second;
else if(end<restricts[i].first)
break;
line[start]+=1;
line[start+1]+=1;
line[end]-=1;
line[end+1]-=1;
}
int ans=line[0];
rep(i,N){
line[i+1]+=line[i];
ans+=line[i+1];
//cout<<line[i+1]<<" ";
}
cout<<ans<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
const ll MOD=1e9+7;
const ll INF=1e18;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int main(){
int n, m; cin>>n>>m;
vector<int> shops(n+10, 0);
for(int i=0; i<m; i++){
int c, d; cin>>c>>d;
for(int j=c; j<d; j++){
shops[j]++;
}
}
int ans=n+1;
for(int i=1; i<n; i++){
int length=0;
while(i<n && shops[i]>0){
length++;
i++;
}
ans+=length*2;
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
int n, m;
int l[505], r[505];
int dif[1005], sum[1005];
int main(void)
{
cin >> n >> m;
for(int i = 1; i <= m; i++) cin >> l[i] >> r[i];
for(int i = 1; i <= m; i++){
dif[l[i]]++, dif[r[i]]--;
}
for(int i = 1; i <= n+1; i++) sum[i] = sum[i-1] + dif[i];
int ans = 0, cnt = 0;
for(int i = 1; i <= n+1; i++){
if(sum[i] > 0) cnt++;
else{
ans += cnt;
cnt = 0;
}
}
ans *= 2, ans += n+1;
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int N, m;
cin >> N >> m;
vector<bool> select(m, true);
vector<pair<int, int>> cd(m);
for (int i = 0; i < m; ++i)
cin >> cd[i].first >> cd[i].second;
sort(cd.begin(), cd.end());
for (int i = 0; i < m; ++i) {
if (cd[i].second <= cd[i].first)
select[i] = false;
}
for (int i = 0; i < m; ++i) {
if (!select[i])
continue;
bool isIN = false;
for (int j = 0; j < m; ++j) {
if (i == j || !select[j])
continue;
if (cd[j].first <= cd[i].first && cd[i].second <= cd[j].second) {
isIN = true;
break;
}
}
if (isIN)
select[i] = false;
}
for (int i = 0; i < m; ++i) {
if (!select[i])
continue;
for (int j = i + 1; j < m; ++j) {
if (!select[j])
continue;
if (cd[i].first <= cd[j].first && cd[j].first < cd[i].second) {
select[j] = false;
cd[i].second = cd[j].second;
}
}
}
int ans = N + 1;
for (int i = 0; i < m; ++i)
if (select[i])
ans += 2 * (cd[i].second - cd[i].first);
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
// editorial解
// 重なる区間を1発で済ませてしまえば良いことが自明なので
// いもしてホイ
int n, m;
cin >> n >> m;
int imos[1005] = {};
for(int i = 0; i < m; i++){
int c, d;
cin >> c >> d;
imos[c]++, imos[d]--;
}
int ans = n+1;
for(int i = 1; i <= n; i++){
imos[i] += imos[i-1];
if(imos[i]) ans += 2;
}
cout << ans << endl;
return 0;
// 自分の回答(AC)
// int n, m;
// cin >> n >> m;
// if(m == 0){
// cout << n+1 << endl;
// return 0;
// }
// vector<int> dp(m, 1<<30);
// vector<pair<int,int>> vp;
// for(int i = 0; i < m; i++){
// int c, d;
// cin >> c >> d;
// vp.push_back({d, c});
// }
// sort(vp.begin(), vp.end());
// for(int i = 0; i < m; i++){
// int to = vp[i].first, from = vp[i].second;
// for(int j = i; j >= 0; j--){
// from = min(from, vp[j].second);
// dp[i] = min(dp[i], (j==0 ? 0 : dp[j-1])+(to-from)*2);
// }
// }
// cout << dp[m-1]+n+1 << endl;
// return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}
ll flg[1010];
int main(){
ll N, M;
cin >> N >> M;
rep(i, M){
ll a, b;
cin >> a >> b;
for(ll i = a; i < b; i++)flg[i] = 1;
}
ll ans = N + 1;
for(ll i = 1; i <= N; i++)ans += 2 * flg[i];
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]){
int n, m;
cin >> n >> m;
int c[m], d[m];
int road[n + 1];
memset(road, 0, sizeof(road));
for (int i = 0; i < m; i++) {
cin >> c[i] >> d[i];
for (int j = c[i]; j < d[i]; j++) {
road[j] = 1;
}
}
int ans = 0;
for (int i = 0; i <= n; i++) {
if(road[i] == 1)ans++;
// std::cout << road[i];
// if(i == n)std::cout << std::endl;
// else std::cout << " ";
}
std::cout << ans*2 + n + 1 << std::endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dfs(int n, vector<vector<int> >& adj){
if((int)adj[n].size() == 0) return n;
int ret=0;
for(int i=0; i<(int)adj[n].size(); i++){
ret = max(ret, dfs(adj[n][i], adj));
}
adj[n].clear();
return ret;
}
int main(){
int n,m;
cin >> n >> m;
vector<vector<int> > rest(n+1);
for(int i=0; i<m; i++){
int c,d;
cin >> c >> d;
rest[c].push_back(d);
}
vector<int> c,d;
for(int i=1; i<=n; i++){
int top = dfs(i, rest);
if(top != i){
c.push_back(i);
d.push_back(top);
}
}
c.push_back(n+1);
d.push_back(n+1);
int ans = 0;
int s=c[0], e=d[0];
for(int i=1; i<(int)c.size(); i++){
if(c[i] <= e){
e = max(e, d[i]);
}else{
ans += e-s;
s = c[i];
e = d[i];
}
}
ans = n +1 +ans*2;
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // 3.3 ""
// https://onlinejudge.u-aizu.ac.jp/problems/2298
// 所要時間:
// 着席位置: 左前
// 学んだこと:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
typedef long long ll;
int main(void){
int n, m;
cin >> n >> m;
vector<pair<int, int>> make;
for (int i = 0; i < m; i++) {
int c, d;
cin >> c >> d;
make.emplace_back(c, 1);
make.emplace_back(d, -1);
}
sort(make.begin(), make.end());
int ans = n + 1, idx = 0, until;
for (int i = 0; i < 2*m; i++) {
if (make[i].second == 1) {
if (idx == 0) until = make[i].first;
idx++;
}
else idx--;
if (idx == 0) {
ans += 2 * (make[i].first - until);
}
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#pragma warning(disable : 4996)
int n, m, c, d, imos[1111];
int main()
{
scanf("%d", &n);
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &c);
scanf("%d", &d);
imos[c--]++;
imos[d--]--;
}
int ret = n + 1, sum = 0;
for (int i = 0; i < n; i++)
{
sum += imos[i];
if (sum > 0) ret += 2;
}
printf("%d\n", ret);
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1347
// ICPC: アジア地区予選2014C
// Shopping
/*
Problem.
スタート地点:0, ゴール地点: N+1であるとして、
地点1~Nに存在するN個あるノードを全て訪れるときの最短距離を求めよ、
ただし、M個の以下の内容の制約が与えられる。
- 制約(C, D):ノードCの前にノードDを訪れなければならない。
Point.
preceder[i]:iの最も後ろの先行ノード
- preceder[i]より前のiの先行ノードについては、
preceder[i]を訪れる際にコスト0で訪れることが可能であるため、
iの先行ノードはpreceder[i]のみ考慮すれば良い。
dp[j][i]: 最大jまで経由した時の、0~iまでを巡る最短距離.
- dp[i][i]は、0~iまでの全てかつ0~iのみのノードを巡る最短距離であり、
dp[i][i] < INF なら、0~iまでのノードで完結させることが可能と言える。
この時、i+1については、明らかにdp[i][i]の値を元に最短距離が求まる。
以上より、値の更新は
if(dp[i-1][i-1] < INF)dp[j][i] = dp[i-1][i-1] + j-(i-1) + j-i; //i-1 -> j -> i
else dp[j][i] = dp[j][i-1]+1; //素直に前のノードから1つ移動
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>
using namespace std;
using ll = long long;
using pint = pair<int, int>;
#define INF 1e9
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int ti = clock();
// start-----------------------------------------------
int n, m; cin >> n >> m;
vector<vector<int>> dp(n+2, vector<int>(n+2, INF));
vector<int> preceder(n+2);
for(int i = 0; i < n+2; i++) preceder[i] = i;
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b;
preceder[a] = max(preceder[a], b);
}
dp[0][0] = 0;
for(int i = 1; i <= n+1; i++){
for(int j = preceder[i]; j <= n+1; j++){
if(dp[i-1][i-1] < INF) dp[j][i] = dp[i-1][i-1] + j-(i-1) + j-i;
else dp[j][i] = dp[j][i-1] + 1;
}
}
cout << dp[n+1][n+1] << endl;
// end-----------------------------------------------
// cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int N, m;
cin >> N >> m;
bool th[1001] = {false};
for(int i = 0; i < m; i++) {
int c, d; cin >> c >> d;
for(int j = c; j < d; j++) th[j] = true;
}
int ans = N + 1;
for(int i = 0; i < N; i++) {
if(th[i]) ans += 2;
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;
int main(){
int N,M,left,right,ans = 0;
scanf("%d %d",&N,&M);
bool* table = new bool[N+1];
for(int i = 0; i <= N; i++)table[i] = false;
for(int i = 0; i < M; i++){
scanf("%d %d",&left,&right);
for(int k = left;k < right;k++)table[k] = true;
}
for(int i = 0; i <= N; i++){
if(table[i])ans += 3;
else{
ans += 1;
}
}
printf("%d\n",ans);
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
/**
* Shopping
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
String[] words;
line = br.readLine();
int N, m;
N = parseInt(line.substring(0, line.indexOf(' ')));
m = parseInt(line.substring(line.indexOf(' ') + 1));
if (m == 0) {
System.out.println(N + 1);
return;
}
int[] shops = new int[N + 1];
for (int i = 0; i < m; i++) {
line = br.readLine();
int c, d;
c = parseInt(line.substring(0, line.indexOf(' ')));
d = parseInt(line.substring(line.indexOf(' ') + 1));
shops[c]++;
shops[d]--;
}
for (int i = 1; i < shops.length; i++) {
shops[i] += shops[i - 1];
}
int free = 0;
for (int i = 0; i < shops.length; i++) {
if (shops[i] == 0) free++;
}
System.out.println(free + (N + 1 - free) * 3);
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define im imag()
#define re real()
#define r(I, N) for(int I=0;I<N;I++)
#define f(I, A, B) for(int I=A;I<B;I++)
#define fd(I, A, B) for(int I=A;I>=B;I--)
#define ite(x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)
#define sz(x) ((int)(x).size())
#define clr(x) memset(x, 0, sizeof(x))
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef complex<ld> Point;
typedef Point Vector;
const ld pi = acos(-1.0);
const ld eps = 1e-12;
int n, m;
int pos1[1234], pos2[1234];
int f[567], t[567];
int main()
{
cin >> n >> m;
r(i, m)
{
int a, b;
cin >> a >> b;
pos1[a]++, pos2[b]++;
f[i] = a, t[i] = b;
}
int ans = 0, cnt = 0;
int mn = -1;
f(i, 1, n + 1)
{
if(pos2[i])
{
cnt-=pos2[i];
if(cnt == 0)
{
ans += 2 * (i - mn);
mn = -1;
}
}
if(pos1[i])
{
cnt+=pos1[i];
if(mn == -1) mn = i;
}
ans++;
}
cout << ans + 1 << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void){
bool a[1001]={};
int N,m,cnt=0;
cin>>N>>m;
while(m--){
int c,d;
cin>>c>>d;//d???c?????????????????????
for(int i=c;i<d;++i)a[i]=true;//d??????c??????????????????
}
for(int i=0;i<=N;++i)if(a[i])cnt++;//?????£?????????????????°??????
cout<<N+1+cnt*2<<endl;//N+1?????\?????£???????????£?????§????????¢???count*2???????????????
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n,m,x=0,q,w;
cin>>n>>m;
vector<int>a[n];
r(i,m)cin>>q>>w,
a[q-1].push_back(x),
a[w-1].push_back(x++);
set<int>s;int p=0,st,ans=0;
r(i,n){
if(a[i].size()){
if(!p)p++,st=i;
r(j,a[i].size())
if(s.count(a[i][j]))s.erase(a[i][j]);
else s.insert(a[i][j]);
}
ans++;
if(s.size()==0&&p){
p=0;
ans+=(i-st)*2;
}
}
cout<<ans+1<<endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
typedef long long ll;
using namespace std;
typedef vector<int> vi;
int N,m;
int c[505];
int d[505];
bool triple[1005];
int main()
{
memset(triple,false,sizeof(triple));
ll ans=0;
cin >> N >> m;
for(int i=0;i<m;i++)
{
cin >> c[i] >> d[i];
if(d[i]>c[i])
{
for(int j=c[i];j<d[i];j++)
{
triple[j]=true;
}
}
}
for(int i=0;i<=N;i++)
{
ans+=1;
if(triple[i]) ans+=2;
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
//constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
ll N, M; scanf("%lld %lld", &N, &M);
ll left = -1;
set<int> s;
vector<vector<int>> g(N);
ll last = 0;
for (int i = 0; i < M; i++) {
ll c, d; scanf("%lld %lld", &c, &d);
c--; d--;
g[c].push_back(d);
}
ll res = 0;
for (int i = 0; i < N; i++) {
if (g[i].size() > 0) {
if (left == -1) left = i;
for (auto e : g[i]) s.insert(e);
}
if (s.find(i) != s.end()) {
s.erase(s.find(i));
if (s.size() == 0) {
res += i - last + (i - left) * 2;
last = i;
left = -1;
//cout << res << endl;
}
}
}
//cout << last << " " << res << endl;
res += N + 1 - last;
cout << res << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")
#include "bits/stdc++.h"
using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()
unsigned long xor128() {
static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
unsigned long t = (x ^ (x << 11));
x = y; y = z; z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
void init() {
iostream::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
#define int ll
void solve(){
while (true) {
int n, m;
cin >> n >> m;
if (n == 0) return;
int dp[1002] = {};
int left[1002] = {};
left[0] = 1e9;
for (int i = 1; i < 1002; ++i) {
dp[i] = 1e9;
left[i] = 1e9;
}
dp[0] = 0;
REP(i, m) {
int a, b;
cin >> a >> b;
left[b] = min(left[b], a);
}
for (int i = 0; i <= n; ++i) {
int lefting = 1e9;
for (int q = i; q <= n; ++q) {
lefting = min(lefting, left[q]);
dp[q + 1] = min(dp[q + 1], dp[i] + (q - i + 1LL) + max(0LL, 2LL * (q - lefting)));
}
}
cout << dp[n+1] << endl;
return;
}
}
#undef int
int main() {
init();
solve();
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
using namespace std;
int main(){
int N, m, c, d;
cin >> N >> m;
int imos[N+1];
fill(imos, imos+N+1, 0);
for(int i = 0; i < m; ++i){
cin >> c >> d;
++imos[c];
--imos[d];
}
int t = 0, ans = N+1;
for(int i = 0; i <= N; ++i){
t += imos[i];
if(t > 0) ans += 2;
}
cout << ans << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 28;
int dp[510][1010];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
while(cin >> N >> M) {
vector<P> r(M);
for(int i = 0; i < M; i++) {
cin >> r[i].second >> r[i].first;
}
sort(r.begin(), r.end());
fill((int*)begin(dp), (int*)end(dp), INF);
dp[0][N] = 0;
for(int i = 0; i < M; i++) {
int pos = r[i].first;
for(int j = 1; j <= N; j++) {
if(dp[i][j] == INF) continue;
int d = (i == 0) ? pos : abs(pos - r[i - 1].first);
// ??????
int ret = abs(pos - min(r[i].second, j));
dp[i + 1][N] = min(dp[i + 1][N], dp[i][j] + d + 2 * ret);
// ????????????
int nj = min(r[i].second, j);
dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + d);
}
}
int pos = (M == 0) ? 0 : r.back().first;
cout << dp[M][N] + (N + 1 - pos) << endl;
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi acos(-1.0)
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
int main(){
int n, m;
cin >> n >> m;
VI s(n+2);
REP(i,m){
int c, d;
cin >> c >> d;
s[c]++;
s[d]--;
}
REP(i,n+1) s[i+1] += s[i];
int x = 0;
REP(i,n+2) if (s[i] > 0) x++;
cout << n + 1 + 2*x << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java |
import java.io.*;
import java.util.*;
public class Main {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
public void run() {
int n = in.nextInt(), m = in.nextInt();
int[] array = new int[n+1];
for (int i = 0; i < m; i++) {
int c = in.nextInt(), d = in.nextInt();
for (int j = c; j < d; j++) {
array[j] = 1;
}
}
int cnt = 0;
for (int i = 0; i < n; i++)
if (array[i] == 1) cnt++;
System.out.println(cnt * 2 + n + 1);
out.close();
}
public static void main(String[] args) {
new Main().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
int n, m;
int cnt[1010];
bool intersect(pair<int, int> lhs, pair<int, int> rhs) {
if (lhs.first <= rhs.first && rhs.first <= lhs.second) { return true; }
if (rhs.first <= lhs.first && lhs.first <= rhs.second) { return true; }
return false;
}
int main() {
while (scanf("%d %d", &n, &m) > 0) {
MEMSET(cnt, 0);
REP(i, m) {
int x, y;
scanf("%d %d", &x, &y);
if (x > y) { continue; }
x--; y--;
cnt[x]++;
cnt[y]--;
}
int prev = -1;
int ans = n + 1;
int sum = 0;
REP(i, n) {
if (sum == 0 && cnt[i] > 0) {
prev = i;
} else if (sum > 0 && sum + cnt[i] == 0) {
ans += 2 * (i - prev);
}
sum += cnt[i];
}
assert(sum == 0);
printf("%d\n", ans);
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
using namespace std;
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
int main() {
int N, M;
cin >> N >> M;
vector<bool> B(N + 2, false);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
for (int i = a; i < b; i++) B[i] = true;
}
int ans = N + 1;
for (int i = 0; i < B.size(); i++) if (B[i]) ans += 2;
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;
#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
if (n == 0)return 1;
if (n == 1)return x%MOD;
ll res = sq(mod_pow(x, n / 2));
res %= MOD;
if (n % 2 == 1) {
res *= x;
res %= MOD;
}
return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }
//int n, m;
//vector<P> vp;
//
//int main() {
// cin >> n >> m;
// rep(i, m) {
//
// }
//}
int n, m;
vector<P> vp;
int sum[100100];
int main() {
cin >> n >> m;
vp.resize(m);
rep(i, m) {
int a, b;
cin >> a >> b;
sum[a]++;
sum[b]--;
}
rep(i, n + 2)sum[i + 1] += sum[i];
ll ans = 0;
rep(i, n + 1) {
if (sum[i] > 0)ans += 3;
else ans++;
}
cout << ans << endl;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int> >
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
using namespace std;
signed main(){
int n,m;
cin>>n>>m;
vi v(1001);
rep(i,m){
int c,d;
cin>>c>>d;
Rep(j,c,d){
v[j]=2;
}
}
int ans=n+1;
rep(i,1001){
ans+=v[i];
}
cout<<ans<<endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
int main()
{
int N, M;
cin >> N >> M;
map< int, vector<int> > memo;
rep(i, 0, M)
{
int c, d;
cin >> c >> d;
memo[c].push_back(i);
memo[d].push_back(i);
}
ll ans = 0;
set<int> first, second;
int memo_index = -1;
rep(i, 1, N + 1)
{
ans++;
if (memo[i].size() == 0) continue;
if (memo_index < 0) memo_index = i;
for (int n : memo[i])
{
if (first.find(n) == first.end())
first.insert(n);
else
second.insert(n);
}
if (first.size() == second.size())
{
ans += (i - memo_index) * 2;
memo_index = -1;
first.clear();
second.clear();
}
}
cout << ans + 1 << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int main() {
int N, m;
cin >> N >> m;
vi shop(N);
REP(i, m){
int c, d;
cin >> c >> d;
shop[c-1]++;
shop[d-1]--;
}
int res = N + 1;
int cnt = 0;
int start = -1;
REP(i, N){
if(shop[i] == 0) continue;
cnt += shop[i];
if(start == -1) start = i;
if(cnt == 0) {
res += (i - start) * 2;
start = -1;
}
}
cout << res << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | java |
import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;
public class Main {
int INF = 1 << 28;
//long INF = 1L << 62;
double EPS = 1e-10;
void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
if (m == 0) {
System.out.println(n+1);
return;
}
P[] ps = new P[m];
for (int i=0;i<m;i++) {
ps[i] = new P(sc.nextInt(), sc.nextInt());
}
sort(ps);
// for (P p : ps) debug(p.f, p.t);
int s = ps[0].f, e = ps[0].t;
ArrayList<P> l = new ArrayList<P>();
for (int i=1;;i++) {
// debug(s, e, ps[i].f, ps[i].t);
while (i < m && e >= ps[i].f) {
e = max(e, ps[i].t);
// debug(s, e, ps[i].f, ps[i].t);
i++;
}
l.add(new P(s, e));
if (i == m) break;
s = ps[i].f; e = ps[i].t;
}
int len = 0, c = 0;
for (P p: l) {
len += p.f - c;
len += 3 * (p.t - p.f);
c = p.t;
// debug(p.f, p.t);
}
len += n+1 - c;
System.out.println(len);
}
class P implements Comparable<P>{
int f, t;
P(int f, int t) {
this.f = f;
this.t = t;
}
public int compareTo(P o) {
// TODO 自動生成されたメソッド・スタブ
if (f == o.f) {
return o.t - t;
}
return f - o.f;
}
}
void debug(Object... os) {
System.err.println(Arrays.deepToString(os));
}
public static void main(String[] args) {
new Main().run();
}
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define FOR(i,bg,ed) for(int i=bg;i<ed;i++)
#define REP(i,n) FOR(i,0,n)
template<typename T>
bool chmax(T& l,T r){
bool ret = l<r;
if(l<r)l=r;
return ret;
}
template<typename T>
bool chmin(T& l,T r){
bool ret = l>r;
if(l>r)l=r;
return ret;
}
typedef long long LL;
typedef vector<LL> V;
string s;
const LL inf = 1e14;
const int empty = 1e9;
int main(){
int n,m;
cin>>n>>m;
vector<int> p(n+2,-1);
REP(i,m){
int c,d;
cin>>c>>d;
chmax(p[c],d);
}
LL res= n+1;
int bg = empty;
int ed = -1;
REP(i,n+1){
if(p[i+1]>=i+1){
chmin(bg,i+1);
chmax(ed,p[i+1]);
}
if(bg!=empty&&ed<=i+1){
res+=2*(i+1-bg);
ed=-1;
bg = empty;
}
}
cout<<res<<endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int N,M;
bool arr[1010] = {false};
cin >> N >> M;
for(int i = 0 ; i < M ; i++){
int c,d;
cin >> c >> d;
for(int j = c ; j < d ; j++){
arr[j] = true;
}
}
cout << N+2*count(arr,arr+N,true)+1 << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
#define N 1001
using namespace std;
int main(){
int n,m,c,d,l,r,flag,sum;
int V[N]={0};
cin >> n >> m;
for(int i=0;i<m;i++){
cin >> c >> d;
V[c]=max(V[c],d);
}
l=r=0;
flag=sum=0;
for(int i=0;i<=n;i++){
if(flag&&V[i]!=0) r=max(r,V[i]);
else if(flag&&r==i) sum+=3*(r-l),flag=0;
else if(V[i]!=0&&!flag) l=i,r=V[i],flag=1,sum++;
else if(!flag) sum++;
}
cout << sum << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define pmod 1000000007
#define maxn 100005
#define IN freopen("input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define FOR(i,a,b) for(int i=a ; i<=b ; i++)
#define INF 1000000000
#define ll long long int
#define eps (1e-8)
#define sq(x) ( (x)*(x) )
using namespace std;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
priority_queue<pii,vector<pii>, greater<pii> > q;
ll res=0;
int a[1000],b[1000],c1[1051],d1[1051];
int main()
{int N,m;
cin>>N>>m;
FOR (i,1,m){int c,d;
cin>> c>>d;
q.push(mp(c,d));
c1[i]=c;d1[i]=d;
//if (c==3 && d==86) cout<<123<<endl;
}
int t=1;int k=0;
while (!q.empty()){
int c1=q.top().first;
int d1=q.top().second;
q.pop();
if (!q.empty()){int c2=q.top().first;
int d2=q.top().second;
//if (c1==3 && d1==86) cout<<"dm"<<" "<<c2<<" "<<d2<<endl;
//if (k<=10) {k++;cout<<c1<<" "<<d1<<" "<<c2<<" "<<d2<<endl;}
if (d1>=c2){
q.pop();
q.push(mp(c1,max(d1,d2)));
}
else {res+=2*(d1-c1);}
}
else {res+=2*(d1-c1);}
}
cout<<res+N+1<<endl;
/*FOR(i,1,m){ bool u=0;
FOR(j,1,t-1){
if (c1[i]>=a[j] && d1[i]<=b[j]) u=1;
}
if (u==0) cout<<i<<" "<<c1[i]<<" "<<d1[i]<<endl;
}*/
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
int main() {
int N, m; cin >> N >> m;
vector<char> goback(N, 0);
while (m--) {
int c, d; cin >> c >> d;
fill_n(goback.begin() + c, d - c, 1);
}
cout << count_if(all(goback), [&](char c) { return c == 1; }) * 2 + N + 1 << endl;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int main(void)
{
int N, M;
cin >> N >> M;
vector<pair<int, int>> R(M);
for (auto &i : R) cin >> i.first >> i.second;
sort(R.begin(), R.end());
int ans = 0, now = 0;
int c, d;
for (auto i = R.begin(); i != R.end();)
{
c = i->first;
d = i->second;
for (i++; i != R.end(); i++)
{
if (i->first > d) break;
d = max(d, i->second);
}
ans += (d - now);
ans += (d - c);
now = c;
}
ans += (N + 1 - now);
cout << ans << endl;
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n,m;
int rev[1024]={};
int main(){
cin >> n >> m;
for(int i=0;i<m;++i){
int c,d;
cin >> c >> d;
rev[c]++;
rev[d]--;
}
for(int i=0;i<n;++i) rev[i+1]+=rev[i];
int answer=n+1;
for(int i=0;i<=n;++i) if(rev[i]) answer+=2;
cout << answer << endl;
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
int main() {
int n, m;
cin >> n >> m;
vector<int> v(n+2);
for (int i = 0; i < m; ++i) {
int c, d;
cin >> c >> d;
v[c]++; v[d]--;
}
int dep = 0, cur = 0;
int ans = 0;
for (int i = 1; i <= n+1; ++i) {
ans++;
if(dep != 0 && dep+v[i] == 0){
ans += (i-cur-1)*2;
}
dep += v[i];
if(dep == 0) cur = i;
}
cout << ans << "\n";
return 0;
}
|
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
typedef long long ll;
const int N = 1000;
const int M = 500;
int n, m;
int l[M], r[M];
int check[N+2];
int il[M], ir[M];
int main() {
int i, j;
scanf("%d%d ", &n, &m);
for (i=0; i<n; i++)
scanf("%d%d ", l+i, r+i);
memset(check, 0, sizeof(check));
for (i=0; i<m; i++) {
for (j=l[i]; j<r[i]; j++)
check[j] = 1;
if (check[r[i]] != 1)
check[r[i]] = 2;
}
m = 0;
for (i=1; i<=n; i++) {
if (check[i-1] != 1 && check[i])
il[m] = i;
if (check[i] == 2)
ir[m++] = i;
}
int res = 0;
int prev = 0;
i = 0;
while (i<m) {
res += ir[i] - prev;
res += ir[i] - il[i];
prev = il[i++];
}
res += n+1 - prev;
printf("%d\n", res);
return 0;
} |
p00926 Shopping | Example
Input
10 3
3 7
8 9
2 5
Output
23 | {
"input": [
"10 3\n3 7\n8 9\n2 5"
],
"output": [
"23"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define FOR(x,y) for(__typeof(y.begin()) x=y.begin();x!=y.end();x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 1005
void RI(){}
template<typename... T>
void RI( int& head, T&... tail ) {
scanf("%d",&head);
RI(tail...);
}
using namespace std;
typedef long long LL;
int n,m,ans;
bool vis[M];
int main()
{
int x,y;
RI(n,m);
ans = n+1;
REP(i,1,m)
{
RI(x,y);
REP(j,x,y-1) if(!vis[j])
{
vis[j] = true;
ans += 2;
}
}
printf("%d\n", ans);
return 0;
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.