bool compute_gap_new(
        CodeRegion * cr,
        Address addr,
        set<Function *,Function::less> const& funcs,
        set<Function *,Function::less>::const_iterator & beforeGap,
        Address & gapStart,
        Address & gapEnd,
	bool &reset_iterator)
    {
        long MIN_GAP_SIZE = 15;    
        Address lowerBound = cr->offset();
        Address upperBound = cr->offset() + cr->length();


        if (funcs.empty()) {
	    if (addr >= upperBound) return false;
	    gapStart = addr + 1;
	    if (gapStart < lowerBound) gapStart = lowerBound;
	    gapEnd = upperBound;
	    reset_iterator = true;
	    return true;
	} else if (addr < (*funcs.begin())->addr()) {
	    gapStart = addr + 1;
	    if (gapStart < lowerBound) gapStart = lowerBound;
	    gapEnd = (*funcs.begin())->addr();
	    reset_iterator = true;
	    return true;
	} else {
	    reset_iterator = false;
	    set<Function *,Function::less>::const_iterator afterGap(beforeGap);
	    ++afterGap;
	    while (true) {
	        gapStart = calc_end(*beforeGap);
		if (afterGap == funcs.end() || (*afterGap)->addr() > upperBound)
		    gapEnd = upperBound;
		else
		    gapEnd = (*afterGap)->addr();
		if (addr >= gapEnd || (long)(gapEnd - gapStart) <= MIN_GAP_SIZE) {
		    if (afterGap == funcs.end()) return false;
		    beforeGap = afterGap;
		    ++afterGap;
		} else {
		    if (gapStart < addr + 1) gapStart = addr + 1;
		    break;
		}
	    }
	    return true;
	}
    }
