跳到正文

cscst

337517

在洛谷打开
资料、奖项与版本

资料

编号
337517
抓取

二分F1900

代码
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long LL;
LL a[N],st[N],ans[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) 
	{
		cin>>a[i];
	}
	LL res=0,cnt1=0,cnt2=0;
	for(int i=n;i>=1;i--)  //因为要删除数成立,所以不能瞎搞 得从后面开始往前面推 
	{
		st[a[i]]++;
		if(st[a[i]]>=2)  //大于2个的   
		{
			if(st[a[i]]==2)cnt1++; //统计大于2个的个数 
			ans[a[i]]=max(ans[a[i]],cnt1-1); //因为自己本身是2个  不能3个一样 
		}
		else 
		{
			ans[a[i]]=max(ans[a[i]],cnt1);  //和后面的2个进行配对 
		}
	} 
	for(int i=1;i<=n;i++) res+=ans[i];  //把每个数最大的答案加起来 
	cout<<res;
	return 0;
} 
C
#include<bits/stdc++.h>
using namespace std;

long long m, n;
long long s[100010];
long long g[100010];

int main() {
	
	cin >> m >> n;
	for(int i = 1; i <= m; i++)
		cin >> s[i];
	for(int i = 1; i <= n; i++)
		cin >> g[i];
	sort(s+1, s+n+1);
	
	long long x;
	long long ans = 0;
	long long mini = 1e10;
	for(int i = 1; i <= n; i++) {
		x = g[i];
		int l = 1, r = n;
		while(l < r) { 
			int mid = (l+r)/2;
			if(s[mid] >= x) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		int x1 = s[r];
		
		l = 1, r = n;
		while(l < r) {
			int mid = (l+r+1)/2;
			if(s[mid] <= x) {
				l = mid;
			} else {
				r = mid - 1;
			}
		}
		int x2 = s[r];
		
		if(abs(x1-x) >= abs(x2-x)) {
			cout << "student:" << x << "  " << "school:" << x2 << endl;
			ans += abs(x2-x);
		} else {
			cout << "student:" << x << "  " << "school:" << x1 << endl;
			ans += abs(x1-x);
		}
		
		cout << "student:" << x << "  " << "school:" << mini<< endl;
	}
	cout << ans;
}

暴力F1900

C
#include<bits/stdc++.h>
using namespace std;
long long m, n;
long long school[100010];
long long grade[100010];
int main() {
	cin >> m >> n;
	for(int i = 1; i <= m; i++)
		cin >> school[i];
	for(int i = 1; i <= n; i++)
		cin >> grade[i];
	sort(school+1, school+n+1);
	long long x;
	long long ans = 0;
	long long tmp;
	for(int i = 1; i <= n; i++) {
		x = grade[i];
		long long mini = 1e10;
		for(int j = 1; j <= m; j++) {
			if(abs(school[j] - x) < mini) {
				mini = abs(school[j] - x);
				tmp = school[j];
			}
		}

		ans += mini;
		cout << "student:" << x << "  " << "school:" << tmp << endl;
	}
	cout << ans;
}

UVA11572

C
#include<bits/stdc++.h>
using namespace std;
int n, T;
int a[1000010];
map<int, int> vis;
int cnt;
int main() {
	cin >> T >> n;
	for(int i = 1; i <= n; i++)
		cin >> a[i];	
	
	int l = 1;
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		vis[a[i]]++;
		if(vis[a[i]] > 1) {
			while(vis[a[i]]>1)
			{
				vis[a[l]]--;
				l++;
			}
		}
		ans = max(ans, i-l+1);
	}
	cout << ans;
}

P3143

C
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k,a[50005],c[50005],r=2,l,ans;//c[i]用来存从i-1向左最多可以取多少颗钻石。 
int main(){
    scanf("%d %d",&n,&k);
 a[n+1]=2e9;//立个哨兵 
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;++i)
 {
        while(a[r]<=a[i]+k)r++;//可以往右就尽量往右,因为我们只需要最长的。 
        c[r]=max(r-i,c[r]);  //前缀最大值 
        l=max(l,c[i]);//记录第i颗钻石(不包括i)前可以摆最多的钻石。即第一个架子上的钻石。 
        ans=max(ans,l+r-i);//为什么是'r-i'而不是'c[r]'? 因为要防区间重叠.qwq
    }
    printf("%d",ans);
    return 0;
}

