题目链接:https://codeforces.com/contest/1243
水题不说了
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 50;
typedef long long ll;
int n;
int a[mx];
int main() {
int t;
scanf("%d",&t);
while (t--) {
scanf("%d",&n);
for (int i=1;i<=n;i++) {
scanf("%d",a+i);
}
sort(a+1,a+1+n);
int mi = 1e7;
int ans = -1;
for (int i=n;i>=1;i--) {
mi = min(mi,a[i]);
ans = max(ans,min(mi,n-i+1));
}
printf("%d\n",ans);
}
return 0;
}
B2 - Character Swap (Hard Version)
固定第一个字符串,然后第二个字符串的当前位置要和第一个串一样,至多用后面的字符换2次就好了,所以是2*n次
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int mx = 100 + 50;
typedef long long ll;
typedef pair<int,int> pa;
int n;
char s1[mx],s2[mx];
int vis[mx];
pa ans[mx];
int siz;
pa find_pos(int x) {
for (int i=x+1;i<=n;i++)
if (s1[i]==s1[x])
return pa(0,i);
else if (s2[i]==s1[x])
return pa(1,i);
}
bool solve() {
for (int i=0;i<26;i++) if(vis[i]%2!=0)
return 0;
for (int i=1;i<=n;i++) {
if (s1[i]==s2[i]) continue;
pa now = find_pos(i);
if (now.fi == 0) {
swap(s2[i],s1[now.se]);
ans[siz++] = pa(now.se,i);
} else {
swap(s1[n],s2[now.se]);
swap(s1[n],s2[i]);
ans[siz++] = pa(n,now.se);
ans[siz++] = pa(n,i);
}
}
return 1;
}
int main() {
int t;
scanf("%d",&t);
while (t--) {
scanf("%d",&n);
scanf("%s%s",s1+1,s2+1);
siz = 0;
memset(vis,0,sizeof(vis));
for (int i=1;s1[i];i++)
vis[s1[i]-'a']++;
for (int i=1;s2[i];i++)
vis[s2[i]-'a']++;
if(!solve()) puts("No");
else {
printf("Yes\n%d\n",siz);
for (int i=0;i<siz;i++)
printf("%d %d\n",ans[i].fi,ans[i].se);
}
}
return 0;
}
超过一个质数肯定都一样,因为肯定有x使得a*p1 - b*p2 == 1,所以i和i-1颜色一样。
然后一个质数答案就是那个质数了
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int mx = 1e6 + 10;
typedef long long ll;
typedef pair<int,int> pa;
ll n;
vector <int> pri;
bool vis[mx];
void init() {
for (int i=2;i<mx;i++) {
if (!vis[i]) pri.push_back(i);
for (int j : pri) {
if (j*i >= mx) break;
vis[i*j] = 1;
if (i%j==0) break;
}
}
}
int main() {
init();
ll n;
int cnt = 0;
scanf("%lld",&n);
ll ans = n;
for (int i:pri) {
if (i >= n) break;
if (n % i == 0) {
if (cnt) return 0*puts("1");
cnt = i;
ans = cnt;
}
while (n % i == 0) n /= i;
}
if (cnt && n != 1) return 0*puts("1");
printf("%lld\n",ans);
return 0;
}
暴力
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int mx = 1e5 + 10;
typedef long long ll;
typedef pair<int,int> pa;
int n,m;
map <int,int> mp[mx];
set <int> st,nt;
void dfs(int u) {
nt.clear();
vector <int> g;
for (int v:st) {
if (!mp[u][v])
g.push_back(v);
else
nt.insert(v);
}
st = nt;
for (int v:g)
dfs(v);
}
int main() {
scanf("%d%d",&n,&m);
int u,v;
for (int i=0;i<m;i++) {
scanf("%d%d",&u,&v);
mp[u][v] = 1;
mp[v][u] = 1;
}
for (int i=1;i<=n;i++)
st.insert(i);
int ans = 0;
while (!st.empty()) {
int now = *st.begin();
st.erase(st.begin());
dfs(now);
ans++;
}
printf("%d\n",ans-1);
return 0;
}
连下边,然后跑一跑环就ok了,注意是简单环
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pa;
typedef long long ll;
const int mx = 100 + 10;
int m;
ll num[mx];
vector <int> g[mx],r[1<<18];
map <ll,int> mp;
map <int,int> mvis;
stack <int> sta;
pa s[mx*mx];
int vis[1<<18],siz;
bool vis2[1<<18];
pa ans[mx];
ll reg;
int get_ed(int x) {
ll y = reg - num[mp[x]] + x;
return mp.count(y)? y : -1;
}
void full (int u) {
int y = 0;
bool mark[20];
memset(mark,0,sizeof(mark));
while (sta.top() != u){
int v = sta.top();
sta.pop();
if (mark[mp[v]]) return ;
mark[mp[v]] = 1;
y |= 1<<(mp[v]-1);
}
if (mark[mp[u]]) return ;
y |= 1<<(mp[u]-1);
if (vis2[y]) return ;
vis2[y] = 1;
s[siz++] = pa(y,u);
}
void dfs(int u) {
if (mvis[u] == 2) return ;
if (mvis[u] == 1) {
full(u);
return ;
}
mvis[u] = 1;
sta.push(u);
int v = get_ed(u);
if (v != -1) dfs(v);
mvis[u] = 2;
}
int main() {
ll sum = 0;
int cnt = 0;
scanf("%d",&m);
for (int i=1;i<=m;i++) {
int c,u;
scanf("%d",&c);
for (int j=0;j<c;j++) {
scanf("%d",&u);
g[i].push_back(u);
mp[u] = i;
num[i] += u,sum += u;
}
}
if (sum % m) return 0*puts("No");
reg = sum / m;
for (int i=1;i<=m;i++) {
for (int j:g[i]) if (!mvis[j])
dfs(j);
}
vis[0] = 1;
for (int i=1;i<(1<<m);i++) {
for (int j=0;j<siz;j++) if((i&s[j].fi) == s[j].fi)
{
vis[i] = vis[i^s[j].fi];
if (vis[i]) {
r[i].push_back(s[j].se);
r[i].insert(r[i].end(),r[i^s[j].fi].begin(),r[i^s[j].fi].end());
break;
}
}
}
int bv = (1<<m) - 1;
if (r[bv].size()) {
puts("Yes");
for (int i:r[bv]) {
int j = i;
do {
int nj = get_ed(j);
int np = mp[j];
ans[mp[nj]] = pa(nj,np);
swap(j,nj);
} while (j != i);
}
for (int i=1;i<=m;i++)
printf("%d %d\n",ans[i].fi,ans[i].se);
} else puts("No");
return 0;
}
标签:typedef,599,题解,ll,long,int,Div,mx,define From: https://blog.51cto.com/u_12468613/6384518