Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added code from SRM537, TCO 11,12 Qual 1, 2 codeforces rounds.

  • Loading branch information...
commit c664f58c9e214f519e3ecb873e5f1d89e79bdb03 1 parent 0220460
@yasith authored
View
95 codeforces/round109/d.cpp
@@ -0,0 +1,95 @@
+#include <vector>
+#include <list>
+#include <map>
+#include <set>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <bitset>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+#include <utility>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include <cstdio>
+#include <cmath>
+#include <cstdlib>
+#include <ctime>
+#include <cstring>
+
+#define p(x) cout << #x << ":" << x << endl;
+
+using namespace std;
+
+map<int, int> off;
+map<int, int> on;
+
+int n, r;
+
+int gcd(int a, int b){
+ if(b == 0){
+ return a;
+ }
+ return gcd(b, a%b);
+}
+
+int main(){
+
+ cin >> n >> r;
+
+ for(int i = 0; i < n; i++){
+ off[i+1] = 0;
+ }
+
+ for (int i = 0; i < r; i++) {
+ char c;
+ int j;
+ cin >> c >> j;
+
+ if(c == '+'){
+ if(on.find(j) != on.end()){
+ cout << "Already on" << endl;
+ continue;
+ }
+ int conflict = -1;
+ int other = 0;
+
+ map<int, int>::iterator it;
+ for(it = on.begin(); it != on.end(); it++){
+ other = (*it).first;
+ if(gcd(other, j) != 1){
+ conflict = other;
+ break;
+ }
+ }
+
+ if(conflict != -1){
+ cout << "Conflict with " << other << endl;
+ continue;
+ }
+
+ on[j] = 0;
+ off.erase(j);
+ cout << "Success" << endl;
+ }
+
+ if(c == '-'){
+ if(off.find(j) != off.end()){
+ cout << "Already off" << endl;
+ continue;
+ }
+
+ on.erase(j);
+ off[j] = 0;
+
+ cout << "Success" << endl;
+ }
+ }
+
+
+ return 0;
+}
+
+
View
49 codeforces/vk2012-qual-1/a.cpp
@@ -0,0 +1,49 @@
+#include <vector>
+#include <list>
+#include <map>
+#include <set>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <bitset>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+#include <utility>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include <cstdio>
+#include <cmath>
+#include <cstdlib>
+#include <ctime>
+#include <cstring>
+
+#define p(x) cout << #x << ":" << x << endl;
+
+using namespace std;
+
+int main(){
+
+ int n, k;
+ int a[50];
+
+ cin >> n >> k;
+ k --;
+
+ for (int i = 0; i < n; i++) {
+ cin >> a[i];
+ }
+
+ int cnt = 0;
+ for (int i = 0; i < n; i++) {
+ if(a[i] >= a[k] && a[i] > 0)
+ cnt ++;
+ }
+
+ cout << cnt << endl;
+
+ return 0;
+}
+
+
View
34 topcoder/SRM537/KingXNewBaby.java
@@ -0,0 +1,34 @@
+import java.math.*;
+import java.util.*;
+import java.io.*;
+
+public class KingXNewBaby {
+
+ public String isValid(String name) {
+ String vow = "aeiou";
+ String vs = "";
+
+ if(name.length() != 8) {
+ return "NO";
+ }
+
+ int v = 0;
+ for(int i = 0; i < name.length(); i++) {
+ for(int j = 0; j < vow.length(); j++) {
+ if(vow.charAt(j) == name.charAt(i)) {
+ v ++;
+ vs += name.charAt(i);
+ break;
+ }
+ }
+ }
+
+ if(v == 2) {
+ if(vs.charAt(0) == vs.charAt(1)) {
+ return "YES";
+ }
+ }
+
+ return "NO";
+ }
+}
View
67 topcoder/SRM537/KingXNewCurrency.java
@@ -0,0 +1,67 @@
+import java.math.*;
+import java.util.*;
+import java.io.*;
+
+public class KingXNewCurrency {
+
+ public int gcd(int a, int b) {
+ if(b == 0) {
+ return a;
+ }
+ return gcd(b, a%b);
+ }
+
+ public int howMany(int A, int B, int X) {
+ if(A % X == 0 && B % X == 0) {
+ return -1;
+ }
+
+ Set<Integer> set = new TreeSet<Integer>();
+
+ if(A % X == 0 && B % X != 0) {
+ int num = B;
+ while(num > 0) {
+ for(int i = 1; i*i <= num; i++) {
+ if(num % i == 0) {
+ set.add(new Integer(i));
+ set.add(new Integer(num / i));
+ }
+ }
+ num -= X;
+ }
+ return set.size();
+ }
+
+ int temp = A;
+ A = B;
+ B = temp;
+ if(A % X == 0 && B % X != 0) {
+ int num = B;
+ while(num > 0) {
+ for(int i = 1; i*i <= num; i++) {
+ if(num % i == 0) {
+ set.add(new Integer(i));
+ set.add(new Integer(num / i));
+ }
+ }
+ num -= X;
+ }
+ return set.size();
+ }
+
+ for(int i = A; i > 0; i -= X) {
+ for(int j = B; j > 0; j -= X) {
+ int target = gcd(i, j);
+
+ for(int x = 1; x*x <= target; x++) {
+ if(target % x == 0) {
+ set.add(new Integer(x));
+ set.add(new Integer(target/x));
+ }
+ }
+ }
+ }
+
+ return set.size();
+ }
+}
View
17 topcoder/SRM538/LeftOrRight.java
@@ -0,0 +1,17 @@
+public class LeftOrRight {
+
+ public int maxDistance(String program) {
+
+ int lmax = 0;
+ int rmax = 0;
+
+ for(int i = 0; i < program.length(); i++){
+ char c = program.charAt(i);
+ lmax = c == 'R' ? lmax - 1 : lmax + 1;
+ rmax = c == 'L' ? rmax - 1 : rmax + 1;
+ }
+
+ return Math.max(lmax, rmax);
+ }
+
+}
View
23 topcoder/TCO 11 Q1/MinimumLiars.java
@@ -0,0 +1,23 @@
+public class MinimumLiars {
+
+ public int getMinimum(int[] claim) {
+
+ int sum = 0;
+
+ for(int i = 1; i < claim.length+1; i++){
+ int count = 0;
+
+ for(int x: claim){
+ sum += x;
+ if(x > i)
+ count ++;
+ }
+
+ if(count == i)
+ return i;
+ }
+
+ return sum == 0 ? 0 : -1;
+ }
+
+}
View
98 topcoder/TCO 12 Q1/EllysJuice.java
@@ -0,0 +1,98 @@
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+
+public class EllysJuice {
+
+ class Player implements Comparable<Player>{
+
+ String name;
+ int drinks;
+
+ public Player(String name, int drinks){
+ this.name = name;
+ this.drinks = drinks;
+ }
+
+ public int compareTo(Player o) {
+ return this.drinks - o.drinks;
+ }
+
+ }
+
+ double f(int n){
+ return 1.0/(Math.pow(2, Math.ceil( ((double)(n+1))/2.0 ) ) );
+ }
+
+ HashMap<String, Integer>map = new HashMap<String, Integer>();
+
+ public String[] getWinners(String[] players) {
+ for(String player: players){
+ if(map.containsKey(player)){
+ map.put(player, new Integer(map.get(player) + 1));
+ }else{
+ map.put(player, 1);
+ }
+ }
+
+ ArrayList<Player> plist = new ArrayList<EllysJuice.Player>();
+
+ Object[] pnames = map.keySet().toArray();
+ Object[] pvals = map.values().toArray();
+
+ for(int i = 0; i < pnames.length; i++){
+ plist.add(new Player((String)pnames[i], (Integer)pvals[i]));
+ }
+
+ Collections.sort(plist);
+
+ ArrayList<String> wlist = new ArrayList<String>();
+
+ if(plist.size() == 1){
+ return new String[]{plist.get(0).name};
+ }
+
+ for(int i = 0; i < plist.size(); i++){
+ if(i == 0){
+ double total = 0;
+ for(int j = 0; j < plist.get(i).drinks; j++){
+ total += f(j);
+ }
+ double other = 0;
+ for(int j = 0; j < plist.get(i+1).drinks; j++){
+ other += f(j + plist.get(i).drinks);
+ }
+
+
+ if(total > other){
+ wlist.add(plist.get(i).name);
+ }
+ } else{
+ double total = 0;
+ for(int j = 0; j < plist.get(i).drinks; j++){
+ total += f(j);
+ }
+ double other = 0;
+ for(int j = 0; j < plist.get(0).drinks; j++){
+ other += f(j + plist.get(i).drinks);
+ }
+
+
+ if(total > other){
+ wlist.add(plist.get(i).name);
+ }
+ }
+ }
+
+ Collections.sort(wlist);
+ String arr[] = new String[wlist.size()];
+
+ int i = 0;
+ for(String name: wlist){
+ arr[i++] = name;
+ }
+
+ return arr;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.