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

来源:互联网 发布:c语言开发环境 编辑:程序博客网 时间:2024/05/21 07:56

A. Fraction
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard 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
水题

import com.sun.jmx.snmp.tasks.TaskServer;import java.io.OutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.StringTokenizer;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.InputStream;public class Main {    public static void main(String []args){        InputStream inputStream = System.in;        OutputStream outputStream = System.out;        InputReader in = new InputReader(inputStream);        PrintWriter out = new PrintWriter(outputStream);        TaskA taskA = new TaskA();        taskA.solve(1,in,out);        out.close();    }    static class TaskA{        private int gcd(int n,int m){            if(n>m){                int t=n;                n=m;                m=t;            }            while(n!=0){                int q=m%n;                m=n;                n=q;            }            return m;        }        int a;        int b;        public void solve(int test,InputReader in,PrintWriter out){            int n;            n=in.nextInt();            for(int i=1;i<=n/2;i++){                if(gcd(i,n-i)==1){                    a=i;                    b=n-i;                }            }            out.print(a+" ");            out.println(b);        }    }    static class InputReader {        public BufferedReader reader;        public StringTokenizer tokenizer;        public InputReader(InputStream stream) {            reader = new BufferedReader(new InputStreamReader(stream), 32768);            tokenizer = null;        }        public String next() {            while (tokenizer == null || !tokenizer.hasMoreTokens()) {                try {                    tokenizer = new StringTokenizer(reader.readLine());                } catch (IOException e) {                    throw new RuntimeException(e);                }            }            return tokenizer.nextToken();        }        public int nextInt() {            return Integer.parseInt(next());        }    }}

B. Maxim Buys an Apartment
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments 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 ≤ 109, 0 ≤ 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 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, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
水题

import com.sun.jmx.snmp.tasks.TaskServer;import java.io.OutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.StringTokenizer;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.InputStream;public class Main {    public static void main(String []args){        InputStream inputStream = System.in;        OutputStream outputStream = System.out;        InputReader in = new InputReader(inputStream);        PrintWriter out = new PrintWriter(outputStream);        TaskB taskB = new TaskB();        taskB.solve(1,in,out);        out.close();    }    static class TaskB{        public void solve(int test,InputReader in,PrintWriter out){            int n,k;            n=in.nextInt();            k=in.nextInt();            if(n==k || k==0){                out.println("0 0");            } else {                if(2*k<=n-k){                    out.print("1 ");                    out.println(2*k);                } else {                    out.print("1 ");                    out.println(n-k);                }            }        }    }    static class InputReader {        public BufferedReader reader;        public StringTokenizer tokenizer;        public InputReader(InputStream stream) {            reader = new BufferedReader(new InputStreamReader(stream), 32768);            tokenizer = null;        }        public String next() {            while (tokenizer == null || !tokenizer.hasMoreTokens()) {                try {                    tokenizer = new StringTokenizer(reader.readLine());                } catch (IOException e) {                    throw new RuntimeException(e);                }            }            return tokenizer.nextToken();        }        public int nextInt() {            return Integer.parseInt(next());        }    }}

C. Planning
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.

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 2
4 2 1 10 2
output
20
3 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 = 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 = 20 burles.

贪心

#include <stdio.h>#include <iostream>#include <string.h>#include <cmath>#include <set>#include <map>#include <vector>#include <algorithm>#include <cmath>using namespace std;struct node{    int num;    int id;}c[300100];long long a[300100];int b[300100];bool cmp(node a,node b){    return a.num>b.num;}set<int> s;int main(){    int n,k;    cin>>n>>k;    for(int i=1;i<=n;i++){        scanf("%d",&c[i].num);        c[i].id=i;        s.insert(i+k);    }    sort(c+1,c+n+1,cmp);    for(int i=1;i<=n;i++){        auto it=s.lower_bound(c[i].id);        int t1=*it;        if(it!=s.begin()) it--;        int t2=*it;        if(abs(c[i].id-t1)<abs(c[i].id-t2)){            a[i]=abs(c[i].id-t1)*1LL;            b[c[i].id]=t1;            s.erase(t1);        } else {            a[i]=abs(c[i].id-t2)*1LL;            b[c[i].id]=t2;            s.erase(t2);        }    }    long long sum=0;    for(int i=1;i<=n;i++){        sum+=(a[i]*c[i].num*1LL);    }    cout<<sum<<endl;    for(int i=1;i<=n;i++){        cout<<b[i]<<" ";    }    puts("");    return 0;}
阅读全文
0 0
原创粉丝点击