Example #2
0
Expr perform_inline(Expr e, const map<string, Function> &env,
                    const set<string> &inlines,
                    const vector<string> &order) {
    if (inlines.empty()) {
        return e;
    }

    bool funcs_to_inline = false;
    Expr inlined_expr = e;

    do {
        funcs_to_inline = false;
        // Find all the function calls in the current expression.
        FindAllCalls find;
        inlined_expr.accept(&find);
        const set<string> &calls_unsorted = find.funcs_called;

        vector<string> calls(calls_unsorted.begin(), calls_unsorted.end());
        // Sort 'calls' based on the realization order in descending order
        // if provided (i.e. last to be realized comes first).
        if (!order.empty()) {
            std::sort(calls.begin(), calls.end(),
                [&order](const string &lhs, const string &rhs){
                    const auto &iter_lhs = std::find(order.begin(), order.end(), lhs);
                    const auto &iter_rhs = std::find(order.begin(), order.end(), rhs);
                    return iter_lhs > iter_rhs;
                }
            );
        }

        // Check if any of the calls are in the set of functions to be inlined.
        // Inline from the last function to be realized to avoid extra
        // inlining works.
        for (const auto &call : calls) {
            if (inlines.find(call) != inlines.end()) {
                Function prod_func = env.at(call);
                // Impure functions cannot be inlined.
                internal_assert(prod_func.is_pure());
                // Inline the function call and set the flag to check for
                // further inlining opportunities.
                inlined_expr = inline_function(inlined_expr, prod_func);
                funcs_to_inline = true;
                break;
            }
        }
    } while (funcs_to_inline);

    return inlined_expr;
}
string ProgramOptionsDescription::usage (set<string> help) const {

  // footer with info about specific sections
  string footer;

  // we want all help, therefore use all help sections
  bool helpAll = help.find("--HELP-ALL--") != help.end();
  help.erase("--HELP-ALL--");
  help.erase("--HELP--");

  if (help.empty()) {
    help.insert("help-default");
  }

  set<string> ho = helpOptions();

  // remove help default
  set<string> hd = ho;
  hd.erase("help-default");

  // add footer
  if (helpAll) {
    help = ho;
    footer = "\nFor specific sections use: " + StringUtils::join(hd, ", ") + " or help";
  }
  else {
    set<string> is;
    set_difference(hd.begin(), hd.end(), help.begin(), help.end(), inserter(is, is.end()));

    if (! is.empty()) {
      footer = "\nFor more information use: " + StringUtils::join(is, ", ") + " or help-all";
    }
  }

  // compute all relevant names
  map<string, string> names;
  fillAllNames(help, names);
  size_t oWidth = 0;

  for (auto const& name : names) {
    size_t len = name.second.length();

    if (oWidth < len) {
      oWidth = len;
    }
  }

  return usageString(help, names, oWidth) + footer;
}
Example #4
0
string TransformForSql(set<Ice::Long>& gids) {
  ostringstream result;
  if (gids.empty()) {
    return result.str();
  }
  for(set<Ice::Long>::iterator itg = gids.begin(); itg!= gids.end(); ++itg) {
    if(itg == gids.begin()) {
      result << " ( " << *itg;
    }else {
      result <<" , " << *itg;
    }
  }
  result << " ) ";
  return result.str();
}
Example #5
0
int main () {
	int n, k, x;
	while (scanf("%d", &n) == 1) {
		S.clear();
		while (n--) {
			scanf("%d", &k);
			if (k == 1) {
				scanf("%d", &x);
				S.insert(x);
			} else if (k == 2 && !S.empty())
				S.erase(S.begin());
			else if (k == 3) {
				if (S.empty())
					printf("0\n");
				else {
					iter it = S.end();
					it--;
					printf("%d\n", *it);
				}
			}
		}
	}
	return 0;
}
const set<string>& HSolve::handledClasses()
{
    static set<string> classes;
    if (classes.empty())
    {
        classes.insert("CaConc");
        classes.insert("ZombieCaConc");
        classes.insert("HHChannel");
        classes.insert("ZombieHHChannel");
        classes.insert("Compartment");
        classes.insert("SymCompartment");
        classes.insert("ZombieCompartment");
    }
    return classes;
}
Example #7
0
int cal( int x )
{
    if(s.empty())
        return 0;
    int a,b;
    set<int>::iterator it = s.lower_bound(low[x]), itx = it;
    if( it == s.end() || it == s.begin())
    {
        it = s.begin();
        itx = s.end();
        itx--;
    }
    else itx--;
    return dis[x]-dis[lca(x,dfn[(*it)])]-dis[lca(x,dfn[(*itx)])]+dis[lca(dfn[(*it)],dfn[(*itx)])];
}
static
bool isFloodProne(const map<s32, CharReach> &look,
                  const set<CharReach> &flood_reach) {
    if (flood_reach.empty()) {
        return false;
    }

    for (const CharReach &flood_cr : flood_reach) {
        if (isFloodProne(look, flood_cr)) {
            return true;
        }
    }

    return false;
}
static
void handle_pending_vertices(GoughSSAVar *def, const GoughGraph &g,
                             const GoughGraphAux &aux,
                             set<GoughVertex> &pending_vertex,
                             set<const GoughSSAVar *> &rv) {
    if (pending_vertex.empty()) {
        return;
    }

    GoughVertex def_v = GoughGraph::null_vertex();
    if (contains(aux.containing_v, def)) {
        def_v = aux.containing_v.at(def);
    }
    ue2::unordered_set<GoughVertex> done;
    while (!pending_vertex.empty()) {
        GoughVertex current = *pending_vertex.begin();
        pending_vertex.erase(current);
        if (contains(done, current)) {
            continue;
        }
        done.insert(current);
        handle_pending_vertex(def_v, g, current, pending_vertex, rv);
    }
}
void construct1() {
    memset(cnt, 0, sizeof cnt);
    for (int i = 0; i < m; ++i) {
        ++cnt[w[i]];
        w[i] = -1;
    }
    fill(d+1, d+n, inf);
    d[0] = 0;
    st.insert(make_pair(d[0], 0));
    int big = K-1;
    while (cnt[big] == 0 && big > 0) --big;
    while (!st.empty()) {
        auto it = st.begin();
        int cur = it->second;
        st.erase(it);
        set<int> pr;
        for (int i = 0; i < K; ++i) {
            if (cnt[i] <= 0) continue;
            if (isPrime[d[cur] + i])
                pr.insert(i);
        }
        for (int i : g[cur]) {
            int other = u[i]^v[i]^cur;
            if (w[i] == -1) {
                while (!pr.empty()) {
                    auto ix = pr.begin();
                    if (cnt[*ix] <= 0)
                        pr.erase(ix);
                    else {
                        --cnt[*ix];
                        w[i] = *ix;
                        break;
                    }
                }
                if (w[i] == -1) {
                    while (cnt[big] == 0 && big > 0) --big;
                    w[i] = big;
                    --cnt[big];
                }
            }
            if (d[other] > d[cur] + w[i]) {
                st.erase(make_pair(d[other], other));
                d[other] = d[cur] + w[i];
                st.insert(make_pair(d[other], other));
            }
        }
    }
}
void codevs_ai::produce_some_workers() { // {{{
    constexpr int magic_resource = 170; // MAGIC:
    if (100 < turn and not friend_base_ids.empty()) return;
    if (magic_resource <= get_next_resource()) {
        vector<pair<double,unit_id> > vs;
        for (auto id : friend_viladge_ids) {
            auto unit = units[id];
            if (is_command_issued(id)) continue;
            auto p = unit->get_pos();
            double potential = 0;
            bool idling_worker = false;
            bool near_resource_possible = false;
            constexpr int r = 30; // MAGIC:
            // calculate potential
            for (auto d : manhattan_range(r)) {
                auto q = p + d;
                if (not on_board(q)) continue;
                if (next_resource_possible[q.y][q.x]) near_resource_possible = true;
                potential += (next_resource_possible[q.y][q.x] ? 1 : -0.1) * 1 /(double) (r - manhattan(d) + 1); // MAGIC:
                if (manhattan(d) == 0) {
                    for (auto it : next_at[p.y][p.x]) {
                        if (units[it]->kind == kind::worker) {
                            if (not is_command_issued(it)) {
                                idling_worker = true; break;
                            }
                        }
                    }
                } else if (1 <= manhattan(d) and manhattan(d) <= 20) { // MAGIC:
                    for (auto it : next_at[q.y][q.x]) {
                        if (units[it]->kind == kind::worker and not resource_points.count(q)) {
                            if (not is_command_issued(id)) {
                                idling_worker = true; break;
                            }
                        }
                    }
                }
                if (idling_worker) break;
            }
            if (not idling_worker and near_resource_possible) vs.push_back(make_pair(potential,id));
        }
        sort(vs.begin(), vs.end());
        while (not vs.empty() and magic_resource <= get_next_resource()) { // MAGIC:
            unit_id id = vs.back().second;
            vs.pop_back();
            issue_command(id, command::worker);
        }
    }
} // }}}
Example #12
0
inline void dijkstra(int src) {
	fill(dist, dist + N, INF);
	fill(par, par + N, -2);

	dist[src] = 0;
	par[src] = -1;
	S.insert(mp(0, src));

	while (!S.empty()) {
		int v = S.begin()->second;
		S.erase(S.begin());

		for (pii edge : list[v])
			relax(v, edge.second, edge.first);
	}
}
Example #13
0
  ~ObjectPool()
  {
    for (typename list<T *>::iterator it = m_pool.begin(); it != m_pool.end(); it++)
    {
      T * cur = *it;
#ifdef DEBUG
      typename set<T *>::iterator its = m_checkerSet.find(cur);
      ASSERT(its != m_checkerSet.end(), ("The same element returned twice or more!"));
      m_checkerSet.erase(its);
#endif
      delete cur;
    }
#ifdef DEBUG
    ASSERT(m_checkerSet.empty(), ("Alert! Don't all elements returned to pool!"));
#endif
  }
