Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)

来源:互联网 发布:唱歌测音软件 编辑:程序博客网 时间:2024/06/05 04:44

第一次在正式场里面做出四题,前100,鸡冻惹!


A. Fraction
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction  is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1).

During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (÷) and got sum of numerator and denominator that was equal to n instead of the expected decimal notation.

Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That's why he decided to determine maximum possible proper irreducible fraction  such that sum of its numerator and denominator equals n. Help Petya deal with this problem.

Input

In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.

Output

Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.

Examples
input
3
output
1 2
input
4
output
1 3
input
12
output
5 7

求个gcd


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int gcd(int x,int y) {int z;if (x<y) swap(x,y);while (y) {z=y;y=x%y;x=z;}return x;}int main() {int n,i,j;scanf("%d",&n);for (i=n/2,j=n-i;i>0&&j<n;i--,j++) {if (gcd(i,j)==1) {printf("%d %d\n",i,j);return 0;}}return 0;}

B. Maxim Buys an Apartment
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has napartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.

Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly k already inhabited apartments, but he doesn't know their indices yet.

Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim.

Input

The only line of the input contains two integers: n and k (1 ≤ n ≤ 1090 ≤ k ≤ n).

Output

Print the minimum possible and the maximum possible number of apartments good for Maxim.

Example
input
6 3
output
1 3
Note

In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 12 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 13 and 5 were inhabited. In this case all other apartments: 24 and 6 are good.




水,分类讨论一下。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int main() {int n,k;scanf("%d%d",&n,&k);if (n==1) {printf("0 0");return 0;}if (k==0||k==n) printf("0"); else printf("1");printf(" ");if (k>=(n+2)/3) printf("%d",n-k); else printf("%d",2*k);return 0;}

C. Planning
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today, the i-th of them is planned to depart at the i-th minute of the day.

Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first kminutes of the day, so now the new departure schedule must be created.

All n scheduled flights must now depart at different minutes between (k + 1)-th and (k + n)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do so — their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule.

Helen knows that each minute of delay of the i-th flight costs airport ci burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 300 000), here n is the number of flights, and k is the number of minutes in the beginning of the day that the flights did not depart.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 107), here ci is the cost of delaying the i-th flight for one minute.

Output

The first line must contain the minimum possible total cost of delaying the flights.

The second line must contain n different integers t1, t2, ..., tn (k + 1 ≤ ti ≤ k + n), here ti is the minute when the i-th flight must depart. If there are several optimal schedules, print any of them.

Example
input
5 24 2 1 10 2
output
203 6 7 4 5 
Note

Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3 - 1)·4 + (4 - 2)·2 + (5 - 3)·1 + (6 - 4)·10 + (7 - 5)·2 = 38burles.

However, the better schedule is shown in the sample answer, its cost is (3 - 1)·4 + (6 - 2)·2 + (7 - 3)·1 + (4 - 4)·10 + (5 - 5)·2 = 20 burles.



