src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class A
{
public A()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Integer mas[] = new Integer[n];
int b = 0;
for (int i = 0 ; i < n ; i ++)
{
mas[i] = sc.nextInt();
b+=mas[i];
}
Arrays.sort(mas, new Comparator<Integer>()
{
@Override
public int compare(Integer o1, Integer o2)
{
if(o1>o2)
return -1;
else if(o1==o2)
return 0;
else
return 1;
}
});
int N = 0; int g = 0;
for (int i = 0 ; i < n ; i ++)
{
g+=mas[i];
if(g>(int)(b/2))
{
System.out.println(i+1);
return;
}
}
System.out.println(n);
}
public static void main(String[] args)
{
new A();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class D {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
int[] p = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = p[i] = in.nextInt();
Arrays.sort(a);
int sum = 0;
int t = 0;
for (int i = 0; i < a.length; i++)
t += a[i];
int cnt = 0;
for (int i = a.length - 1; i >= 0; i--) {
cnt++;
sum += a[i];
if (t - sum < sum)
break;
}
System.out.println(cnt);
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
public class A
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
String FInput="";
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
catch (NullPointerException e)
{
line=null;
}
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
long NextLong()
{
String n = inputParser.nextToken();
long val = Long.parseLong(n);
//System.out.println("I read this number: " + val);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public static void main(String [] argv)
{
String filePath=null;
if(argv.length>0)filePath=argv[0];
new A(filePath);
}
public void readFInput()
{
for(;;)
{
try
{
readNextLine();
FInput+=line+" ";
}
catch(Exception e)
{
break;
}
}
inputParser = new StringTokenizer(FInput, " ");
}
public A(String inputFile)
{
openInput(inputFile);
readNextLine();
int n=NextInt();
int ret=0;
int [] p = new int [n];
readNextLine();
int sum=0;
for(int i=0; i<n; i++)
{
int a=NextInt();
p[i]=a;
sum+=a;
}
Arrays.sort(p);
int my=0;
for(int i=n-1; i>=0; i--)
{
my+=p[i];
ret++;
if(my*2>sum)break;
}
System.out.println(ret);
closeInput();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.InputMismatchException;
public class R111_D2_A {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
// BufferedReader in = new BufferedReader(new
// InputStreamReader(System.in));
int n = in.readInt();
int[] inp = new int[n];
for (int i = 0; i < inp.length; i++) {
inp[i] = in.readInt();
}
Arrays.sort(inp);
int sum1 = 0;
int res = 0;
for (int i = inp.length - 1; i >= 0; i--) {
sum1 += inp[i];
res++;
int sum2 = 0;
for (int j = 0; j < i; j++) {
sum2 += inp[j];
}
if (sum1 > sum2) {
break;
}
}
System.out.println(res);
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private 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++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
public class A {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int n=in.nextInt(),s=0;
int[] a= new int[n];
for (int i=0;i<n;i++) {a[i]=in.nextInt(); s+=a[i];}
Arrays.sort(a); int k=0,ans=0;
for (int i=n-1;i>=0;i--)
if (k<=s/2) {k+=a[i];ans++;}
System.out.println(ans);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nova
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] a = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
sum += a[i];
}
Arrays.sort(a);
int res = 0;
int mySum = 0;
int hisSum = sum;
for (int i = n - 1; i >= 0; i--) {
mySum += a[i];
hisSum -= a[i];
res++;
if (mySum > hisSum) break;
}
out.println(res);
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
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());
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
//coded by : ariefianto17 | Reza Ariefianto
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main (String[]args)
{
Scanner read = new Scanner (new BufferedInputStream (System.in));
int n = read.nextInt();
int[]arr = new int[n];
int sum=0;
int sum2=0;
int coin=0;
for(int i=0;i<n;i++)
{
arr[i] = read.nextInt();
sum+=arr[i];
}
Arrays.sort(arr);
for(int i=n-1;i>=0;i--)
{
sum2+=arr[i];
sum-=arr[i];
coin++;
if(sum2>sum)
break;
}
System.out.println(coin);
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class solver {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
new solver().solve();
}
public void solve() {
int n = in.nextInt();
int[]tab = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
tab[i] = in.nextInt();
sum += tab[i];
}
Arrays.sort(tab);
int v1 = 0;
int count = 0;
for (int i = tab.length - 1; i >= 0; i--) {
v1 += tab[i];
count++;
if (v1 > getSum(i, tab)) {
break;
}
}
out.println(count);
in.close();
out.close();
}
public int getSum(int index, int[]tab) {
int sum = 0;
for (int i = 0; i < index; i++) sum += tab[i];
return sum;
}
public int max(int a, int b) {
if (a > b) return a;
else return b;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
System.err.println(e);
return "";
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
BigInteger nextBigInt() {
return new BigInteger(next());
}
void close() {
try {
br.close();
} catch (IOException e) {
}
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
import java.math.*;
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
Scanner c=new Scanner(System.in);
int N=c.nextInt();
int A[]=new int[N];
for(int i=0;i<N;i++)
{
A[i]=c.nextInt();
}
Arrays.sort(A);
int sum=0;
for(int i=0;i<N;i++)
{
sum+=A[i];
}
int my=0;
int coins=0;
for(int i=N-1;i>=0;i--)
{
coins++; //include coin i
my+=A[i];
if(my>sum-my)
{
System.out.println(coins);
break;
}
}
}
}
//must declare new classes here | nlogn | 160_A. Twins | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] A = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
A[i] = in.nextInt();
sum += A[i];
}
Arrays.sort(A);
int cnt = 0;
int temp = 0;
for (int i = n - 1; i >= 0; i--) {
temp += A[i];
sum -= A[i];
cnt++;
if (temp > sum)
break;
}
System.out.println(cnt);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class CF_111_A {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt(), sum = 0, sum2 = 0;
int[] a = new int[n];
for (int i = 0; i < n; i++){
a[i] = in.nextInt();
sum += a[i];
}
Arrays.sort(a);
for (int i = n - 1; i >=0; i--){
sum2 +=a[i];
if (sum2 * 2 > sum){
System.out.println(n - 1 - i + 1);
System.exit(0);
}
}
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class A {
int n;
int[] a;
public void run() throws IOException{
Scanner s = new Scanner(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = s.nextInt();
a = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum+=a[i];
}
Arrays.sort(a);
long mysum = 0;
int count = 0;
for (int i = n-1; i > -1; i--) {
if (mysum > sum)
break;
count++;
mysum+=a[i];
sum-=a[i];
}
System.out.println(count);
}
public static void main(String[] args) throws IOException {
(new A()).run();
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
Arrays.sort(a);
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
int cur = 0;
for (int i = n - 1; i >= 0; i--) {
cur += a[i];
if (cur > sum - cur) {
out.println(n - i);
return;
}
}
}
void inp() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new A().inp();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "0";
}
}
return st.nextToken();
}
String nextString() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "0";
}
}
return st.nextToken("\n");
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer=null;
public static void main(String[] args) throws IOException
{
new Main().execute();
}
void debug(Object...os)
{
System.out.println(Arrays.deepToString(os));
}
int ni() throws IOException
{
return Integer.parseInt(ns());
}
long nl() throws IOException
{
return Long.parseLong(ns());
}
double nd() throws IOException
{
return Double.parseDouble(ns());
}
String ns() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(br.readLine());
return tokenizer.nextToken();
}
String nline() throws IOException
{
tokenizer=null;
return br.readLine();
}
//Main Code starts Here
int totalCases, testNum;
int sum;
int n;
int arr[];
void execute() throws IOException
{
totalCases = 1;
for(testNum = 1; testNum <= totalCases; testNum++)
{
if(!input())
break;
solve();
}
}
void solve() throws IOException
{
Arrays.sort(arr);
int count = 0;
int ans = 0;
for(int i = n-1;i>=0;i--)
{
count+= arr[i];
if(count>sum-count)
{
ans = n-i;
break;
}
}
System.out.println(ans);
}
boolean input() throws IOException
{
n = ni();
sum = 0;
arr = new int[n];
for(int i = 0;i<n;i++)
{
arr[i] = ni();
sum = sum+arr[i];
}
return true;
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
public class A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) a[i] = s.nextInt();
int r = 0, an = 0;
Arrays.sort(a);
int t = 0;
for(int z : a) t += z;
for(int i=a.length-1;i>=0;i--){
r += a[i];
an++;
if (r > t - r) break;
}
System.out.println(an);
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution implements Runnable {
public static void main(String[] args) {
(new Thread(null, new Solution(), "1", 1l << 28)).start();
}
public void run() {
try {
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
out.close();
}
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = null;
String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
//if (!in.ready()) return null;
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
void solve() throws Exception {
int n = nextInt();
int[] a = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) sum += (a[i] = nextInt());
Arrays.sort(a);
int ans = 0;
int s = 0;
for (int i = n - 1; i >= 0; i--) {
s += a[i]; ans++;
if (2 * s > sum) break;
}
out.println(ans);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class A {
public void solve() throws Exception {
int n = nextInt();
int[] p = nextArr(n);
Arrays.sort(p);
int sum = 0;
for (int i=0; i<n; ++i) sum+=p[i];
int curr = 0;
for (int i=n-1; i>=0; --i) {
curr += p[i];
if (curr>sum-curr) halt(n-i);
}
}
////////////////////////////////////////////////////////////////////////////
boolean showDebug = true;
static boolean useFiles = false;
static String inFile = "input.txt";
static String outFile = "output.txt";
double EPS = 1e-7;
int INF = Integer.MAX_VALUE;
long INFL = Long.MAX_VALUE;
double INFD = Double.MAX_VALUE;
int absPos(int num) {
return num<0 ? 0:num;
}
long absPos(long num) {
return num<0 ? 0:num;
}
double absPos(double num) {
return num<0 ? 0:num;
}
int min(int... nums) {
int r = Integer.MAX_VALUE;
for (int i: nums)
if (i<r) r=i;
return r;
}
int max(int... nums) {
int r = Integer.MIN_VALUE;
for (int i: nums)
if (i>r) r=i;
return r;
}
long minL(long... nums) {
long r = Long.MAX_VALUE;
for (long i: nums)
if (i<r) r=i;
return r;
}
long maxL(long... nums) {
long r = Long.MIN_VALUE;
for (long i: nums)
if (i>r) r=i;
return r;
}
double minD(double... nums) {
double r = Double.MAX_VALUE;
for (double i: nums)
if (i<r) r=i;
return r;
}
double maxD(double... nums) {
double r = Double.MIN_VALUE;
for (double i: nums)
if (i>r) r=i;
return r;
}
long sumArr(int[] arr) {
long res = 0;
for (int i: arr)
res+=i;
return res;
}
long sumArr(long[] arr) {
long res = 0;
for (long i: arr)
res+=i;
return res;
}
double sumArr(double[] arr) {
double res = 0;
for (double i: arr)
res+=i;
return res;
}
long partsFitCnt(long partSize, long wholeSize) {
return (partSize+wholeSize-1)/partSize;
}
boolean odd(long num) {
return (num&1)==1;
}
boolean hasBit(int num, int pos) {
return (num&(1<<pos))>0;
}
long binpow(long a, int n) {
long r = 1;
while (n>0) {
if ((n&1)!=0) r*=a;
a*=a;
n>>=1;
}
return r;
}
boolean isLetter(char c) {
return (c>='a' && c<='z') || (c>='A' && c<='Z');
}
boolean isLowercase(char c) {
return (c>='a' && c<='z');
}
boolean isUppercase(char c) {
return (c>='A' && c<='Z');
}
boolean isDigit(char c) {
return (c>='0' && c<='9');
}
boolean charIn(String chars, String s) {
if (s==null) return false;
if (chars==null || chars.equals("")) return true;
for (int i=0; i<s.length(); ++i)
for (int j=0; j<chars.length(); ++j)
if (chars.charAt(j)==s.charAt(i)) return true;
return false;
}
String stringn(String s, int n) {
if (n<1 || s==null) return "";
StringBuilder sb = new StringBuilder(s.length()*n);
for (int i=0; i<n; ++i) sb.append(s);
return sb.toString();
}
String str(Object o) {
if (o==null) return "";
return o.toString();
}
long timer = System.currentTimeMillis();
void startTimer() {
timer = System.currentTimeMillis();
}
void stopTimer() {
System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0);
}
static class InputReader {
private byte[] buf;
private int bufPos = 0, bufLim = -1;
private InputStream stream;
public InputReader(InputStream stream, int size) {
buf = new byte[size];
this.stream = stream;
}
private void fillBuf() throws IOException {
bufLim = stream.read(buf);
bufPos = 0;
}
char read() throws IOException {
if (bufPos>=bufLim) fillBuf();
return (char)buf[bufPos++];
}
boolean hasInput() throws IOException {
if (bufPos>=bufLim) fillBuf();
return bufPos<bufLim;
}
}
static InputReader inputReader;
static BufferedWriter outputWriter;
char nextChar() throws IOException {
return inputReader.read();
}
char nextNonWhitespaceChar() throws IOException {
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
return c;
}
String nextWord() throws IOException {
StringBuilder sb = new StringBuilder();
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
while (c>' ') {
sb.append(c);
c = inputReader.read();
}
return new String(sb);
}
String nextLine() throws IOException {
StringBuilder sb = new StringBuilder();
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
while (c!='\n' && c!='\r') {
sb.append(c);
c = inputReader.read();
}
return new String(sb);
}
int nextInt() throws IOException {
int r = 0;
char c = nextNonWhitespaceChar();
boolean neg = false;
if (c=='-') neg=true;
else r=c-48;
c = nextChar();
while (c>='0' && c<='9') {
r*=10;
r+=c-48;
c=nextChar();
}
return neg ? -r:r;
}
long nextLong() throws IOException {
long r = 0;
char c = nextNonWhitespaceChar();
boolean neg = false;
if (c=='-') neg=true;
else r = c-48;
c = nextChar();
while (c>='0' && c<='9') {
r*=10L;
r+=c-48L;
c=nextChar();
}
return neg ? -r:r;
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextWord());
}
int[] nextArr(int size) throws NumberFormatException, IOException {
int[] arr = new int[size];
for (int i=0; i<size; ++i)
arr[i] = nextInt();
return arr;
}
long[] nextArrL(int size) throws NumberFormatException, IOException {
long[] arr = new long[size];
for (int i=0; i<size; ++i)
arr[i] = nextLong();
return arr;
}
double[] nextArrD(int size) throws NumberFormatException, IOException {
double[] arr = new double[size];
for (int i=0; i<size; ++i)
arr[i] = nextDouble();
return arr;
}
String[] nextArrS(int size) throws NumberFormatException, IOException {
String[] arr = new String[size];
for (int i=0; i<size; ++i)
arr[i] = nextWord();
return arr;
}
char[] nextArrCh(int size) throws IOException {
char[] arr = new char[size];
for (int i=0; i<size; ++i)
arr[i] = nextNonWhitespaceChar();
return arr;
}
char[][] nextArrCh(int rows, int columns) throws IOException {
char[][] arr = new char[rows][columns];
for (int i=0; i<rows; ++i)
for (int j=0; j<columns; ++j)
arr[i][j] = nextNonWhitespaceChar();
return arr;
}
char[][] nextArrChBorders(int rows, int columns, char border) throws IOException {
char[][] arr = new char[rows+2][columns+2];
for (int i=1; i<=rows; ++i)
for (int j=1; j<=columns; ++j)
arr[i][j] = nextNonWhitespaceChar();
for (int i=0; i<columns+2; ++i) {
arr[0][i] = border;
arr[rows+1][i] = border;
}
for (int i=0; i<rows+2; ++i) {
arr[i][0] = border;
arr[i][columns+1] = border;
}
return arr;
}
void printf(String format, Object... args) throws IOException {
outputWriter.write(String.format(format, args));
}
void print(Object o) throws IOException {
outputWriter.write(o.toString());
}
void println(Object o) throws IOException {
outputWriter.write(o.toString());
outputWriter.newLine();
}
void print(Object... o) throws IOException {
for (int i=0; i<o.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(o[i].toString());
}
}
void println(Object... o) throws IOException {
print(o);
outputWriter.newLine();
}
void printn(Object o, int n) throws IOException {
String s = o.toString();
for (int i=0; i<n; ++i) {
outputWriter.write(s);
if (i!=n-1) outputWriter.write(' ');
}
}
void printnln(Object o, int n) throws IOException {
printn(o, n);
outputWriter.newLine();
}
void printArr(int[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Integer.toString(arr[i]));
}
}
void printArr(long[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Long.toString(arr[i]));
}
}
void printArr(double[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Double.toString(arr[i]));
}
}
void printArr(String[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(arr[i]);
}
}
void printArr(char[] arr) throws IOException {
for (char c: arr) outputWriter.write(c);
}
void printlnArr(int[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(long[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(double[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(String[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(char[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void halt(Object... o) throws IOException {
if (o.length!=0) println(o);
outputWriter.flush(); outputWriter.close();
System.exit(0);
}
void debug(Object... o) {
if (showDebug) System.err.println(Arrays.deepToString(o));
}
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.US);
if (!useFiles) {
inputReader = new InputReader(System.in, 1<<16);
outputWriter = new BufferedWriter(new OutputStreamWriter(System.out), 1<<16);
} else {
inputReader = new InputReader(new FileInputStream(new File(inFile)), 1<<16);
outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outFile))), 1<<16);
}
new A().solve();
outputWriter.flush(); outputWriter.close();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
//Edwin Lunando template for online algorithm contest
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
// class node implements Comparable<node> {
//
// public int a;
// public int b;
//
// public int compareTo(node a) {
// if (a.b == this.b) {
// return a.a - this.a;
// } else {
// return a.b - this.b;
// }
// }
//
// public node(int a, int b) {
// this.a = a;
// this.b = b;
// }
// }
private void solve() throws IOException {
int n = nextInt();
int[] arr = new int[n];
int count = 0;
for (int x = 0; x < n; x++) {
arr[x] = nextInt();
count+= arr[x];
}
Arrays.sort(arr);
count /=2;
int result = 0, sum = 0;
for (int x = arr.length - 1; x >= 0; x--) {
sum += arr[x];
result++;
if (sum > count) {
break;
}
}
System.out.println(result);
}
public static void main(String[] args) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter(System.out);
//out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
new Main().solve();
out.close();
} catch (Throwable e) {
System.out.println(e);
System.exit(239);
}
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static int[] nextIntArray(int n) throws IOException {
int[] temp = new int[n];
for (int x = 0; x < n; x++) {
temp[x] = nextInt();
}
return temp;
}
static long[] nextLongArray(int n) throws IOException {
long[] temp = new long[n];
for (int x = 0; x < n; x++) {
temp[x] = nextLong();
}
return temp;
}
static String[] nextArray(int n) throws IOException {
String[] temp = new String[n];
for (int x = 0; x < n; x++) {
temp[x] = nextToken();
}
return temp;
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
public class A implements Runnable {
private MyScanner in;
private PrintWriter out;
private void solve() {
int n = in.nextInt();
int[] a = new int[n];
int all = 0;
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
all += a[i];
}
Arrays.sort(a);
int sum = 0, ans = 0;
for (int i = n - 1; i >= 0; --i) {
sum += a[i];
++ans;
if (sum > all - sum) {
break;
}
}
out.println(ans);
}
@Override
public void run() {
in = new MyScanner();
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
public static void main(String[] args) {
new A().run();
}
static class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return st.nextToken();
}
public String nextLine() {
try {
st = null;
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
try {
while (br.ready()) {
st = new StringTokenizer(br.readLine());
if (st != null && st.hasMoreTokens()) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
public class codeee {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n==1){System.out.println(1); return;}
int []mas=new int[n];
int sum=0;
for (int i = 0; i < n; i++) {
mas[i]=sc.nextInt();
sum+=mas[i];
}
Arrays.sort(mas);
int sum1=0;
int ans=0;
for(int i=0;i<n;i++){
sum1+=mas[n-i-1];
if(sum1>(sum-sum1)){
ans=i;
break;
}
}
System.out.println(ans+1);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author codeKNIGHT
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n=in.nextInt(),i,sum=0;
int a[]=new int[n];
for(i=0;i<n;i++) {
a[i]=in.nextInt();
sum+=a[i];
}
Arrays.sort(a);
int s=0,c=0;
for(i=n-1;i>=0;i--)
{
if(s>sum)
break;
s+=a[i];
sum-=a[i];
c++;
}
out.println(c);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main implements Runnable {
PrintWriter out;
BufferedReader in;
StringTokenizer st;
void solve() throws Exception {
int n = nextInt();
int[] a = new int[n];
int sum = 0;
for(int i = 0; i < n; ++i) {
a[i] = nextInt();
sum += a[i];
}
Arrays.sort(a);
int ans = 0, csum = 0;
for(int i = n - 1; csum <= sum - csum && i >= 0; i--) {
csum += a[i];
ans++;
}
out.println(ans);
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static public void main(String args[]) throws Exception {
(new Main()).run();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextToken() {
while(st == null || !st.hasMoreTokens()) {
try {
String line = in.readLine();
st = new StringTokenizer(line);
} catch(Exception e) {
return null;
}
}
return st.nextToken();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
public class Solution {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] a = new int [n];
int i;
int s = 0;
for (i = 0; i < n; i++) {
a[i] = scan.nextInt();
s += a[i];
}
Arrays.sort(a);
int sum = 0;
for (i = n - 1; i > -1; i--) {
sum += a[i];
if (s - sum < sum) {
System.out.println(n - i);
return;
}
}
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Test {
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
}
private static void solve() throws IOException {
int n=nextInt();
int[] a=new int[n];
int first=0;
for (int i=0;i<n;i++)
{
a[i]=nextInt();
first+=a[i];
}
Arrays.sort(a);
int ans=1;
int last=a[n-1];
first-=a[n-ans];
while (true)
{
if (first<last)
break;
ans++;
last+=a[n-ans];
first-=a[n-ans];
}
writer.print(ans);
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class AA implements Runnable {
public static void main(String[] args) {
new Thread(new AA()).run();
}
static class Utils {
private Utils() {
}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex,
int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++]
: rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
BufferedReader br;
StringTokenizer str = new StringTokenizer("");
PrintWriter pw;
public Integer ni() {
return Integer.parseInt(nextToken());
}
public Double nd() {
return Double.parseDouble(nextToken());
}
public Long nl() {
return Long.parseLong(nextToken());
}
public boolean EOF() {
try {
if (!br.ready() && !str.hasMoreTokens()) {
return true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public String nextToken() {
while (!str.hasMoreTokens())
try {
str = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str.nextToken();
}
@Override
public void run() {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
// try {
// br = new BufferedReader(new FileReader("input.txt"));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// try {
// pw = new PrintWriter(new FileWriter("output.txt"));
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
solve();
pw.close();
}
public void solve() {
int n = ni();
int[] a = new int[n];
int total = 0;
for (int i = 0; i < n; i++) {
a[i] = ni();
total+=a[i];
}
Arrays.sort(a);
int c =0;
int left=0;
for(int i=n-1; i>=0;i--){
if (left<=total){
c++;
left+=a[i];
total-=a[i];
}
}
pw.print(c);
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Round111A {
public static void main(String[] args) throws IOException {
new Round111A().run();
}
public void run() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner scanner = new Scanner(reader);
PrintWriter writer = new PrintWriter(System.out);
int n = scanner.nextInt();
int sum = 0;
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
sum += a[i];
}
Arrays.sort(a, Collections.reverseOrder());
int s = 0;
int i = 0;
while (i < n && (s <= sum / 2)) {
s += a[i];
i++;
}
writer.print(i);
scanner.close();
writer.close();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class C111A{
static BufferedReader br;
public static void main(String args[])throws Exception{
br=new BufferedReader(new InputStreamReader(System.in));
int n = toInt();
int nm[] = toIntArray();
double a=0.0;
double sum=0;
for(int i=0;i<n;i++){
sum+=nm[i];
}
a=sum/2;
Arrays.sort(nm);
int cur=0;
int count=0;
for(int i=nm.length-1;i>=0;i--){
cur+=nm[i];
count++;
if(cur>a){
break;
}
}
System.out.println(count);
}
/****************************************************************/
public static int[] toIntArray()throws Exception{
String str[]=br.readLine().split(" ");
int k[]=new int[str.length];
for(int i=0;i<str.length;i++){
k[i]=Integer.parseInt(str[i]);
}
return k;
}
public static int toInt()throws Exception{
return Integer.parseInt(br.readLine());
}
public static long[] toLongArray()throws Exception{
String str[]=br.readLine().split(" ");
long k[]=new long[str.length];
for(int i=0;i<str.length;i++){
k[i]=Long.parseLong(str[i]);
}
return k;
}
public static long toLong()throws Exception{
return Long.parseLong(br.readLine());
}
public static double[] toDoubleArray()throws Exception{
String str[]=br.readLine().split(" ");
double k[]=new double[str.length];
for(int i=0;i<str.length;i++){
k[i]=Double.parseDouble(str[i]);
}
return k;
}
public static double toDouble()throws Exception{
return Double.parseDouble(br.readLine());
}
public static String toStr()throws Exception{
return br.readLine();
}
public static String[] toStrArray()throws Exception{
String str[]=br.readLine().split(" ");
return str;
}
/****************************************************************/
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public A () throws IOException {
int N = sc.nextInt();
int [] A = new int [N];
for (int n = 0; n < N; ++n)
A[n] = sc.nextInt();
solve(N, A);
}
public void solve (int N, int [] A) {
//start();
Arrays.sort(A);
int S1 = 0;
for (int n = 0; n < N; ++n)
S1 += A[n];
int S2 = 0;
for (int n = N - 1; n >= 0; --n) {
S2 += A[n];
if (S2 > S1 - S2)
exit(N - n);
}
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static long t;
static void print (Object o) {
System.out.println(o);
}
static void exit (Object o) {
print(o);
//print2((millis() - t) / 1000.0);
System.exit(0);
}
static void run () throws IOException {
sc = new MyScanner ();
new A();
}
public static void main(String[] args) throws IOException {
run();
}
static long millis() {
return System.currentTimeMillis();
}
static void start() {
t = millis();
}
static class MyScanner {
String next() throws IOException {
newLine();
return line[index++];
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
String nextLine() throws IOException {
line = null;
return r.readLine();
}
//////////////////////////////////////////////
private final BufferedReader r;
MyScanner () throws IOException {
this(new BufferedReader(new InputStreamReader(System.in)));
}
MyScanner(BufferedReader r) throws IOException {
this.r = r;
newLine();
}
private String [] line;
private int index;
private void newLine() throws IOException {
if (line == null || index == line.length) {
line = r.readLine().split(" ");
index = 0;
}
}
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solver {
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) throws NumberFormatException, IOException {
Solver solver = new Solver();
solver.open();
solver.solve();
solver.close();
}
public void open() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
public String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public void solve() throws NumberFormatException, IOException {
int n = nextInt();
int[] ar = new int[n];
int sum = 0;
for (int i=0;i<n;i++){
ar[i]=nextInt();
sum+=ar[i];
}
Arrays.sort(ar);
int me = 0;
int k = 0;
while (me<=sum){
k++;
int coin = ar[ar.length-k];
me += coin;
sum -= coin;
}
out.println(k);
}
public void close() {
out.flush();
out.close();
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class A implements Runnable {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
tok = new StringTokenizer("");
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
System.err.println("Time = " + (endTime - startTime) + " ms");
System.err.println("Memory = " + ((totalMemory - freeMemory) / 1024) + " KB");
} catch (Throwable e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
void debug(Object... o) {
if (!ONLINE_JUDGE) {
System.err.println(Arrays.deepToString(o));
}
}
public static void main(String[] args) {
new Thread(null, new A(), "", 256 * 1024 * 1024).start();
}
static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
//------------------------------------------------------------------------------
void solve() throws IOException {
int n = readInt();
int[] a = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = readInt();
sum += a[i];
}
Utils.mergeSort(a);
int s = 0, c = 0;
for (int i = n-1; i >= 0; i--) {
s += a[i];
c++;
if (2 * s > sum) {
break;
}
}
out.println(c);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class con111_A {
public static void main( final String[] args ) throws IOException {
final BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
final int n = Integer.parseInt( br.readLine() );
final int[] a = new int[n];
final String[] parts = br.readLine().split( " " );
for ( int i = 0; i < n; ++i ) {
a[ i ] = Integer.parseInt( parts[ i ] );
}
System.out.println( solve( n, a ) );
}
private static int solve( final int n, final int[] a ) {
Arrays.sort( a );
int sum = 0;
for ( int i = 0; i < n; ++i ) {
sum += a[ i ];
}
int res = 0;
int ms = 0;
for ( int i = n - 1; i >= 0; --i ) {
if ( ms > sum / 2 ) {
break;
} else {
ms += a[ i ];
++res;
}
}
return res;
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Twins {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] val = new int[n];
for (int i=0; i<n; i++)
val[i] = in.nextInt();
Arrays.sort(val);
int sum = 0, count = 0;
for (int i=n-1; i>=0; i--) {
count++;
sum += val[i];
int his = 0;
for (int j=0; j<i; j++) his += val[j];
if (his < sum) break;
}
System.out.println(count);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Round111ProbA {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[]a = new int[n];
int s =0;
for(int i =0 ; i < n;i++)
{
a[i] = in.nextInt();
s += a[i];
}
Arrays.sort(a);
int x =0;
int c =0;
for(int i =n-1 ; i >-1;i-- )
{
x +=a[i];
s -= a[i];
c++;
if(x > s)break;
}
System.out.println(c);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.*;
import java.util.*;
import java.lang.Math.*;
public class A111
{
public static void main(String args[])throws Exception
{
Scanner in=new Scanner(System.in);
// br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int n,i,j,k=0,l;
n=in.nextInt();
int a[]=new int[n];
int sum=0,sum1=0;
for(i=0;i<n;i++)
{
a[i]=in.nextInt();
sum+=a[i];
}
Arrays.sort(a);
for(j=n-1;j>=0;j--)
{
sum1+=a[j];
k++;
if(sum1*2>sum)
break;
}
pw.println(k);
pw.flush();
}
} | nlogn | 160_A. Twins | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class round111A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] coins = new int [n];
for(int i = 0 ; i < n ; ++i)
coins[i] = sc.nextInt();
Arrays.sort(coins);
int ans = (int)1e9;
for(int i = 1 ; i <= n ; ++i){
int sum1 = 0;
int c = 0;
int j = n - 1;
for(j = n - 1 ; j >= 0 && c < i ; --j, ++c){
sum1 += coins[j];
}
int sum2 = 0;
for(int k = 0 ; k <= j ; ++k)
sum2 += coins[k];
if(sum1 > sum2){
System.out.println(i);
return;
}
}
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
public class TaskA {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
Arrays.sort(a);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
int take = 0, num = 0;
for (int i = n - 1; i > -1; i--) {
num++;
take += a[i];
sum -= a[i];
if (take > sum) {
break;
}
}
System.out.println(num);
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class nA {
Scanner in;
PrintWriter out;
void solve() {
int n = in.nextInt();
int a[] = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
sum+=a[i];
}
Arrays.sort(a);
int nowsum = 0;
int kol = 0;
for(int i = n - 1; i >= 0; i--){
if(nowsum <= sum / 2){
nowsum+=a[i];
kol++;
}else{
break;
}
}
out.println(kol);
}
void run() {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
try {
solve();
} finally {
out.close();
}
}
public static void main(String[] args) {
new nA().run();
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author Andrew Porokhin, [email protected]
*/
public class Problem111A implements Runnable {
void solve() throws NumberFormatException, IOException {
// TODO: Write your code here ...
final int n = nextInt();
final int[] a = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
final int nextInt = nextInt();
sum += nextInt;
a[i] = nextInt;
}
Arrays.sort(a);
int currSum = 0;
int maxCoins = 0;
for (int j = a.length - 1; j >= 0; j--) {
currSum += a[j];
maxCoins++;
if (sum - currSum < currSum) {
break;
}
}
System.out.println(maxCoins);
}
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) {
// Introduce thread in order to increase stack size
new Problem111A().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (Exception e) {
System.exit(9000);
} finally {
out.flush();
out.close();
}
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.awt.Point;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Testt {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Testt().run();
}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex,
int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++]
: rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
public void solve() throws IOException {
int n = readInt();
int [] a = new int [n];
for (int i = 0; i < n; i++){
a[i] = readInt();
}
mergeSort(a);
int sum = 0;
for (int i = 0; i <n; i++){
sum+=a[i];
}
int sum2 = 0;
int ans = 0;
for (int i = n-1; i >=0; i-- ){
sum2+=a[i];
sum-=a[i];
ans++;
if (sum2>sum){
break;
}
}
out.print(ans);
}
/* for (int i =0; i<n; i++){
out.print(a[i]+" ");
}*/
}
| nlogn | 160_A. Twins | CODEFORCES |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.Map.*;
public class codeforces {
static int count =0;
static boolean f=false;
static int [] arr;
static PrintWriter pw=new PrintWriter(System.out);
static void solve(int index , int mask) {
if(index==arr.length) {
int sum1=0; int sum2=0;
for(int i=0;i<arr.length;i++) {
if((mask & 1<<i)!=0) sum1+=arr[i];
}
return;
}
solve(index+1, mask | 1<<index);
solve(index+1, mask);
}
public static void main(String [] args) throws IOException, InterruptedException {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
pair [] arr=new pair[x];
for(int i=0;i<x;i++) arr[i]=new pair(i, sc.nextInt(),0);
for(int i=0;i<x;i++) arr[i].y=sc.nextInt();
Arrays.sort(arr);
PriorityQueue<Integer> qq=new PriorityQueue<>();
//pw.println(Arrays.toString(arr));
Long [] list=new Long [x];
long sum=0;
for(int i=0;i<x;i++) {
pair w=arr[i];
if(qq.size()<y) {
qq.add(w.y);
sum+=w.y;
list[w.i]=sum;
}else if(!qq.isEmpty()) {
sum+=w.y;
list[w.i]=sum;
int first=qq.poll();
if(w.y>first) {
sum-=first;
qq.add(w.y);
}else {
qq.add(first);
sum-=w.y;
}
} else list[w.i]=(long) w.y;
//pw.println(qq);
}
for(Long w:list) pw.print(w+" ");
pw.flush();
pw.close();
}
static class pair implements Comparable<pair>{
String name; int x,y,i ;
public pair(String name , int x) {
this.name=name; this.x=x;
}
public pair (int i,int x,int y) {
this.i=i; this.x=x; this.y=y;
}
public int compareTo(pair o) {
return x-o.x;
}
public int compareTo1(pair o) {
if(!name.equals(o.name))
return name.compareTo(o.name);
return x-o.x;
}
public String toString() {
return i+" "+x+" "+y;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | nlogn | 994_B. Knights of a Polygonal Table | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static void main(String[] args) {
int n=nextInt();
int m=nextInt();
long b[]=new long[n];
long g[]=new long[m];
for(int i=0;i<n;i++)
b[i]=nextInt();
for(int i=0;i<m;i++)
g[i]=nextInt();
Arrays.sort(b);
Arrays.sort(g);
if(b[n-1]>g[0])
System.out.println("-1");
else if(b[n-1]==g[0]){
long sum=0;
for(int i=0;i<m;i++)
sum+=g[i];
for(int i=0;i<n-1;i++){
sum+=(m*b[i]);
}
System.out.println(sum);
}else{
long sum=0;
for(int i=0;i<m;i++)
sum+=g[i];
sum+=b[n-1];
sum+=(b[n-2]*(m-1));
for(int i=0;i<n-2;i++){
sum+=(m*b[i]);
}
System.out.println(sum);
}
}
static int nextInt(){
try {
st.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (int)st.nval;
}
}
| nlogn | 1159_C. The Party and Sweets | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new PrintStream(System.out));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
long[] arrB = new long[n];
long[] arrG = new long[m];
st=new StringTokenizer(f.readLine());
for(int i=0;i<n;i++){
arrB[i]=Long.parseLong(st.nextToken());
}
st=new StringTokenizer(f.readLine());
for(int j=0;j<m;j++){
arrG[j]=Long.parseLong(st.nextToken());
}
Arrays.sort(arrB);
Arrays.sort(arrG);
long ans = 0;
// for (int i = 0; i < n; i++) ans += arrB[i] * m;
// for (int i = 0; i < m - 1; i++) ans += arrG[i] - arrB[0];
// if (arrB[m - 1] != arrB[0]) {
// if (arrB.length == 1) {
// ans=-1;
// }
// else ans += arrG[m - 1] - arrB[1];
// }
// if (arrG[m-1] < arrB[0]) {
// ans=-1;
// }
for(int i=0;i<n;i++){
ans+=arrB[i]*(long)m;
}
for(int i=1;i<m;i++){
ans+=arrG[i]-arrB[n-1];
}
if(arrB[n-1]!=arrG[0]){
if(n==1){
ans=-1;
}
else{
//smallest g goes to second to last
ans+=arrG[0]-arrB[n-2];
}
}
if(arrB[n-1]>arrG[0]){
ans=-1;
}
System.out.println(ans);
f.close();
out.close();
}
} | nlogn | 1159_C. The Party and Sweets | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws NumberFormatException, IOException {
String[] s = br.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
long b[] = new long[n];
s = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++) {
b[i] = Integer.parseInt(s[i]);
}
long g[] = new long[m];
s = br.readLine().trim().split(" ");
for(int i = 0; i < m; i++) {
g[i] = Integer.parseInt(s[i]);
}
Arrays.sort(b);
Arrays.sort(g);
if(g[0] < b[n-1]) {
System.out.println("-1");
}
else if(g[0] == b[n-1]){
long ans = 0;
for(int i = 0; i < m; i++) {
ans += g[i];
}
for(int i = 0; i < n-1; i++) {
ans += (m)*b[i];
}
System.out.println(ans);
}
else {
long ans = 0;
for(int i = 0; i < m; i++) {
ans += g[i];
}
for(int i = 0; i < n-1; i++) {
ans += (m)*b[i];
}
ans += b[n-1]-b[n-2];
System.out.println(ans);
}
}
} | nlogn | 1159_C. The Party and Sweets | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
public class Main2 {
private FastScanner scanner = new FastScanner();
public static void main(String[] args) {
new Main2().solve();
}
private void solve() {
int n = scanner.nextInt();
int a[][] = new int[n][3];
for (int i = 0; i < n; i++) {
a[i][0] = scanner.nextInt();
a[i][1] = scanner.nextInt();
a[i][2] = i;
}
int l = -1, r = -1;
Arrays.sort(a, (o1, o2) -> {
if (o1[0] != o2[0]) {
return o1[0] - o2[0];
} else {
return o2[1] - o1[1];
}
});
int maxr = -1, maxi = -1;
for (int i = 0; i < n; i++) {
if (a[i][1] <= maxr) {
l = a[i][2] + 1;
r = maxi + 1;
break;
}
if (a[i][1] > maxr) {
maxi = a[i][2];
maxr = a[i][1];
}
}
System.out.println(l + " " + r);
}
boolean check(int cnt[][], int[] tcnt, int mid) {
boolean ok = true;
for (int j = 0; j < 27; j++) {
if (cnt[mid][j] < tcnt[j]) {
ok = false;
}
}
return ok;
}
class Pair {
int c, f;
}
class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
Integer[] nextA(int n) {
Integer a[] = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} | nlogn | 976_C. Nested Segments | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
team[] t = new team[n];
for (int i = 0; i < n; i++)
t[i] = new team(in.nextInt(), in.nextInt());
Arrays.sort(t);
int cnt = 0;
team tm = t[t.length - k];
for (int i = t.length - 1; i >= 0; i--)
if (tm.equal(t[i]))
cnt++;
System.out.println(cnt);
}
static class team implements Comparable<team> {
int p, t;
public team(int pp, int tt) {
p = pp;
t = tt;
}
@Override
public int compareTo(team o) {
if (p == o.p)
return o.t - t;
return p - o.p;
}
public boolean equal(team a) {
return a.p == p && a.t == t;
}
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.awt.Point;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt() - 1;
Point[] A = new Point[n];
for (int i = 0; i < n; i++)
A[i] = new Point(in.nextInt(), in.nextInt());
Arrays.sort(A, new Comparator<Point>() {
public int compare(Point o1, Point o2) {
if (o1.x != o2.x)
return o2.x - o1.x;
if (o1.y != o2.y)
return o1.y - o2.y;
return 0;
}
});
int i = k;
int j = k;
while (i >= 0 && A[i].x == A[k].x && A[i].y == A[k].y)
i--;
while (j < n && A[j].x == A[k].x && A[j].y == A[k].y)
j++;
System.out.println(j - i - 1);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class CF113_Div2_A implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
final boolean ONLINE_JUDGE = (System.getProperty("ONLINE_JUDGE") != null);
public static void main(String[] args) {
new Thread(null, new CF113_Div2_A(), "", 256 * (1L << 20)).start();
}
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
Locale.setDefault(Locale.US);
tok = new StringTokenizer("");
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
System.err.println("Time = " + (endTime - startTime));
long freeMemory = Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
System.err.println("Memory = " + ((totalMemory - freeMemory) >> 10));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
/** http://pastebin.com/j0xdUjDn */
static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static final int MAGIC_VALUE = 50;
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
void debug(Object... o) {
if (!ONLINE_JUDGE) {
System.err.println(Arrays.deepToString(o));
}
}
// solution
class Team implements Comparable<Team>{
int cnt, time;
public Team(int cnt, int time) {
this.cnt = cnt;
this.time = time;
}
@Override
public int compareTo(Team x) {
if (cnt == x.cnt) return time - x.time;
return x.cnt - cnt;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + cnt;
result = prime * result + time;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Team other = (Team) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (cnt != other.cnt)
return false;
if (time != other.time)
return false;
return true;
}
private CF113_Div2_A getOuterType() {
return CF113_Div2_A.this;
}
}
void solve() throws IOException {
int n = readInt();
int k = readInt();
k--;
Team[] a = new Team[n];
for (int i =0 ; i < n; i++) {
a[i] = new Team(readInt(), readInt());
}
Arrays.sort(a);
int res = 1;
for (int i = k-1; i >= 0; i--) {
if (a[k].equals(a[i])) res++;
}
for (int i = k+1; i < n; i++) {
if (a[k].equals(a[i])) res++;
}
out.print(res);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Scanner;
public class R113A {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
int[] ind = new int[n];
int[] p = new int[n];
int[] t = new int[n];
for (int i = 0; i < n; i++){
ind[i] = i;
p[i] = in.nextInt();
t[i] = in.nextInt();
}
//System.out.println("erwer");
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++){
if (p[i] < p[j] || (p[i] == p[j] && t[i] > t[j])){
int tmp = p[i];
p[i] = p[j];
p[j] = tmp;
tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
int i = k - 1;
// System.out.println(i+" "+p[i]);
while (i > 0 && p[i] == p[i - 1] && t[i] == t[i - 1]) i--;
// System.out.println(i);
int j = 0;
while (i < n - 1 && p[i] == p[i + 1] && t[i] == t[i + 1]) {
i++;
j++;
}
// System.out.println(i);
System.out.println(j + 1);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
FastScanner in = new FastScanner(System.in);
// FastScanner in = new FastScanner(new File("test.txt"));
PrintWriter out = new PrintWriter(System.out);
public static void main (String[]args) {
Main task = new Main();
task.solve();
task.close();
}
public void close () {
in.close();
out.close();
}
public void solve() {
int n = in.nextInt();
int k = in.nextInt();
Team[]teams = new Team[n];
for (int i = 0; i < n; i++) {
Team t = new Team();
t.tasks = in.nextInt();
t.penalty = in.nextInt();
teams[i] = t;
}
Arrays.sort(teams);
Team t = teams[k - 1];
int ans = 0;
for (int i = 0; i < teams.length; i++) {
if (teams[i].equals(t)) ans++;
}
System.out.println(ans);
}
class Team implements Comparable<Team>{
int tasks;
int penalty;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + penalty;
result = prime * result + tasks;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Team other = (Team) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (penalty != other.penalty)
return false;
if (tasks != other.tasks)
return false;
return true;
}
@Override
public int compareTo(Team o) {
if (this.tasks > o.tasks) return -1;
else if (this.tasks == o.tasks) {
if (this.penalty <= o.penalty) return -1;
else return 1;
}
else return 1;
}
private Main getOuterType() {
return Main.this;
}
}
public int max (int a, int b) {
if (a > b) return a;
else return b;
}
}
class Algebra {
/****
* Number of co-prime numbers on [1, n].
* Number a is Co-prime if gcd (a, n) == 1
* O (sqrt(n))
****/
public static int phi(int n) {
int result = n;
for (int i = 2; i*i <= n; ++i) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
/****
* Raise number a to power of n.
* O (log n)
****/
public static int binpow (int a, int n) {
int res = 1;
while (n != 0) {
if ((n & 1) == 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
/****
* Finding the greatest common divisor of two numbers.
* O (log min(a, b))
****/
public static int gcd (int a, int b) {
return (b != 0) ? gcd (b, a % b) : a;
}
/****
* Finding the lowest common multiple of two numbers.
* O (log min(a, b))
****/
public static int lcm (int a, int b) {
return a / gcd (a, b) * b;
}
/****
* Eratosthenes Sieve of numbers - [0..n]. True - simple, False - not simple.
* O (n log log n)
****/
public static boolean[] sieveOfEratosthenes (int n) {
boolean [] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i=2; i<=n; ++i) {
if (prime[i]) {
if (i * 1L * i <= n) {
for (int j=i*i; j<=n; j+=i) {
prime[j] = false;
}
}
}
}
return prime;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
System.err.println(e);
return "";
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
BigInteger nextBigInt() {
return new BigInteger(next());
}
void close() {
try {
br.close();
}
catch (IOException e) {
}
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.security.SecureRandom;
import java.util.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
class Pair implements Comparable<Pair> {
int col;
int time;
int place;
public Pair() {
}
public Pair(int col, int time) {
this.col = col;
this.time = time;
}
@Override
public int compareTo(Pair arg0) {
if (col == arg0.col) {
return time - arg0.time;
}
return arg0.col - col;
}
}
public void solve() throws Exception {
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Pair> a = new ArrayList<Pair>();
for (int i = 0;i < n; ++ i) {
a.add(new Pair(sc.nextInt(), sc.nextInt()));
}
Collections.sort(a);
int place = 1;
int placex = 0;
int ans2 = 0;
int ans1 = 0;
for (int i = 0;i < n; ++ i) {
if (i > 0 && a.get(i).compareTo(a.get(i - 1)) != 0) {
++ place;
++ placex;
} else {
++ placex;
}
a.get(i).place = place;
if (placex == k) {
ans1 = place;
}
}
for (int i = 0;i < n; ++ i) {
if (a.get(i).place == ans1) {
++ ans2;
}
}
out.println(ans2);
}
/*--------------------------------------------------------------*/
static String filename = "";
static boolean fromFile = false;
BufferedReader in;
PrintWriter out;
FastScanner sc;
public static void main(String[] args) {
new Thread(null, new Solution(), "", 1 << 25).start();
}
public void run() {
try {
init();
solve();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
out.close();
}
}
void init() throws Exception {
if (fromFile) {
in = new BufferedReader(new FileReader(filename+".in"));
out = new PrintWriter(new FileWriter(filename+".out"));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
sc = new FastScanner(in);
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
public FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws IOException {
while (strTok == null || !strTok.hasMoreTokens()) {
strTok = new StringTokenizer(reader.readLine());
}
return strTok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
public BigDecimal nextBigDecimal() throws IOException {
return new BigDecimal(nextToken());
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution
{
public static void main(String[] args) throws IOException
{
new Solution().run();
}
StreamTokenizer in;
Scanner ins;
PrintWriter out;
int nextInt() throws IOException
{
in.nextToken();
return (int)in.nval;
}
void run() throws IOException
{
if(System.getProperty("ONLINE_JUDGE")!=null)
{
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
ins = new Scanner(System.in);
out = new PrintWriter(System.out);
}
else
{
in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
ins = new Scanner(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"));
}
int n = nextInt(),k = nextInt();
Team[] A = new Team[n];
for(int i = 0; i < n; i++)
{
A[i] = new Team();
A[i].p = nextInt(); A[i].t = nextInt();
}
Arrays.sort(A);
k--;
int sum = 0;
for(int i = 0; i < n; i++)
if(A[k].compareTo(A[i])==0)
sum++;
out.print(sum);
out.close();
}
class Team implements Comparable
{
public int p,t;
public int compareTo(Object obj)
{
Team a = (Team) obj;
if(p>a.p || p==a.p && t<a.t)
return -1;
else
if(p==a.p && t==a.t)
return 0;
else
return 1;
}
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ProblemA {
private final BufferedReader in;
private final PrintStream out;
private StringTokenizer tok = new StringTokenizer("");
private String nextLine = null;
public static void main(String[] args) throws Exception {
new ProblemA();
}
private ProblemA() throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = System.out;
start();
end();
}
private int nextInt() {
return Integer.parseInt(nextWord());
}
private String nextWord() {
if (tok.hasMoreTokens()) {
return tok.nextToken();
} else {
while (!tok.hasMoreTokens()) {
try {
nextLine = in.readLine();
if (nextLine == null) {
return null;
} else {
tok = new StringTokenizer(nextLine);
}
} catch (IOException ex) {
Logger.getLogger(ProblemA.class.getName()).log(Level.SEVERE, null, ex);
}
}
return tok.nextToken();
}
}
private void start() {
int n = nextInt();
int k = nextInt();
T[] ts = new T[n];
for (int i = 0; i < n; i++) {
ts[i] = new T(nextInt(), nextInt());
}
Arrays.sort(ts, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
if (o1.p > o2.p) {
return -1;
}
if (o1.p < o2.p) {
return 1;
}
if (o1.t < o2.t) {
return -1;
}
if (o1.t > o2.t) {
return 1;
}
return 0;
}
});
int t = ts[k - 1].t;
int p = ts[k - 1].p;
int res = 0;
for (int i = 0; i < n; i++) {
if (ts[i].p == p && ts[i].t == t) {
res++;
}
}
out.println(res);
}
class T {
int p;
int t;
public T(int p, int t) {
this.p = p;
this.t = t;
}
}
private void end() {
out.close();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer=null;
public static void main(String[] args) throws IOException
{
new Main().execute();
}
void debug(Object...os)
{
System.out.println(Arrays.deepToString(os));
}
int ni() throws IOException
{
return Integer.parseInt(ns());
}
long nl() throws IOException
{
return Long.parseLong(ns());
}
double nd() throws IOException
{
return Double.parseDouble(ns());
}
String ns() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(br.readLine());
return tokenizer.nextToken();
}
String nline() throws IOException
{
tokenizer=null;
return br.readLine();
}
//Main Code starts Here
int totalCases, testNum;
int k,n;
void execute() throws IOException
{
totalCases = 1;
for(testNum = 1; testNum <= totalCases; testNum++)
{
input();
solve();
}
}
void solve()
{
int a = arr[k-1].a;
int b = arr[k-1].b;
//debug(a,b);
int count = 0;
for(int i = 0;i<n;i++)
{
if(arr[i].a == a && arr[i].b == b)
count++;
}
System.out.println(count);
}
void printarr(int [] a,int b)
{
for(int i = 0;i<=b;i++)
{
if(i==0)
System.out.print(a[i]);
else
System.out.print(" "+a[i]);
}
System.out.println();
}
class Pair implements Comparable<Pair>
{
int a,b;
Pair(int _a,int _b)
{
a=_a;
b=_b;
}
public int compareTo(Pair x)
{
if(a == x.a) return b-x.b;
return -(a-x.a);
}
public boolean equals(Pair x)
{
return a==x.a && b==x.b;
}
}
Pair[] arr;
boolean input() throws IOException
{
n = ni();
k = ni();
arr = new Pair[n];
for(int i = 0 ;i<n;i++)
{
Pair p =new Pair(ni(),ni());
arr[i] = p;
}
Arrays.sort(arr);
//debug(arr);
return true;
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.awt.Point;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Start {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Start().run();
}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int levtIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (levtIndex < rightIndex) {
if (rightIndex - levtIndex <= MAGIC_VALUE) {
insertionSort(a, levtIndex, rightIndex);
} else {
int middleIndex = (levtIndex + rightIndex) / 2;
mergeSort(a, levtIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, levtIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int levtIndex, int middleIndex,
int rightIndex) {
int length1 = middleIndex - levtIndex + 1;
int length2 = rightIndex - middleIndex;
int[] levtArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, levtIndex, levtArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = levtIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = levtArray[i++];
} else {
a[k] = levtArray[i] <= rightArray[j] ? levtArray[i++]
: rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int levtIndex, int rightIndex) {
for (int i = levtIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= levtIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
class Lol implements Comparable<Lol>{
int x;
int y;
public Lol (int x , int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Lol arg0) {
if (arg0.x == x) {
return y-arg0.y;
}
return arg0.x-x;
}
}
public void solve() throws IOException {
int n = readInt();
int k = readInt();
k--;
Lol [] a = new Lol [n];
for (int i = 0 ; i <n; i++){
int x = readInt();
int y = readInt();
a[i] = new Lol(x, y);
}
Arrays.sort(a);
int ans = 1;
for (int i =k+1; i>-1; i++){
if (i==n) break;
if (a[i].x==a[k].x && a[i].y == a[k].y){
ans++;
}
else break;
}
if (k!=0){
for (int i =k-1; i>=0; i--){
if (a[i].x==a[k].x && a[i].y == a[k].y){
ans++;
}
else break;
}
}
out.print(ans);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
public class C{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n=in.nextInt(),key=in.nextInt(),ans=0;
int[] a = new int[101], b = new int[101];
for (int i=1;i<=n;i++) {a[i]=in.nextInt();b[i]=in.nextInt();}
for (int i=1;i<n;i++)
for (int j=i+1;j<=n;j++)
if (a[i]<a[j] || (a[i]==a[j] && b[i]>b[j])) {
int yed = a[i];a[i]=a[j]; a[j]=yed;
yed = b[i];b[i]=b[j];b[j]=yed;
}
int k=0;
for (int i=1;i<=n;i++) {
if (a[i]==a[i-1] && b[i]==b[i-1]) k++; else
{if (i>key && ans==0) ans = k;k=1;}
}
if (ans == 0) ans = k;
System.out.println(ans);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int N = r.nextInt();
int K = r.nextInt() - 1;
T[] a = new T[N];
for(int i = 0; i < N; i++)
a[i] = new T(r.nextInt(), r.nextInt());
Arrays.sort(a, new Comparator<T>() {
@Override
public int compare(T x, T y) {
if(x.p > y.p)return -1;
else if(x.p == y.p){
if(x.t < y.t)return -1;
else if(x.t == y.t)return 0;
else return 1;
}else return 1;
}
});
int ret = 0;
for(int i = 0; i < N; i++)
if(a[i].p == a[K].p && a[i].t == a[K].t)ret++;
System.out.println(ret);
}
}
class T{
int p, t;
public T(int pi, int ti){
p = pi;
t = ti;
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class ProblemA {
public static class Team {
int solved;
int penalty;
Team(int s, int p) {
solved = s;
penalty = p;
}
}
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
String[] data = s.readLine().split(" ");
int n = Integer.valueOf(data[0]);
int k = Integer.valueOf(data[1]);
Team[] t = new Team[n];
for (int i = 0 ; i < n ; i++) {
String[] line = s.readLine().split(" ");
t[i] = new Team(Integer.valueOf(line[0]), Integer.valueOf(line[1]));
}
Arrays.sort(t, new Comparator<Team>(){
public int compare(Team arg0, Team arg1) {
if (arg0.solved != arg1.solved) {
return arg1.solved - arg0.solved;
}
return arg0.penalty - arg1.penalty;
}
});
int idx = k - 1;
int ksol = t[idx].solved;
int kpen = t[idx].penalty;
int count = 0;
for (int i = 0 ; i < n ; i++) {
if (t[i].solved == ksol && t[i].penalty == kpen) {
count++;
}
}
System.out.println(count);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
public class A
{
static class T
{
public int s,p;
}
public static void main(String args[]) throws Exception
{
InputReader sc=new InputReader(System.in);
int n=sc.readInt(),k=sc.readInt(),i,j,z;
T m[]=new T[n];
for(i=0;i<n;i++) {m[i]=new T();m[i].s=sc.readInt();m[i].p=sc.readInt();}
for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(m[i].s<m[j].s){z=m[i].s;m[i].s=m[j].s;m[j].s=z;z=m[i].p;m[i].p=m[j].p;m[j].p=z;}
for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(m[i].s==m[j].s&&m[i].p>m[j].p){z=m[i].s;m[i].s=m[j].s;m[j].s=z;z=m[i].p;m[i].p=m[j].p;m[j].p=z;}
k--;int s=m[k].s,p=m[k].p,res=0;
for(i=0;i<n;i++){if(m[i].s==s&&m[i].p==p)res++;}
System.out.println(res);
}
}
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public 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++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
public class AA {
static class O implements Comparable<O> {
int problems;
int penalty;
public O(int p, int pp) {
problems = p;
penalty = pp;
}
public int compareTo(O arg0) {
if (problems == arg0.problems) {
return penalty - arg0.penalty;
}
return -(problems - arg0.problems);
}
}
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
String[] sp = s.split("[ ]+");
int n = new Integer(sp[0]), k = new Integer(sp[1]) - 1;
O[] arr = new O[n];
for (int i = 0; i < arr.length; i++) {
s = r.readLine();
sp = s.split("[ ]+");
arr[i] = new O(new Integer(sp[0]), new Integer(sp[1]));
}
Arrays.sort(arr);
int res = 1;
int i = k + 1;
while (i < arr.length && arr[i].problems == arr[k].problems
&& arr[i].penalty == arr[k].penalty) {
i++;
res++;
}
i = k - 1;
while (i >= 0 && arr[i].problems == arr[k].problems
&& arr[i].penalty == arr[k].penalty) {
i--;
res++;
}
System.out.println(res);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
public class R113_D2_A {
public static void main(String[] args) throws NumberFormatException,
IOException {
// BufferedReader in = new BufferedReader(new
// InputStreamReader(System.in));
InputReader4 in = new InputReader4(System.in);
int n = in.readInt();
int k = in.readInt();
p[] inp = new p[n];
for (int i = 0; i < inp.length; i++) {
inp[i] = new p(in.readInt(), in.readInt());
}
Arrays.sort(inp);
for (int i = 0; i < inp.length;) {
int j = i + 1;
while (j < inp.length && inp[i].x == inp[j].x
&& inp[i].y == inp[j].y) {
j++;
}
int num = j - i;
if (k <= num) {
System.out.println(num);
return;
} else
k -= num;
i = j;
}
}
static class p implements Comparable<p> {
int x;
int y;
public p(int a, int b) {
x = a;
y = b;
}
public int compareTo(p o) {
if (x > o.x)
return -1;
if (x < o.x)
return 1;
return y - o.y;
}
}
static class InputReader4 {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader4(InputStream stream) {
this.stream = stream;
}
private 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++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
public class A
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
String FInput="";
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
catch (NullPointerException e)
{
line=null;
}
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
long NextLong()
{
String n = inputParser.nextToken();
long val = Long.parseLong(n);
//System.out.println("I read this number: " + val);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public static void main(String [] argv)
{
String filePath=null;
if(argv.length>0)filePath=argv[0];
new A(filePath);
}
public void readFInput()
{
for(;;)
{
try
{
readNextLine();
FInput+=line+" ";
}
catch(Exception e)
{
break;
}
}
inputParser = new StringTokenizer(FInput, " ");
}
public A(String inputFile)
{
openInput(inputFile);
readNextLine();
int n=NextInt();
int k=NextInt()-1;
int ret=0;
Team [] t = new Team[n];
for(int i=0; i<n; i++)
{
readNextLine();
t[i]=new Team(NextInt(), NextInt());
}
Arrays.sort(t);
int ti=t[k].t, p=t[k].p;
for(int i=0; i<n; i++)
if(t[i].t==ti&&t[i].p==p)ret++;
System.out.print(ret);
closeInput();
}
private class Team implements Comparable<Team>
{
int p,t;
Team(int p, int t)
{
this.p=p;
this.t=t;
}
public int compareTo(Team d)
{
return 10000*(d.p-p)+t-d.t;
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class C113A{
static BufferedReader br;
public static void main(String args[])throws Exception{
br=new BufferedReader(new InputStreamReader(System.in));
int nm[]=toIntArray();
int n=nm[0];
int k=nm[1];
Pai p[]=new Pai[n];
for(int i=0;i<n;i++){
nm=toIntArray();
p[i]=new Pai(nm[0],nm[1]);
}
Arrays.sort(p);
int count=0;
for(int i=0;i<n;i++){
if(p[k-1].first==p[i].first && p[k-1].second==p[i].second){
count++;
}
}
System.out.println(count);
}
/****************************************************************/
public static int[] toIntArray()throws Exception{
String str[]=br.readLine().split(" ");
int k[]=new int[str.length];
for(int i=0;i<str.length;i++){
k[i]=Integer.parseInt(str[i]);
}
return k;
}
public static int toInt()throws Exception{
return Integer.parseInt(br.readLine());
}
public static long[] toLongArray()throws Exception{
String str[]=br.readLine().split(" ");
long k[]=new long[str.length];
for(int i=0;i<str.length;i++){
k[i]=Long.parseLong(str[i]);
}
return k;
}
public static long toLong()throws Exception{
return Long.parseLong(br.readLine());
}
public static double[] toDoubleArray()throws Exception{
String str[]=br.readLine().split(" ");
double k[]=new double[str.length];
for(int i=0;i<str.length;i++){
k[i]=Double.parseDouble(str[i]);
}
return k;
}
public static double toDouble()throws Exception{
return Double.parseDouble(br.readLine());
}
public static String toStr()throws Exception{
return br.readLine();
}
public static String[] toStrArray()throws Exception{
String str[]=br.readLine().split(" ");
return str;
}
/****************************************************************/
}
class Pai implements Comparable<Pai> {
int first;
int second;
public Pai(int f, int s) {
first = f;
second = s;
}
public int compareTo(Pai p) {
int ret = 0;
if (first < p.first) {
ret = 1;
} else if (first > p.first) {
ret = -1;
} else {
if(second<p.second){
ret=-1;
}
else if(second>p.second){
ret=1;
}
else ret=0;
}
return ret;
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
class Team implements Comparable<Team>{
int ac;
int penalty;
public Team(int ac, int penalty) {
this.ac = ac;
this.penalty = penalty;
}
@Override
public int compareTo(Team o) {
if (ac != o.ac)
return ac > o.ac ? -1 : 1;
return (penalty == o.penalty) ? 0 : (penalty < o.penalty ? -1 : 1);
}
}
void solve() throws IOException {
int n = nextInt();
int k = nextInt() - 1;
Team[] a = new Team[n];
for (int i = 0; i < n; i++)
a[i] = new Team(nextInt(), nextInt());
Arrays.sort(a);
for (int i = 0; i < n;) {
int j = i;
while (j < n && a[j].compareTo(a[i]) == 0)
j++;
if (i <= k && k < j) {
out.println(j - i);
return;
}
i = j;
}
}
void inp() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new A().inp();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
import java.awt.Point;
import static java.lang.Math.*;
public class P166A {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
Point[] P = new Point[n];
for(int i=0; i<n; i++)
P[i] = new Point(in.nextInt(), in.nextInt());
Arrays.sort(P, new Comparator<Point>() {
public int compare(Point A, Point B) {
if(A.x != B.x) return B.x-A.x;
return A.y - B.y;
}
});
int cnt = 0;
Point ans = P[k-1];
for(int i=0; i<n; i++) {
if(P[i].x == ans.x && P[i].y==ans.y)
cnt++;
}
System.out.println(cnt);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] argv) {
new Main().run();
}
void run() {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
try {
solve();
} finally {
out.close();
}
}
PrintWriter out;
Scanner in;
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
int[] readArr(int size) {
int[] a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = in.nextInt();
}
return a;
}
void solve() {
int n = in.nextInt();
int k = in.nextInt();
Pair[] a = new Pair[n];
for (int i = 0; i < n; i++) {
a[i] = new Pair(in.nextInt(), in.nextInt());
}
Arrays.sort(a, new Comparator<Pair>() {
@Override
public int compare(Pair p1, Pair p2) {
if (p2.x != p1.x) {
return p2.x - p1.x;
}
return p1.y - p2.y;
}
});
int cnt = 1;
int ans = 0;
int[] res = new int[n];
res[0] = 1;
for (int i = 1; i < n; i++) {
if (!(a[i].x == a[i - 1].x && a[i].y == a[i - 1].y)) {
cnt++;
}
res[i] = cnt;
//out.println(a[i] + " * " + cnt);
}
int el = res[k - 1];
for (int i = 0; i < n; i++) {
if (res[i] == el) {
ans++;
}
}
out.println(ans);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class A {
static StreamTokenizer st;
static PrintWriter pw;
static class Sort implements Comparable<Sort> {
int x,y;
public int compareTo(Sort arg0) {
if (this.x==arg0.x)
return this.y-arg0.y;
return -(this.x-arg0.x);
}
}
public static void main(String[] args) throws IOException{
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int k = nextInt();
Sort[]a = new Sort[n+1];
for (int i = 1; i <= n; i++) {
a[i] = new Sort();
a[i].x = nextInt();
a[i].y = nextInt();
}
Arrays.sort(a,1, n+1);
// for (int i = 1; i <= n; i++) {
// System.out.println(a[i].x+" "+a[i].y);
// }
// int plase = 1;
// if (k==1) {
// int ans = 0;
// for (int j = 1; j <= n; j++) {
// if (a[j].x==a[1].x && a[j].y ==a[1].y) {
// ans++;
// }
// }
// System.out.println(ans);
// return;
// }
// for (int i = 2; i <= n; i++) {
// if (a[i].x==a[i-1].x && a[i].y==a[i-1].y) {
//
// }
// else {
// plase++;
// if (plase==k) {
// int ans = 0;
// for (int j = 1; j <= n; j++) {
// if (a[j].x==a[i].x && a[j].y ==a[i].y) {
// ans++;
// }
// }
// System.out.println(ans);
// return;
// }
// }
// }
int ans = 0;
for (int i = 1; i <= n; i++) {
if (a[i].x==a[k].x && a[i].y==a[k].y)
ans++;
}
System.out.println(ans);
pw.close();
}
private static int nextInt() throws IOException{
st.nextToken();
return (int) st.nval;
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class A {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt()-1;
team[] t = new team[n];
for(int i = 0 ;i < n ; i++)
t[i] = new team(scan.nextInt(), scan.nextInt());
Arrays.sort(t);
int a =0;
int b = 0;
while(k+a < t.length-1 && t[k+a+1].compareTo(t[k]) == 0)
a++;
while(k-b > 0 && t[k-b-1].compareTo(t[k]) == 0)
b++;
System.out.println(a+b+1);
}
}
class team implements Comparable<team>
{
int p;
int t;
public team(int pp , int tt)
{
p = pp;
t= tt;
}
@Override
public String toString()
{
return p+" "+t;
}
@Override
public int compareTo(team e)
{
int a = e.p-p;
if(a == 0)
{
return t-e.t;
}else
return a;
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt() - 1 ;
int a[][] = new int[n][2];
for (int i = 0;i <n; i++) {
a[i][0]=input.nextInt();
a[i][1]=input.nextInt();
}
for (int i = 0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (a[i][0]<a[j][0]) {
int x=a[i][0];
int y=a[i][1];
a[i][0]=a[j][0];
a[i][1]=a[j][1];
a[j][0]=x;
a[j][1]=y;
}
}
}
for (int i = 0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if ((a[i][1]>a[j][1])&&(a[i][0]==a[j][0])) {
int x=a[i][0];
int y=a[i][1];
a[i][0]=a[j][0];
a[i][1]=a[j][1];
a[j][0]=x;
a[j][1]=y;
}
}
}
int x = a[k][0];
int y = a[k][1];
int s = 0;
for (int i = 0; i<n; i++) {
if ((a[i][0]==x)&&(a[i][1]==y)) {
s++;
}
}
System.out.println(s);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
//package codeforces;
import java.util.*;
public class Main {
class team implements Comparable<team>{
int pro,time;
public int compareTo(team oth) {
if(pro>oth.pro)
return -1;
if(pro==oth.pro&&time<oth.time)
return -1;
// TODO Auto-generated method stub
return 1;
}
}
Scanner scan=new Scanner(System.in);
void run(){
int n=scan.nextInt();
int k=scan.nextInt()-1;
team tm[]=new team[n];
for(int i=0;i<n;i++){
tm[i]=new team();
tm[i].pro=scan.nextInt();
tm[i].time=scan.nextInt();
}
Arrays.sort(tm);
int sum=0;
for(int i=k;i>=0;i--)
if(tm[i].pro==tm[k].pro&&tm[i].time==tm[k].time)
sum++;
for(int i=k;i<n;i++)
if(tm[i].pro==tm[k].pro&&tm[i].time==tm[k].time)
sum++;
System.out.println(sum-1);
}
public static void main(String args[]) {
new Main().run();
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
public class Main {
private void solve() {
int n = in.nextInt();
int k = in.nextInt();
final int[] p = new int[n];
final int[] t = new int[n];
for(int i =0 ; i < n; ++i) {
p[i] = in.nextInt();
t[i] = in.nextInt();
}
Integer[] ord = new Integer[n];
for(int i = 0; i < n; ++i)
ord[i] = i;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n - 1; ++j) {
if (Less(ord[j], ord[j + 1], p, t)) {
Integer tx = ord[j];
ord[j] = ord[j + 1];
ord[j + 1] = tx;
}
}
}
for(int i = 0, j = 0; i < n; i = j) {
for(j = i; j < n && p[ord[i]] == p[ord[j]] && t[ord[i]] == t[ord[j]]; ++j) ;
int first = i+1;
int second = j;
if (first <= k && k <= second) {
out.print(j - i);
return ;
}
}
out.print(0);
}
private boolean Less(Integer i, Integer j, int[] p, int[] t) {
return p[i] < p[j] || p[i] == p[j] && t[i] > t[j];
}
private void run() {
try {
in = new FastScanner();
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main().run();
}
FastScanner in;
PrintWriter out;
class FastScanner {
public BufferedReader reader;
private StringTokenizer tokenizer;
public FastScanner(String file) {
try {
reader = new BufferedReader(new FileReader(new File(file)));
tokenizer = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.StringTokenizer;
public class A {
private class Pair {
public final int prob;
public final int time;
public Pair(int prob, int time) {
this.prob = prob;
this.time = time;
}
}
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
Pair[] p = new Pair[n];
for (int i = 0; i < n; i++) {
p[i] = new Pair(nextInt(), nextInt());
}
Arrays.sort(p, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
if (o1.prob == o2.prob) {
return o1.time - o2.time;
}
return o2.prob - o1.prob;
}
});
int time = p[k - 1].time;
int prob = p[k - 1].prob;
int res = 0;
for (int i = 0; i < n; i++) {
if (p[i].time == time && p[i].prob == prob) {
res++;
}
}
println(res);
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
private double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private void print(Object o) {
writer.print(o);
}
private void println(Object o) {
writer.println(o);
}
private void printf(String format, Object... o) {
writer.printf(format, o);
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
Locale.setDefault(Locale.US);
new A().run();
System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(13);
}
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Round113_A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt() - 1;
Obe[] a = new Obe[n];
for (int i = 0; i < n; i++)
a[i] = new Obe(in.nextInt(), in.nextInt());
Arrays.sort(a);
int c = 0;
int p = 0, d = 0;
if (k > -1 && k < n) {
c = 1;
p = a[k].p;
d = a[k].d;
} else {
System.out.println(c);
return;
}
for (int i = k + 1; i < n; i++) {
if (a[i].p == p && a[i].d == d)
c++;
}
for (int i = k - 1; i > -1; i--) {
if (a[i].p == p && a[i].d == d)
c++;
}
System.out.println(c);
}
}
class Obe implements Comparable<Obe> {
int p, d;
public Obe(int pe, int de) {
p = pe;
d = de;
}
@Override
public int compareTo(Obe o) {
int x = new Integer(o.p).compareTo(this.p);
if (x != 0)
return x;
return new Integer(this.d).compareTo(o.d);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.List;
import java.util.Scanner;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author @zhendeaini6001
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
class Pair{
public int a;
public int b;
public Pair(int a, int b) {
// TODO Auto-generated constructor stub
this.a = a;
this.b = b;
}
}
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt(); --k;
ArrayList<Pair> list = new ArrayList<Pair>();
for (int i = 1; i <= n; ++i){
int num = in.nextInt();
int pen = in.nextInt();
Pair t = new Pair(num, pen);
list.add(t);
}
Collections.sort(list, new Comparator<Pair>(){
public int compare(Pair o1, Pair o2){
if (o1.a != o2.a){
return (o2.a - o1.a);
}
return (o1.b - o2.b);
}
});
int res = 1;
Pair compare = list.get(k);
int i = k - 1;
while (i >= 0){
Pair t = list.get(i);
if (t.a == compare.a && t.b == compare.b){
--i;
++res;
continue;
}else{
break;
}
}
i = k + 1;
while (i < list.size()){
Pair t = list.get(i);
if (t.a == compare.a && t.b == compare.b){
++res; ++i;
continue;
}else{
break;
}
}
out.println(res);
return;
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class P113A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintStream out = System.out;
//CODING TAIM
int n = sc.nextInt();
int k = sc.nextInt();
List<Team> l = new ArrayList<Team>();
for (int i = 0; i < n; i++) {
l.add(new Team(sc.nextInt(), sc.nextInt()));
}
Collections.sort(l, new Comparator<Team>() {
public int compare(Team a, Team b) {
if (a.s == b.s)
return a.t - b.t;
return b.s - a.s;
}
});
int f = k - 1;
int la = k - 1;
Team p = l.get(k - 1);
while (la < n && l.get(la).s == p.s && l.get(la).t == p.t) la++;
while ( f >= 0 && l.get(f).s == p.s && l.get(f).t == p.t) f--;
out.println(la - f - 1);
}
static class Team {
int s;
int t;
public Team(int a, int b) { s = a; t = b; }
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
private void solve() throws IOException
{
int n = nextInt();
int k = nextInt();
int p[] = new int[n];
int t[] = new int[n];
for(int i = 0; i < n; i++)
{
p[i] = nextInt();
t[i] = nextInt();
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(p[i] < p[j] || (p[i] == p[j] && t[i] > t[j]))
{
int tmp = p[i];
p[i] = p[j];
p[j] = tmp;
tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
int pN = p[k - 1];
int tN = t[k - 1];
int counter = 0;
for(int i = 0; i < n; i++)
{
if(p[i] == pN && t[i] == tN)
{
counter++;
}
}
System.out.println(counter);
}
String nextToken() throws IOException
{
if (st == null || !st.hasMoreTokens())
{
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException
{
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException
{
return Double.parseDouble(nextToken());
}
public static void main(String args[]) throws IOException
{
new A().solve();
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nova
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
class Team implements Comparable<Team> {
int solved = 0;
int penalty = 0;
Team(int solved, int penalty) {
this.solved = solved;
this.penalty = penalty;
}
public int compareTo(Team o) {
return this.solved == o.solved ? this.penalty - o.penalty : -(this.solved - o.solved);
}
public boolean equals(Object obj) {
if (obj instanceof Team) {
Team o = (Team) obj;
return ((this.solved == o.solved) && (this.penalty == o.penalty));
}
return false;
}
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int k = in.nextInt();
Team[] teams = new Team[n];
for (int i = 0; i < n; i++)
teams[i] = new Team(in.nextInt(), in.nextInt());
Arrays.sort(teams);
int[] top = new int[n];
int[] map = new int[n];
int cur = -1;
for (int i = 0; i < n; i++) {
if (i == 0 || !teams[i].equals(teams[i - 1])) cur = i;
top[cur]++;
map[i] = cur;
}
out.println(top[map[k - 1]]);
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
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());
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
int x[] = new int[n];
for (int i = 0; i < n; i++) {
int p = in.nextInt(), t = in.nextInt();
x[i] = (50 - p) * 100 + t;
}
Arrays.sort(x);
int cnt = 0;
for (int q: x)
if (q == x[k - 1]) cnt++;
System.out.println(cnt);
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution {
static class Team implements Comparable<Team> {
int pr;
int time;
int id;
public Team(int P, int T, int I) {
pr = P;
time = T;
id = I;
}
@Override
public int compareTo(Team t) {
return pr != t.pr ? t.pr - pr : time != t.time ? time - t.time : id - t.id;
}
}
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
Team[] a = new Team[n];
int[] c = new int[n + 1];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(in.readLine());
int p = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
a[i] = new Team(p, t, i);
}
Arrays.sort(a);
int prev = 1;
c[1]++;
for (int i = 1; i < n; i++) {
if (a[i].pr == a[i - 1].pr && a[i].time == a[i - 1].time)
for (int j = i + 1; j >= prev; j--)
c[j] = i + 2 - prev;
else {
prev = i + 1;
c[prev] = 1;
}
}
out.println(c[k]);
out.close();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Teams {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
Random rnd;
class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
if(a != p.a) {
return -(a - p.a);
}
return (b - p.b);
}
}
void solve() throws IOException {
int n = nextInt(), k = nextInt();
Pair[] ps = new Pair[n];
for(int i = 0; i < n; i++)
ps[i] = new Pair(nextInt(), nextInt());
Arrays.sort(ps);
int curPlace = 1, res = 0;
int[] places = new int[n];
for(int i = 0; i < n; i++) {
if(i - 1 >= 0 && ps[i].compareTo(ps[i - 1]) != 0)
++curPlace;
places[i] = curPlace;
}
for(int i = 0; i < n; i++) {
if(places[i] == places[k - 1])
++res;
}
out.println(res);
}
public static void main(String[] args) {
new Teams().run();
}
public void run() {
try {
final String className = this.getClass().getName().toLowerCase();
try {
in = new BufferedReader(new FileReader(className + ".in"));
out = new PrintWriter(new FileWriter(className + ".out"));
} catch (FileNotFoundException e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
rnd = new Random();
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(42);
}
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = in.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import sun.reflect.generics.tree.Tree;
import java.io.*;
import java.util.*;
public class A2 {
static Scanner in; static int next() throws Exception {return in.nextInt();};
// static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;}
// static BufferedReader in;
static PrintWriter out;
public static void main(String[] args) throws Exception {
in = new Scanner(System.in);
// in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
// in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = next(), k = next()-1;
int x[] = new int[n];
for (int i = 0;i < n;i++) x[i] = (100-next())*100+next();
Arrays.sort(x);
int res = 0, t = x[k];
for (int i = 0;i < n;i++) if (t == x[i]) res++;
out.println(res);
out.println();
out.close();
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class A113 {
public static void main(String[] args) {
new A113().run();
}
public void run() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
Team[] teams = new Team[n];
for (int i = 0; i < teams.length; i++) {
teams[i] = new Team(in.nextInt(), in.nextInt());
}
Arrays.sort(teams);
int counter = 1;
int index = k-2;
while (index >= 0 && teams[index].p == teams[k-1].p && teams[index].t == teams[k-1].t) {
index--;
counter++;
}
index = k;
while (index < n && teams[index].p == teams[k-1].p && teams[index].t == teams[k-1].t) {
index++;
counter++;
}
System.out.println(counter);
}
private class Team implements Comparable<Team> {
int p;
int t;
public Team(int pp, int tt) {
p = pp; t = tt;
}
@Override
public int compareTo(Team o) {
if (o.p - this.p == 0)
return this.t - o.t;
return o.p - this.p;
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author codeKNIGHT
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n=in.nextInt(),k=in.nextInt()-1,i;
scores a[]=new scores[n];
for(i=0;i<n;i++)
a[i]=new scores(in.nextInt(),in.nextInt());
Arrays.sort(a);
int c=1;
for(i=k-1;i>=0;i--)
{
if(a[i].p==a[k].p&&a[i].t==a[k].t)
c++;
else break;
}
for(i=k+1;i<n;i++)
{
if(a[i].p==a[k].p&&a[i].t==a[k].t)
c++;
else break;
}
out.println(c);
}
class scores implements Comparable<scores>
{
int p,t;
public scores(int p,int t)
{
this.p=p;
this.t=t;
}
public int compareTo(scores a)
{
if(a.p>this.p)
return 1;
if(a.p==this.p&&a.t<this.t)
return 1;
return -1;
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Scanner;
import java.util.Arrays;
public class P166A
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
int n = myScanner.nextInt();
int k = myScanner.nextInt();
Team[] queue = new Team[n];
for (int i = 0; i < n; i++)
{
queue[i] = new Team(myScanner.nextInt(), myScanner.nextInt());
}
Arrays.sort(queue);
int counter = 0;
int i = 0;
int p = -1;
int t = -1;
for (; i < k; i++)
{
if (p == queue[i].problems && t == queue[i].penalty)
counter++;
else
{
p = queue[i].problems;
t = queue[i].penalty;
counter = 1;
}
}
for (; i < n; i++)
{
if (p == queue[i].problems && t == queue[i].penalty)
counter++;
else
break;
}
System.out.println(counter);
}
static class Team implements Comparable<Team>
{
int problems;
int penalty;
public Team(int problems, int penalty)
{
this.problems = problems;
this.penalty = penalty;
}
public int compareTo(Team t)
{
if (problems > t.problems) return -1;
else if (problems < t.problems) return 1;
else if (penalty > t.penalty) return 1;
else if (penalty < t.penalty) return -1;
else return 0;
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class A {
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
Point[] p = new Point[n];
for (int i = 0; i < n; i++)
p[i] = new Point(nextInt(), nextInt());
Arrays.sort(p, new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
if (o1.x == o2.x) return o1.y - o2.y;
return o2.x - o1.x;
}
});
Point cur = p[k - 1];
int res = 0;
for (int i = 0; i < p.length; i++) {
if (p[i].x == cur.x && p[i].y == cur.y) res++;
}
pl(res);
}
public static void main(String[] args) {
new A().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
void p(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.flush();
writer.print(objects[i]);
writer.flush();
}
}
void pl(Object... objects) {
p(objects);
writer.flush();
writer.println();
writer.flush();
}
int cc;
void pf() {
writer.printf("Case #%d: ", ++cc);
writer.flush();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
public class A {
static class team implements Comparable<team>
{
int problems;
int penalty;
public team(int problems,int penalty)
{
this.penalty=penalty;
this.problems=problems;
}
public int compareTo(team a) {
if (a.problems==this.problems)
return this.penalty - a.penalty;
return a.problems - this.problems;
}
public boolean igual(team a)
{
if (this.problems!=a.problems)
return false;
return (this.penalty==a.penalty);
}
}
static class Scanner
{
BufferedReader rd;
StringTokenizer tk;
public Scanner() throws IOException
{
rd=new BufferedReader(new InputStreamReader(System.in));
tk=new StringTokenizer(rd.readLine());
}
public String next() throws IOException
{
if (!tk.hasMoreTokens())
{
tk=new StringTokenizer(rd.readLine());
return tk.nextToken();
}
return tk.nextToken();
}
public int nextInt() throws NumberFormatException, IOException
{
return Integer.valueOf(this.next());
}
}
static team[] array=new team[100];
static int N,K;
public static void main(String args[]) throws IOException
{
Scanner sc=new Scanner();
N=sc.nextInt();
K=sc.nextInt();
for(int i=0;i<N;i++)
{
array[i]=new team(sc.nextInt(),sc.nextInt());
}
Arrays.sort(array,0,N);
/*
for(int i=0;i<N;i++)
System.out.println(array[i].problems);*/
int shared=0;
for(int i=K-1;i>=0 && array[K-1].igual(array[i]);i--,shared++);
for(int i=K;i<N && array[K-1].igual(array[i]);i++,shared++);
System.out.println(shared);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
public class Solution {
class Q implements Comparable<Q> {
int p, t;
Q(int q, int w) {
p = q; t = w;
}
@Override
public int compareTo(Q arg0) {
if (p == arg0.p) return t - arg0.t;
return arg0.p - p;
}
}
void solve() throws Exception {
int n = nextInt();
int k = nextInt() - 1;
Q[] a = new Q[n];
for (int i = 0; i < n; i++) a[i] = new Q(nextInt(), nextInt());
Arrays.sort(a);
int ans = 1;
for (int i = k - 1; i >= 0; i--) if (a[i].compareTo(a[k]) == 0) ans++; else break;
for (int i = k + 1; i < n; i++) if (a[i].compareTo(a[k]) == 0) ans++; else break;
out.println(ans);
}
public static void main(String[] args) {
new Solution().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
out.close();
}
}
String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
String s = in.readLine();
if (s == null) return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
class Team implements Comparable<Team>{
int p, t;
public Team(int p, int t) {
this.p = p;
this.t = t;
}
public int compareTo(Team other) {
if (this.p != other.p) return other.p - this.p;
return this.t - other.t;
}
}
public void solve() throws IOException {
int n = nextInt();
int K = nextInt() - 1;
Team[] team = new Team[n];
for (int i = 0; i < n; i++)
team[i] = new Team(nextInt(), nextInt());
Arrays.sort(team);
int ans = -1;
int pre = 0;
for (int i = 1; i < n; i++)
if (team[i].compareTo(team[i - 1]) != 0) {
if (K >= pre && K < i) {
ans = i - pre;
break;
}
pre = i;
}
if (ans == -1) ans = n - pre;
writer.println(ans);
}
public static void main(String[] args) throws FileNotFoundException {
new A().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
long tbegin = System.currentTimeMillis();
reader = new BufferedReader(new InputStreamReader(System.in));
//reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.inp")));
tokenizer = null;
writer = new PrintWriter(System.out);
//writer = new PrintWriter(new FileOutputStream("test.out"));
solve();
//reader.close();
//System.out.println(System.currentTimeMillis() - tbegin + "ms");
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.io.*;
public class cf166a {
private static boolean[][] matrix;
private static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// long l = sc.nextLong();
// int i = sc.nextInt();
// String input = sc.nextLine();
int n = sc.nextInt();
int k = sc.nextInt();
int[] p = new int[n];
int[] t = new int[n];
int[] score = new int[n];
for(int i=0;i<n;i++){
p[i] = sc.nextInt();
t[i] = sc.nextInt();
score[i] = p[i] * 100 + (50 - t[i]);
}
boolean[] called = new boolean[n];
int x = 0;
boolean check = false;
while(true){
int max = 0;
int y = 0;
for(int i=0;i<n;i++){
if(called[i]==false&&score[i]>max){max=score[i];}
}
for(int i=0;i<n;i++){
if(max==score[i]){
called[i] = true;
x++;
y++;
if(x==k){check=true;}
}
}
if(check){
System.out.println(y);
break;
}
}
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int place = in.readInt() - 1;
final int[] points = new int[count];
final int[] time = new int[count];
IOUtils.readIntArrays(in, points, time);
Comparator<Integer> comparator = new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
if (points[o1] != points[o2])
return points[o2] - points[o1];
return time[o1] - time[o2];
}
};
Integer[] order = ArrayUtils.order(count, comparator);
int answer = 0;
for (int i = 0; i < count; i++) {
if (comparator.compare(order[place], order[i]) == 0)
answer++;
}
out.printLine(answer);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public 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++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
class ArrayUtils {
public static Integer[] generateOrder(int size) {
Integer[] order = new Integer[size];
for (int i = 0; i < size; i++)
order[i] = i;
return order;
}
public static Integer[] order(int size, Comparator<Integer> comparator) {
Integer[] order = generateOrder(size);
Arrays.sort(order, comparator);
return order;
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt() - 1;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int p = scan.nextInt();
int t = scan.nextInt();
arr[i] = -p * 10000 + t;
}
Arrays.sort(arr);
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == arr[k]) {
count++;
}
}
System.out.println(count);
}
} | nlogn | 166_A. Rank List | CODEFORCES |
//package round113;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), k = ni();
int[][] d = new int[n][2];
for(int i = 0;i < n;i++){
d[i][0] = ni();
d[i][1] = ni();
}
Arrays.sort(d, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0] - b[0] != 0)return -(a[0] - b[0]);
return a[1] - b[1];
}
});
int b;
for(b = k-1;b >= 0 && d[k-1][0] == d[b][0] && d[k-1][1] == d[b][1];b--);
b++;
int a;
for(a = k-1;a < n && d[k-1][0] == d[a][0] && d[k-1][1] == d[a][1];a++);
a--;
out.println(a-b+1);
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception
{
new A().run();
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public long nl()
{
try {
long num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String ns()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
public char[] ns(int n)
{
char[] buf = new char[n];
try{
int b = 0, p = 0;
while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
if(b == -1)return null;
buf[p++] = (char)b;
while(p < n){
b = is.read();
if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
buf[p++] = (char)b;
}
return Arrays.copyOf(buf, p);
} catch (IOException e) {
}
return null;
}
double nd() { return Double.parseDouble(ns()); }
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author hheng
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int N = in.nextInt();
int k = in.nextInt();
Team[] t = new Team[N];
for (int i=0; i<N; i++) t[i] = new Team(in.nextInt(), in.nextInt());
Arrays.sort(t);
int p_k = t[k-1].p, t_k = t[k-1].t;
int count = 0;
for (int i=0; i<N; i++) if (t[i].p==p_k && t[i].t ==t_k) count++;
out.println(count);
}
}
class Team implements Comparable<Team>{
int p, t;
Team(int a, int b) { p=a; t=b;}
public int compareTo(Team g) {
if (p < g.p) return 1;
if (p > g.p) return -1;
if (t < g.t) return -1;
if (t > g.t) return 1;
return 0;
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solver {
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) throws NumberFormatException, IOException {
Solver solver = new Solver();
solver.open();
solver.solve();
solver.close();
}
public void open() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
public String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public class result implements Comparable{
public int t = 0;
public int p = 0;
public result(int p,int t){
this.t = t;
this.p = p;
}
@Override
public int compareTo(Object o) {
result r = (result)o;
int out = r.p-p;
if (out==0) out = t-r.t;
return out;
}
}
public void solve() throws NumberFormatException, IOException {
int n = nextInt();
int k = nextInt();
result[] table = new result[n];
for (int i=0;i<n;i++){
int p = nextInt();
int t = nextInt();
table[i] = new result(p,t);
}
Arrays.sort(table);
int result = 1;
k--;
for (int i=k-1;i>=0 && table[i].p==table[k].p && table[i].t==table[k].t;i--){
result++;
}
for (int i=k+1;i<n && table[i].p==table[k].p && table[i].t==table[k].t;i++){
result++;
}
out.println(result);
}
public void close() {
out.flush();
out.close();
}
} | nlogn | 166_A. Rank List | CODEFORCES |
import java.util.Arrays;
/**
* @author piuspratik (Piyush Das)
*/
public class TaskA {
class Contest implements Comparable<Contest>
{
int problems;
int penalty;
Contest (int problems, int penalty) {
this.problems = problems;
this.penalty = penalty;
}
public int compareTo(Contest contest) {
if(problems != contest.problems) return contest.problems - problems;
return penalty - contest.penalty;
}
}
void run(){
int n = nextInt(), k = nextInt();
Contest[] c = new Contest[n];
for(int i = 0; i < n; i++) {
c[i] = new Contest(nextInt(), nextInt());
}
Arrays.sort(c);
int cproblem = c[k - 1].problems, cpenalty = c[k - 1].penalty;
int ans = 0;
for(int i = 0; i < n; i++) {
if(c[i].problems == cproblem && c[i].penalty == cpenalty) ans++;
}
System.out.println(ans);
}
int nextInt(){
try{
int c = System.in.read();
if(c == -1) return c;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return c;
}
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;
}
}
long nextLong(){
try{
int c = System.in.read();
if(c == -1) return -1;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return -1;
}
if(c == '-') return -nextLong();
long 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());
}
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;
}
}
String nextLine(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(c == '\r' || c == '\n')
c = System.in.read();
do{
res.append((char)c);
c = System.in.read();
}while(c != '\r' && c != '\n');
return res.toString();
}catch(Exception e){
return null;
}
}
public static void main(String[] args){
new TaskA().run();
}
}
| nlogn | 166_A. Rank List | CODEFORCES |
//package fourninetysixDiv3;
import java.util.HashMap;
import java.util.Scanner;
public class Median_Segments_general {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
System.out.println(func(n, m, arr)-func(n, m+1, arr));
}
public static long func(int n,int m,int[] arr) {
HashMap<Long, Integer> map = new HashMap<>();
map.put((long) 0, 1);
long sum = 0;
long res = 0;
long add=0;
for(int i=0;i<n;i++) {
if(arr[i]<m) {
sum--;
if(map.containsKey(sum)) {
add-=map.get(sum);
}
}
else {
if(map.containsKey(sum)) {
add+=map.get(sum);
}
sum++;
}
res+=add;
if(map.containsKey(sum)) {
map.put(sum, map.get(sum)+1);
}
else {
map.put(sum,1);
}
}
return res;
}
}
| nlogn | 1005_E1. Median on Segments (Permutations Edition) | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tok;
static boolean hasNext()
{
while(tok==null||!tok.hasMoreTokens())
try{
tok=new StringTokenizer(in.readLine());
}
catch(Exception e){
return false;
}
return true;
}
static String next()
{
hasNext();
return tok.nextToken();
}
static long nextLong()
{
return Long.parseLong(next());
}
static int nextInt()
{
return Integer.parseInt(next());
}
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap();
map.put(0,1);
int n = nextInt();
int m = nextInt();
int index = -1;
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=nextInt();
if(a[i]==m)
index=i;
}
int sum = 0;
for(int i=0;i<index;i++){
if (a[i]<m)
sum--;
else
sum++;
if (map.containsKey(sum)){
map.put(sum,map.get(sum)+1);
}else {
map.put(sum,1);
}
}
long ans = 0;
for(int i=index;i<n;i++){
if (a[i]<m)
sum--;
else if(a[i]>m)
sum++;
if (map.containsKey(sum))
ans+=map.get(sum);
if (map.containsKey(sum-1))
ans+=map.get(sum-1);
}
out.print(ans);
out.flush();
}
}
| nlogn | 1005_E1. Median on Segments (Permutations Edition) | CODEFORCES |
import java.io.*;
import java.util.*;
import java.util.function.Function;
public class Main {
static int N;
static int[] U, V;
static int[] A;
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
N = sc.nextInt();
U = new int[N-1];
V = new int[N-1];
for (int i = 0; i < N - 1; i++) {
U[i] = sc.nextInt()-1;
V[i] = sc.nextInt()-1;
}
A = sc.nextIntArray(N, -1);
System.out.println(solve() ? "Yes" : "No");
}
static boolean solve() {
if( A[0] != 0 ) return false;
int[][] G = adjB(N, U, V);
Map<Integer, Integer> parents = new HashMap<>();
for (Node node : orderFromRoot(N, G, 0)) {
parents.put(node.a, node.parent);
}
ArrayDeque<Integer> q = new ArrayDeque<>();
for (int next : G[0]) {
q.add(0);
}
int idx = 1;
while(!q.isEmpty()) {
int p = q.poll();
int a = A[idx++];
if( parents.get(a) != p ) {
return false;
}
for (int next : G[a]) {
if( next == p ) continue;
q.add(a);
}
}
return true;
}
static int[][] adjB(int n, int[] from, int[] to) {
int[][] adj = new int[n][];
int[] cnt = new int[n];
for (int f : from) {
cnt[f]++;
}
for (int t : to) {
cnt[t]++;
}
for (int i = 0; i < n; i++) {
adj[i] = new int[cnt[i]];
}
for (int i = 0; i < from.length; i++) {
adj[from[i]][--cnt[from[i]]] = to[i];
adj[to[i]][--cnt[to[i]]] = from[i];
}
return adj;
}
static Node[] orderFromRoot(int N, int[][] G, int root) {
ArrayDeque<Node> q = new ArrayDeque<>();
Node[] ret = new Node[N];
int idx = 0;
q.add(new Node(-1, root));
while(!q.isEmpty()) {
Node n = q.poll();
ret[idx++] = n;
for (int next : G[n.a]) {
if( next == n.parent ) continue;
q.add(new Node(n.a, next));
}
}
return ret;
}
static class Node {
int parent, a;
public Node(int parent, int a) {
this.parent = parent;
this.a = a;
}
}
@SuppressWarnings("unused")
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
long nextLong() {
return Long.parseLong(next());
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static <A> void writeLines(A[] as, Function<A, String> f) {
PrintWriter pw = new PrintWriter(System.out);
for (A a : as) {
pw.println(f.apply(a));
}
pw.flush();
}
static void writeLines(int[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (int a : as) pw.println(a);
pw.flush();
}
static void writeLines(long[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (long a : as) pw.println(a);
pw.flush();
}
static int max(int... as) {
int max = Integer.MIN_VALUE;
for (int a : as) max = Math.max(a, max);
return max;
}
static int min(int... as) {
int min = Integer.MAX_VALUE;
for (int a : as) min = Math.min(a, min);
return min;
}
static void debug(Object... args) {
StringJoiner j = new StringJoiner(" ");
for (Object arg : args) {
if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
else j.add(arg.toString());
}
System.err.println(j.toString());
}
}
| nlogn | 1037_D. Valid BFS? | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class MicroWorld {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] temp = new int[1000001];
StringTokenizer st1 = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++){
temp[Integer.parseInt(st1.nextToken())]++;
}
int b = k + 1;
for (int i = 1000000; i > 0; i--){
if (temp[i] > 0){
if (b <= k){
n -= temp[i];
}
b = 1;
}else{
b++;
}
}
System.out.println(n);
}
}
| nlogn | 990_B. Micro-World | CODEFORCES |
/*
Keep solving problems.
*/
import java.util.*;
import java.io.*;
public class CFA {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
private static long MOD = 1000L * 1000L * 1000L + 7;
private static final int[] dx = {0, -1, 0, 1};
private static final int[] dy = {1, 0, -1, 0};
private static final String yes = "Yes";
private static final String no = "No";
int n;
int m;
char[][] mat;
long base = 397;
void solve() throws IOException {
n = nextInt();
m = nextInt();
mat = new char[n][m];
for (int i = 0; i < n; i++) {
mat[i] = nextString().toCharArray();
}
int alpha = 26;
long[] pow = new long[alpha];
pow[0] = 1;
for (int i = 1; i < alpha; i++) {
pow[i] = pow[i - 1] * base % MOD;
}
long res = 0;
for (int l = 0; l < m; l++) {
//[l, r]
long[] hash = new long[n];
long[] mask = new long[n];
for (int r = l; r < m; r++) {
for (int i = 0; i < n; i++) {
hash[i] += pow[mat[i][r] - 'a'];
hash[i] %= MOD;
mask[i] = mask[i] ^ (1L << (mat[i][r] - 'a'));
}
int start = 0;
while (start < n) {
if ((mask[start] & (mask[start] - 1)) != 0) {
start++;
continue;
}
int end = start;
List<Long> l1 = new ArrayList<>();
while (end < n && (mask[end] & (mask[end] - 1)) == 0) {
l1.add(hash[end]);
end++;
}
start = end;
res += manacher(l1);
}
}
}
outln(res);
}
long manacher(List<Long> arr) {
int len = arr.size();
long[] t = new long[len * 2 + 3];
t[0] = -1;
t[len * 2 + 2] = -2;
for (int i = 0; i < len; i++) {
t[2 * i + 1] = -3;
t[2 * i + 2] = arr.get(i);
}
t[len * 2 + 1] = -3;
int[] p = new int[t.length];
int center = 0, right = 0;
for (int i = 1; i < t.length - 1; i++) {
int mirror = 2 * center - i;
if (right > i) {
p[i] = Math.min(right - i, p[mirror]);
}
// attempt to expand palindrome centered at i
while (t[i + (1 + p[i])] == t[i - (1 + p[i])]) {
p[i]++;
}
// if palindrome centered at i expands past right,
// adjust center based on expanded palindrome.
if (i + p[i] > right) {
center = i;
right = i + p[i];
}
}
long res = 0;
for (int i = 0; i < 2 * len; i++) {
int parLength = p[i + 2];
if (i % 2 == 0) {
res += (parLength + 1) / 2;
}
else {
res += parLength / 2;
}
}
return res;
}
void shuffle(int[] a) {
int n = a.length;
for(int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
long gcd(long a, long b) {
while(a != 0 && b != 0) {
long c = b;
b = a % b;
a = c;
}
return a + b;
}
private void outln(Object o) {
out.println(o);
}
private void out(Object o) {
out.print(o);
}
private void formatPrint(double val) {
outln(String.format("%.9f%n", val));
}
public CFA() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CFA();
}
public long[] nextLongArr(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
public int[] nextIntArr(int n) throws IOException {
int[] res = new int[n];
for(int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
public String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | nlogn | 1080_E. Sonya and Matrix Beauty | CODEFORCES |
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.io.InputStream ;
import java.util.InputMismatchException;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
//Scanner sc = new Scanner();
Reader in = new Reader();
Main solver = new Main();
solver.solve(out, in);
out.flush();
out.close();
}
void solve(PrintWriter out, Reader in) throws IOException{
int n = in.nextInt();
int m = in.nextInt();
int[] vert = new int[n+1];
for(int i=0 ;i<n ;i++) vert[i] = in.nextInt();
vert[n] = (int)1e9;
int cnt=0,x,y;
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0 ;i<m ;i++) {
x = in.nextInt();
y = in.nextInt();
in.nextInt();
if(x==1) arr.add(y);
}
horz = new int[arr.size()];
for(int i=0 ;i<arr.size();i++) horz[i] = arr.get(i);
Arrays.sort(horz);
Arrays.sort(vert);
int ans = 2*(int)1e5+10;
for(int i=0 ;i<=n ;i++){
int lesshorz = bs(vert[i],horz.length);
ans = Math.min(ans,i+horz.length-lesshorz-1);
}
out.println(ans);
}
static int[] horz;
static int bs(int num,int m){
int mid,lo=0,hi=m-1,r=-1;
while(lo<=hi){
mid = (lo+hi)/2;
if(horz[mid]<num){
lo = mid+1;
r = mid;
}else{
hi =mid-1;
}
}
return r;
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public 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();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| nlogn | 1075_C. The Tower is Going Home | CODEFORCES |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.