Example #14
0
int defend(set<missile> fired, list<missile> mlist){
        if(mlist.size() == 0) return 0;
        missile m = mlist.front();
        mlist.pop_front();
        cout<<"m: "<<m.t<<", "<<m.f<<endl;

        if(fired.empty()){
            fired.insert(m);
            int ret = defend(fired, mlist);
            cout<<"res: "<<m.t<<", "<<m.f<<" = "<<ret+1<<endl;
            return ret+1;
        }
        else{
            set<missile>::iterator it;
            for(it = fired.begin(); it != fired.end() && !valid(*it, m) && !same(*it, m); it++);
            if(it != fired.end() && valid(*it, m)){

                vector<missile> candidate;
                for(set<missile>::iterator j = it; j != fired.end(); j++)
                    if(valid(*j, m)) candidate.push_back(*j);
                fired.insert(m);
                cout<<"----candidate----"<<endl;
                for(vector<missile>::iterator j = candidate.begin(); j != candidate.end(); j++) cout<<j->t<<" : "<<j->f<<endl;
                cout<<"----candidate----"<<endl;
                int minval = INT_MAX;

                for(int k = 0; k < candidate.size(); k++){
                    missile c = candidate[k];
                    fired.erase(c);
                    int val = defend(fired, mlist);
                    if(minval > val) minval = val;
                    fired.insert(c);
                }
                int ret = min(defend(fired, mlist)+1, minval);
                cout<<"res: "<<m.t<<", "<<m.f<<" = "<<ret<<endl;
                return ret;
            }

            else {
                fired.insert(m);
                int ret = defend(fired, mlist);
                if(it == fired.end()) ret++;
                cout<<"res: "<<m.t<<", "<<m.f<<" = "<<ret<<endl;
                return ret;
            }
        }
}
Example #15
0
void MyStrategy::move(const Trooper& self, const World& world,
        const Game& game, Move& move) {
    clock_t start_clock = clock();

    if (empty_map.empty()) {
        fill_empty_map(world, empty_map);
    }

    if (safety_map.empty()) {
        init_safety_map(world);
    }

    if (explore_map.empty()) {
        fill_empty_map(world, explore_map);
    }

    if (team_map.empty()) {
        fill_empty_map(world, team_map);
    }

    update_explore_map(world);
    update_team_map(world);
    update_enemies(world, self);

    if (obstacles.empty()) {
        for (int x = 0; x < world.getWidth(); x++) {
            for (int y = 0; y < world.getHeight(); y++) {
                if (world.getCells().at(x).at(y) != FREE) {
                    obstacles.insert(Point(x, y));
                }
            }
        }
    }

    validate_missions();

    if (!is_mission_active(self)) {
        auto mission = unique_ptr<Mission>(new MissionExplore(world));
        assign_mission(self, std::move(mission));
    }

    evaluate_mission(game, world, self, move);

    printf("TIME: %.4f\n", double(clock() - start_clock) / CLOCKS_PER_SEC);

    draw_map(world, team_map, world.getTroopers(), self);
}
Example #16
0
void Solver::fdirection(int d, ARRAY<2, double>& a, set<int> &C, ARRAY<1, double> &delta_f, ARRAY<1, double> &delta_a) {
    int n = delta_f.dim(1);
    for (int i = 1; i <= n; ++i) {
        delta_a(i) = a(i,d);
        delta_f(i) = 0;
    }
    delta_f(d) = 1;
    bool flag = false;
    for (int i = 1; i <= n; ++i) {
        double ttt = 0;
        for (int j = 1; j <= n; ++j)
            ttt += a(i,j) * delta_f(j);
        if (fabs(ttt - delta_a(i)) > 1e-4) {
            flag = true;
            break;
        }
    }
    if (!C.empty()) {
        ARRAY<1, double> vi((int)C.size()), X((int)C.size());
        ARRAY<2, double> A((int)C.size(), (int)C.size());
        int s = 0;
        for (set<int>::iterator it = C.begin(); it != C.end(); ++it) {
            s++;
            int s1 = 0;
            for (set<int>::iterator it1 = C.begin(); it1 != C.end(); ++it1) {
                s1++;
                A(s,s1) = a(*it,*it1);
            }
            vi(s) = -a(*it, d);
        }
        LinearSolve(A, vi, X);
        s = 0;
        for (set<int>::iterator it = C.begin(); it != C.end(); ++it) {
            s++;
            delta_f(*it) = X(s);
            for (int i = 1; i <= n; ++i) {
                delta_a(i) += a(i,*it) * X(s);
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        double t = 0;
        for (int j = 1; j <= n; ++j) {
            t += a(i,j) * delta_f(j);
        }
    }
}
void dijkstra(int sourceX, int sourceY,int destX,int destY)
{
	for (int i = 0; i < N;i++)
	 for (int j = 0; j < M; j++)
			dist[i][j] = INF, pos[i][j]=-1, fatherX[i][j]=0, fatherX[i][j]=0;

	arb.clear();
	dist[sourceX][sourceY] = 0;
	arb.insert( mp(0, mp(sourceX, sourceY) ) );

	pos[sourceX][sourceY] = 0;
	fatherX[sourceX][sourceY] = 0;
	fatherY[sourceX][sourceY] = 0;

	pos[oldx][oldy] = 100;

	int x, y,nx,ny, cst;
	while (!arb.empty())
	{
		x = (arb.begin()->second.first);
		y = (arb.begin()->second.second);

		arb.erase(arb.begin()); 
		if (x == destX && y == destY)
		{
			return;
		}

		for (int k = 1; k < 5; k++)
		{
			nx = x + dx[k]; ny = y + dy[k];

			if (a[pos[x][y]][nx][ny].bTime || a[pos[x][y]][nx][ny].fTime || a[pos[x][y]][nx][ny].wall || pos[nx][ny]>=0) continue;
			
			cst = nodeCost(nx, ny, pos[x][y]); 
			if (dist[nx][ny]> dist[x][y] + cst)
			{
				dist[nx][ny] = dist[x][y] + cst;
				arb.insert(mp(dist[nx][ny], mp(nx, ny)));

				fatherX[nx][ny] = x;
				fatherY[nx][ny] = y;
				pos[nx][ny] = pos[x][y] + 1;
			}
		}
	}
}
Example #18
0
bool PathPlanner::check_in_set(set<cell> nodes_set, int row_index, int cols_index){
	cell current_cell;
	set<cell>::iterator it;

	if (!nodes_set.empty()) {
		for (it = nodes_set.begin(); it != nodes_set.end(); ++it) {
			current_cell = *it;

			if ((current_cell.x_Coordinate == cols_index ) &&
				(current_cell.y_Coordinate == row_index )){
				return true;
		   }
		}
	}

	return false;
}
set<set<T>> powerSetRecursive(const set<T>& s) {
    set<set<T>> powerSet;
    if (s.empty()) {
        powerSet.insert(set<T>());
        return powerSet;
    }
    set<T> rem = s;
    T firstElt = *rem.begin();
    rem.erase(rem.begin());
    set<set<T>> remPowerSet = powerSetRecursive(rem);
    powerSet = remPowerSet;
    for (auto subset : remPowerSet) {
        subset.insert(firstElt);
        powerSet.insert(subset);
    }
    return powerSet;
}
void codevs_ai::produce_bases_near_enemy_castle() { // {{{
    if (not friend_base_ids.empty() and friend_base_ids.size() * 200 + produce_cost(kind::base) <= get_next_resource()) { // MAGIC:
        point<int> dest = { 70, 70 }; // enemy_castle_point ? *enemy_castle_point : bottom_right;
        int d = numeric_limits<int>::max();
        optional<unit_id> id;
        for (auto je : friend_worker_ids) {
            if (is_command_issued(je)) continue;
            auto unit = units[je];
            int e = manhattan(unit->get_pos() - dest);
            if (/* 12 <= e and */ e < d) {
                id = je;
                d = e;
            }
        }
        if (id) issue_command(*id, command::base);
    }
} // }}}
void nextLevel(vector<psi> &v, set<string> &name_set, unordered_map<string, pssb > m, set<string> str_set, int d){
	if(str_set.empty()) return;
	set<string> total_set, tmp_set;
	for(auto &str : str_set){
		// for each of str_set, if not visited, save its name and height
		if(m[str].second) continue;
		m[str].second = true;
		name_set.erase(str);
		v.push_back({str, d});
		// total up the friends of str_set
		tmp_set.clear();
		for(string new_str : m[str].first) tmp_set.insert(new_str);
		total_set.insert(tmp_set.begin(), tmp_set.end());
	}
	// now repeat for those friends
	nextLevel(v, name_set, m, total_set, d+1);
}
Example #22
0
map<int,int> DispatchTask::GetBayesScores(int actor,const set<int> & target,int stype){
	map<int, int> scores;
  if (target.empty()) {
    return scores;
  }

  map<int,int> scores_tmp;
  vector<int> target_v;
  if (target.size() < 5000 ) {
    for (set<int>::iterator it = target.begin(); it != target.end(); ++it) {
      target_v.push_back(*it);
    }
    try {
    scores = FeedNaiveBayesReplicaAdapter::instance().GetScoreDict(actor,
        target_v, stype);
    } catch (Ice::Exception& e) {
      MCE_WARN("DispatchTask::GetBayesScores  call Bayes Exception"
          << " fid:" << feed_->feed << " actor:" << feed_->actor
          << " error:  "<< e);
    }
    return scores;
  } else {
    int i = 1;
    for (set<int>::iterator it = target.begin(); it != target.end(); ++it,++i) {
      target_v.push_back(*it);
      map<int,int> scoresPart;
      if (target_v.size() == 5000 || i == target.size()) {
        try {
         scoresPart = FeedNaiveBayesReplicaAdapter::instance().GetScoreDict(actor,
             target_v,stype);
        } catch (Ice::Exception& e) {
          MCE_WARN("DispatchTask::GetBayesScores  call Bayes Exception"
              << " fid:" << feed_->feed << " actor:" << feed_->actor
              << " error:  "<< e);
        }
        for (map<int,int>::iterator it = scoresPart.begin(); it != scoresPart.end(); ++it ) {
          scores.insert(map<int,int>::value_type(it->first,it->second));
        }
        scoresPart.clear();
        target_v.clear();
      }
    }
    return scores;
  }
  return scores;
}
Example #23
0
void topoSort ()
{
	set <int>::iterator it;
	while (!Q.empty())
	{
		int v = *Q.begin();
		order.push_back (v);
		Q.erase (Q.begin());
		for (it = graph[v].begin(); it != graph[v].end(); it++)
		{
			int u = *it;
			indeg[u]--;
			if (indeg[u] == 0)
				Q.insert (u);
		}
	}
}
Example #24
0
int play(int X){
	s.erase(X);
	if(s.empty()) return -1;
	int min_val = 0, min_pos = -1e9;
	for(set<int> ::iterator it = s.begin(); it != s.end(); it++){
		int tis = 0;
		for(int i=0; i<n; i++){
			tis += a[*it][i] + a[i][*it];
		}
		if(min_pos < tis){
			min_pos = tis;
			min_val = *it;
		}
	}
	s.erase(min_val);
	return min_val;
}
Example #25
0
int minimumStepTogain_C(int a, int b, int &c) {
	if ((!S.empty() && isThisPairExist(a, b)) || (a_max < c && b_max < c)) {
		return 0;
	}
	cout << a << " " << b << " " << c << endl;
	if (a == c || b == c) {
		return 1;
	}
	currentSet();
	S.insert(make_pair(a, b));
	
	if (a > b) { swap(a ,b); }
	if (a > 0 && a < a_max) {
        return (minimumStepTogain_C(a_max, (b - a), c));
    }
    return (minimumStepTogain_C(0, b, c) || minimumStepTogain_C(a_max, b, c));
}
Example #26
0
vector<set<Vec2>> Vec2::calculateLayers(set<Vec2> elems) {
  vector<set<Vec2>> ret;
  while (1) {
    ret.emplace_back();
    set<Vec2> curElems(elems);
    for (Vec2 v : curElems)
      for (Vec2 v2 : v.neighbors4())
        if (!curElems.count(v2)) {
          ret.back().insert(v);
          elems.erase(v);
          break;
        }
    if (elems.empty())
      break;
  }
  return ret;
}
int main() {
    //ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i=0; i<m; i++) {
        int from, to, len;
        cin >> from >> to >> len;
        from--;
        to--;
        G[to].push_back(make_pair(from, len));
        G[from].push_back(make_pair (to, len));
    }
    dlina [0]=0;
    for (int i=1; i<n; i++) {
        dlina[i]=INF;
    }
    q.insert (make_pair (dlina[0], 0));
    while (!q.empty()) {
        int v = q.begin()->second;
        q.erase (q.begin());
        for (int i=0; i<G[v].size(); i++) {
            int to = G[v][i].first, len = G[v][i].second;
            if (dlina[v]+len<dlina[to]) {
                q.erase (make_pair (dlina[to], to));
                dlina[to] = dlina[v]+len;
                parent[to] = v;
                q.insert (make_pair (dlina[to], to));
            }
        }
    }
    //Display shortest path from 1 to n
    
    if (dlina[n-1]==INF) {
        cout << "-1";
        return 0;
    }
    int c=n-1;
    while (c!=0) {
        ans.push_back(c+1);
        c=parent[c];
    }
    ans.push_back(1);
    for(auto it = ans.rbegin(); it != ans.rend(); it++)
        cout << *it << " ";
    return 0;
}
Example #28
0
int main(){
	int n, a[40][40] = {};
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			scanf("%d",&a[i][j]);
			s.insert(i);
		}
	}
	init(n, a);
	while(!s.empty()){
		int jaehyun;
		scanf("%d",&jaehyun);
		if(s.find(jaehyun) == s.end()){
			// This will not happen in actual grading stage.
			puts("Error : Wrong Pick from Jaehyun");
			return 0;
		}
		JH.push_back(jaehyun);

		int seungwon = play(jaehyun);
		if(seungwon == -1) continue;
		else{
			if(s.find(seungwon) == s.end()){
				// If this happens in grading stage, you will get Wrong Answer verdict.
				puts("Error : Wrong Pick from Seungwon");
				return 0;
			}
			s.erase(seungwon);
			SW.push_back(seungwon);
		}
	}
	int ret = 0;
	for(int i=0; i<JH.size(); i++){
		for(int j=0; j<JH.size(); j++){
			ret -= a[JH[i]][JH[j]];
		}
	}
	for(int i=0; i<SW.size(); i++){
		for(int j=0; j<SW.size(); j++){
			ret += a[SW[i]][SW[j]];
		}
	}
	printf("%d",ret);
	return 0;
}
int main(int argc, char *argv[])
{
//	freopen( "data.in", "r", stdin );
//	freopen( "out", "w", stdout );
	int n;
	point a, b, origin;
	while( scanf( "%d", &n ) != EOF )
	{
		origin.input( );
		n <<= 1;
		for( int i = 0; i < n; i += 2 )
		{
			a.input( );
			b.input( );
			a = a - origin;
			b = b - origin;
			if( D( a ^ b ) < 0 ) swap( a, b );
			idx[ i ] = INFO( a, b, 0, i >> 1 );
			idx[ i + 1 ] = INFO( b, a, 1, i >> 1 );
		}
		sort( idx, idx + n, cmp );

		seg.clear( );
		for( int i = 0; i < n; i++ )
		{
			base = idx[ i ].st;
			if( idx[ i ].flag == 0 &&
				atan2( idx[ i ].st.y, idx[ i ].st.x ) > atan2( idx[ i ].ed.y, idx[ i ].ed.x ) )
				seg.insert( idx[ i ] );
		}
		memset( vis, false, sizeof( vis ) );
		for( int i = 0; i < n; i++ )
		{
			base = idx[ i ].st;
			if( idx[ i ].flag == 0 ) seg.insert( idx[ i ] );
			else seg.erase( INFO( idx[ i ].ed, idx[ i ].st, 0, idx[ i ].sgid ) );
			if( !seg.empty( ) ) vis[ seg.begin( ) -> sgid ] = true;
		}
		n >>= 1;
		int ans = 0;
		for( int i = 0; i < n; i++ )
			ans += vis[ i ];
		printf( "%d\n", ans );
	}
	return 0;
}
void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 0;i < n;++i)
		d[i] = INF;
	for (int i = 0;i < n;++i) {
		scanf("%d", &clr[i]);
		if (clr[i] == 1) {
			d[i] = 0;
			s.insert(mp(0, i));
		}
	}
	for (int i = 0;i < m;++i) {
		int f, t, c;
		scanf("%d%d%d", &f, &t, &c);
		--f; --t;
		g[f].pb(t);
		g[t].pb(f);
		w[f].pb(c);
		w[t].pb(c);
	}
	while (!s.empty()) {
		int x = s.begin() -> se;
		s.erase(s.begin());
		for (int i = 0;i < g[x].size();++i) {
			if (d[g[x][i]] > d[x] + w[x][i]) {
				s.erase(mp(d[g[x][i]], g[x][i]));
				d[g[x][i]] = d[x] + w[x][i];
				p[g[x][i]] = x;
				s.insert(mp(d[g[x][i]], g[x][i]));
			}
		}
	}
	int x = -1;
	for (int i = 0;i < n;++i)
		if (clr[i] == 2 && (x == -1 || d[x] > d[i])) {
			x = i;
		}
	if (x == -1 || d[x] == INF) {
		cout << -1;
		return;
	}
	out(x);
	cout << x + 1 << " " << d[x];
	return;
}