传送门:点击打开链接
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 ninstead 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.
In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.
Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
3
1 2
4
1 3
12
5 7
代码实现;
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
int main()
{
ll n,i,j,k;
while(cin>>n)
{
for(i=n/2;i>=1;i--)
{
k=n-i;
if(gcd(i,k)==1)
{
cout<<i<<' '<<k<<endl;
break;
}
}
}
return 0;
}
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1to 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.
The only line of the input contains two integers: n and k (1 ≤ n ≤ 109, 0 ≤ k ≤ n).
Print the minimum possible and the maximum possible number of apartments good for Maxim.
6 3
1 3
In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
int main()
{
int n,k;
while(cin>>n>>k)
{
if(n-k==0||k==0)
{
cout<<"0"<<' '<<"0"<<endl;
continue;
}
if(n-k==1)
{
cout<<"1"<<' '<<"1"<<endl;
}
else if(n-k==2)
{
cout<<"1 2"<<endl;
}
else
{
if(k>=n/2)
{
cout<<"1 "<<n-k<<endl;
}
else
{
if(n>=k*3)
{
cout<<"1 "<<k*2<<endl;
}
else
{
cout<<"1 "<<n-k<<endl;
}
}
}
}
return 0;
}
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 k minutes 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.
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.
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.
5 2 4 2 1 10 2
20 3 6 7 4 5
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 = 38 burles.
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 = 20burles.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=300005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
struct node{
ll index;
ll x;
friend bool operator <(node a,node b) //越小的优先级越高
{
return a.x<b.x;
}
}p[maxn];
ll map[maxn]; //储存结果
int main()
{
ll n,k,i,j,ans;
while(cin>>n>>k)
{
ans=0;
mset(map,0);
for(i=1;i<=n;i++)
{
cin>>p[i].x;
p[i].index=i;
}
priority_queue <node> q; //定义队列
for(i=1;i<=k;i++) //入队列
{
q.push(p[i]);
}
for(i=k+1;i<=k+n;i++) //枚举
{
if(i<=n) //前n个数入队列
q.push(p[i]);
node temp=q.top(); //取优先级最高的出队列
q.pop();
ans+=(i-temp.index)*temp.x; //计算总成本
map[temp.index]=i; //记录
}
cout<<ans<<endl;
for(i=1;i<=n;i++)
{
if(i==1)
cout<<map[i];
else
cout<<' '<<map[i];
}
cout<<endl;
}
return 0;
}