每次用线段树找一下当前到达的所有航班当中,费用最贵的,把它去掉。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=300005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);ll a[maxn],ans[maxn];int num;struct Tree {int lc,rc,l,r,max;ll num;};Tree tree[4*maxn];void pushup(int now) {if (tree[tree[now].lc].max>tree[tree[now].rc].max) {tree[now].max=tree[tree[now].lc].max;tree[now].num=tree[tree[now].lc].num;}else {tree[now].max=tree[tree[now].rc].max;tree[now].num=tree[tree[now].rc].num;}}void build(int now,int l,int r) {tree[now].l=l;tree[now].r=r;if (l!=r) {num++;tree[now].lc=num;build(num,l,(l+r)/2);num++;tree[now].rc=num;build(num,(l+r)/2+1,r);pushup(now);} else tree[now].max=0,tree[now].num=l;}void update (int now,int l,int r,int c) {//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].num << endl;if (tree[now].l>=l&&tree[now].r<=r) {tree[now].max=c;} else {if (l<=(tree[now].l+tree[now].r)/2)     update(tree[now].lc,l,r,c);if (r>(tree[now].l+tree[now].r)/2)    update(tree[now].rc,l,r,c);pushup(now);}//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].num << endl;}int main() {int n,k,j;ll i;scanf("%d%d",&n,&k);for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);}ll tot=0;num=1;build(1,1,n);for (i=1;i<=k;i++) {update(1,i,i,a[i]);}for (i=k+1;i<=k+n;i++) {if (i<=n) update(1,i,i,a[i]);tot+=(i-tree[1].num)*a[tree[1].num];ans[tree[1].num]=i;update(1,tree[1].num,tree[1].num,0);}printf("%I64d\n",tot);for (i=1;i<=n;i++) {printf("%d ",ans[i]);}return 0;}

D. Jury Meeting
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.

There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.

You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.

Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

Input

The first line of input contains three integers nm and k (1 ≤ n ≤ 1050 ≤ m ≤ 1051 ≤ k ≤ 106).

The i-th of the following m lines contains the description of the i-th flight defined by four integers difitiand ci (1 ≤ di ≤ 1060 ≤ fi ≤ n0 ≤ ti ≤ n1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output

Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.

If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).

Examples
input
2 6 51 1 0 50003 2 0 55002 2 0 600015 0 2 90009 0 1 70008 0 2 6500
output
24500
input
2 4 51 2 0 50002 1 0 45002 1 0 30008 0 1 6000
output
-1
Note

The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 128 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.

In the second sample it is impossible to send jury member from city 2 back home from Metropolis.



对于每个时间,处理一下,得到在它之前所有航班到达首都、之后所有航班从首都回去的最小费用。

然后,枚举其中一个的时间,另一个的最小费用可以用线段树查询到满足条件的时间区间内的最小值。

复杂度O(nlogn)

其实不需要线段树,直接O(n)线性扫描也可以。

明天来写的详细点,早上满课,不敢熬夜。。。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))#define N 1000000using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=1000005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);ll rc[maxn];int to[100005];int num;struct flight {int d,t,c;};flight a[maxn],b[maxn];bool cmp(flight a,flight b) {return a.d<b.d;}bool cmp2(flight a,flight b) {return a.d>b.d;}struct Tree {int lc,rc,l,r;ll min;};Tree tree[3*maxn];void build(int now,int l,int r) {tree[now].l=l;tree[now].r=r;if (l!=r) {num++;tree[now].lc=num;build(num,l,(l+r)/2);num++;tree[now].rc=num;build(num,(l+r)/2+1,r);tree[now].min=min(tree[tree[now].lc].min,tree[tree[now].rc].min);} else tree[now].min=rc[l];}ll findmin (int now,int l,int r) {//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].tag << endl;if (tree[now].l>=l&&tree[now].r<=r) {return tree[now].min;} else {ll ans=llinf;if (l<=(tree[now].l+tree[now].r)/2)     ans=min(ans,findmin(tree[now].lc,l,r));if (r>(tree[now].l+tree[now].r)/2)    ans=min(ans,findmin(tree[now].rc,l,r));return ans;}//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].tag << endl;}int main() {int n,m,k,i,j,x,y,z,w,m1,m2;m1=m2=0;scanf("%d%d%d",&n,&m,&k);for (i=1;i<=m;i++) {scanf("%d%d%d%d",&w,&x,&y,&z);if (y==0) {a[++m1].d=w;a[m1].t=x;a[m1].c=z;} else {b[++m2].d=w;b[m2].t=y;b[m2].c=z;}}sort(a+1,a+m1+1,cmp);sort(b+1,b+m2+1,cmp2);int cnt,last=-1;cnt=0;j=1;meminf(to);rc[N+1]=0;for (i=N;i>=1;i--) {rc[i]=rc[i+1];for (;b[j].d>=i&&j<=m2;j++) {if (to[b[j].t]==inf) {cnt++;to[b[j].t]=b[j].c;rc[i]+=(ll)b[j].c;} else {if (to[b[j].t]>b[j].c) {rc[i]-=(ll)(to[b[j].t]-b[j].c);to[b[j].t]=b[j].c;}}}if (cnt==n&&last==-1) last=i;}num=1;if (last==-1) {cout << -1;return 0;}build(1,1,last);meminf(to);ll ans=llinf,p=0;j=1;cnt=0;for (i=1;i+k<last;i++) {for (;a[j].d<=i&&j<=m1;j++) {if (to[a[j].t]==inf) {cnt++;to[a[j].t]=a[j].c;p+=(ll)a[j].c;} else {if (to[a[j].t]>a[j].c) {p-=(ll)(to[a[j].t]-a[j].c);to[a[j].t]=a[j].c;}}}if (cnt==n) ans=min(ans,p+findmin(1,i+k+1,last));}if (ans==llinf) ans=-1;printf("%I64d",ans);return 0;}


最后截图纪念一下~




阅读全文
0 0
原创粉丝点击