gonoi.com.cn

C++
#include<bits/stdc++.h>
using namespace std;
struct node {
    int x, y, step;
};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n;
int ob[2000];
bool vis[2010][2010];
queue<node> q;
void bfs() {
    q.push({1, 1, 0});
    vis[1][1] = 1;
    while(q.size()) {
        if(t.x == n && t.y == n) {
            cout << "YES\n";
            return ;
        }
        node t = q.front();
        q.pop();
        for(int i = 0; i < 4; i++) {
            int x = t.x + dx[i];
            int y = t.y + dy[i];
            if(x <= 0 || y <= 0 || x > n || y > n) continue;
            if(ob[x][y] != 0 && ob[x][y] + 1 <= t.step) continue;
            if(vis[x][y]) contine;
            vis[x][y] = 1;
            q.push(x, y, step+1);
        }
    }
    cout << "NO\n";
    return ;
}
int main() {
    memset(vis, 0, sizeof(vis));
    memset(ob, 0, sizeof(ob));
    while(q.size()) q.pop();
    cin >> n;
    for(int i = 1; i <= 2*n-2; i++) {
        int x, y;
        cin >> x >> y;
        ob[x][y] = i;
    }
    bfs();
    return 0;
}
C
#include<bits/stdc++.h>
using namespace std;
struct node {
	int x, y;
};
int dx[] = {-2, 2, 2, -2, -1, 1, 1, -1, 2, 2, -2, -2};
int dy[] = {1, 1, -1, -1, 2, 2, -2, -2, 2, -2, -2, 2};
int ans[100][100];
bool vis[100][100];
queue<node> q;

void bfs(int sx, int sy) {
	q.push({sx, sy});
	ans[sx][sy] = 0;
	vis[sx][sy] = 1;
	while(q.size()) {
		node t = q.front();
		q.pop();
		for(int i = 0; i < 12; i++) {
			int x = t.x + dx[i];
			int y = t.y + dy[i];
			if(x <= 0 || y <= 0 || x > 100 || y > 100) continue;
			if(vis[x][y]) continue;
			q.push({x, y});
			vis[x][y] = 1;
			ans[x][y] = ans[t.x][t.y] + 1;
		}
	}
	return ;
}
int main() {
	int x1, x2, y1, y2;
	cin >> x1 >> y1;
	cin >> x2 >> y2;
	
	bfs(x1, y1);
	cout << ans[1][1] << endl;
	
	memset(vis, 0, sizeof(vis));
	memset(ans, -1, sizeof(ans));
	while(q.size()) q.pop();
	
	bfs(x2, y2);
	cout << ans[1][1];
}

#include<bits/stdc++.h> using namespace std; #define int long long int T; signed main() { cin >> T; while(T—) { int n, m, k, d; cin >> n >> m >> k >> d; vector<int> c(n+1); vector<int> sum(n+1); for(int x = 1; x <= n; x++) { vector<int> a(m+1); vector<int> dp(m+1, 0); for(int i = 1; i <= m; i++) cin >> a[i]; dp[1] = 1; deque<int> q; q.push_back(1); for(int i = 2; i <= m; i++) { while(!q.empty() && q.front() < i - d - 1) q.pop_front(); dp[i] = dp[q.front()] + a[i] + 1; while(!q.empty() && dp[q.back()] > dp[i]) q.pop_back(); q.push_back(i); } c[x] = dp[m]; } sum[1] = c[1]; for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + c[i]; int ans = 1e18; for(int i = 1; i <= n - k + 1; i++) ans = min(ans, sum[i+k-1] - sum[i-1]); cout << ans << “\n”; } return 0; }

还没有收录这位用户的文章。