1687B - Railway System - CodeForces Solution


constructive algorithms graphs greedy interactive sortings *1700

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
 
def query(x):
	print("? " + "".join(x))
	return input().strip()
 
edges = [0 for i in range(m)]
ask = ["0"]*m
for i in range(m):
	ask[i-1] = "0"
	ask[i] = "1"
	edges[i] = int(query(ask))
 
s = sorted((v, i) for i, v in enumerate(edges))
 
c_sum = 0
ask[-1] = "0"
 
for v, s_i in s:
	ask[s_i] = "1"
	q = int(query(ask))
 
	if c_sum + v > q:
		ask[s_i] = "0"
	else:
		c_sum = q
 
print("!", c_sum)

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
typedef long long int ll;
#define MAXN 300010
const ll MOD = 1e9+7;

ll x[MAXN],y[MAXN],a[MAXN],b[MAXN];
vector<int> adj[20000];
ll n,m,k;


void solve() 
{
    cin >> n >> m;

    vector<pair<int,int>> weights;

    
    for (int i = 0; i < m; i++) {
        vector<char> sV(m, '0');
        sV[i] = '1';
        string s(sV.begin(), sV.end());
        cout << "? "<< s << endl;
        int val;
        cin >> val;
        weights.push_back({val,i});
    }
    sort(weights.begin(), weights.end());

    vector<char> resV(m, '0');

    ll currCost = 0;
    for (int i = 0;i < m; i++) {
        resV[weights[i].second] = '1';
        string s(resV.begin(), resV.end());
        cout << "? " << s << endl;
        int val;
        cin >> val;
        if (val - currCost == weights[i].first) {
            currCost = val;
        } else {
            resV[weights[i].second] = '0';
        }
    }


    cout << "! " <<currCost << endl;




}


int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
 
	ll t = 1;
	while (t--) {
        solve();
	}
	return EXIT_SUCCESS;
}


Comments

Submit
0 Comments
More Questions

292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents